android listview的多列模版實(shí)例代碼
android listview多列模版
在listview中,可以做出多列模版的效果,關(guān)鍵還是在listview的模版本,比如如下:
<LinearLayout
android:id="@+id/relativeLayout1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/FirstText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="First"
android:layout_weight="1">
</TextView>
<TextView
android:id="@+id/SecondText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Second"
android:layout_weight="2">
</TextView>
<TextView
android:id="@+id/ThirdText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Third"
android:layout_weight="1">
</TextView>
<TextView
android:id="@+id/FourthText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Fourth"
android:layout_weight="1">
</TextView>
</LinearLayout>
listviewadapter.java:
public class listviewAdapter extends BaseAdapter
{
public ArrayList<HashMap<String,String>> list;
Activity activity;
public listviewAdapter(Activity activity, ArrayList<HashMap<String,String>> list) {
super();
this.activity = activity;
this.list = list;
}
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView txtFirst;
TextView txtSecond;
TextView txtThird;
TextView txtFourth;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.listview_row, null);
holder = new ViewHolder();
holder.txtFirst = (TextView) convertView.findViewById(R.id.FirstText);
holder.txtSecond = (TextView) convertView.findViewById(R.id.SecondText);
holder.txtThird = (TextView) convertView.findViewById(R.id.ThirdText);
holder.txtFourth = (TextView) convertView.findViewById(R.id.FourthText);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
HashMap<String, String> map = list.get(position);
holder.txtFirst.setText(map.get(FIRST_COLUMN));
holder.txtSecond.setText(map.get(SECOND_COLUMN));
holder.txtThird.setText(map.get(THIRD_COLUMN));
holder.txtFourth.setText(map.get(FOURTH_COLUMN));
return convertView;
}
主程序:
public class MultiColumnActivity extends Activity
{
private ArrayList<HashMap<String,String>> list;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lview = (ListView) findViewById(R.id.listview);
populateList();
listviewAdapter adapter = new listviewAdapter(this, list);
lview.setAdapter(adapter);
}
private void populateList() {
list = new ArrayList<HashMap<String,String>>();
HashMap<String,String> temp = new HashMap<String,String>();
temp.put(FIRST_COLUMN,"Colored Notebooks");
temp.put(SECOND_COLUMN, "By NavNeet");
temp.put(THIRD_COLUMN, "Rs. 200");
temp.put(FOURTH_COLUMN, "Per Unit");
list.add(temp);
HashMap<String,String> temp1 = new HashMap<String,String>();
temp1.put(FIRST_COLUMN,"Diaries");
temp1.put(SECOND_COLUMN, "By Amee Products");
temp1.put(THIRD_COLUMN, "Rs. 400");
temp1.put(FOURTH_COLUMN, "Per Unit");
list.add(temp1);
HashMap<String,String> temp2 = new HashMap<String,String>();
temp2.put(FIRST_COLUMN,"Note Books and Stationery");
temp2.put(SECOND_COLUMN, "By National Products");
temp2.put(THIRD_COLUMN, "Rs. 600");
temp2.put(FOURTH_COLUMN, "Per Unit");
list.add(temp2);
HashMap<String,String> temp3 = new HashMap<String,String>();
temp3.put(FIRST_COLUMN,"Corporate Diaries");
temp3.put(SECOND_COLUMN, "By Devarsh Prakashan");
temp3.put(THIRD_COLUMN, "Rs. 800");
temp3.put(FOURTH_COLUMN, "Per Unit");
list.add(temp3);
HashMap<String,String> temp4 = new HashMap<String,String>();
temp4.put(FIRST_COLUMN,"Writing Pad");
temp4.put(SECOND_COLUMN, "By TechnoTalaktive Pvt. Ltd.");
temp4.put(THIRD_COLUMN, "Rs. 100");
temp4.put(FOURTH_COLUMN, "Per Unit");
list.add(temp4);
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android編程實(shí)現(xiàn)TextView垂直自動(dòng)滾動(dòng)功能【附demo源碼下載】
- android利用ContentResolver訪問(wèn)者獲取手機(jī)聯(lián)系人信息
- Android編程實(shí)現(xiàn)EditText字?jǐn)?shù)監(jiān)聽(tīng)并顯示的方法
- android監(jiān)聽(tīng)軟鍵盤的彈出與隱藏的示例代碼
- Android 自定義可拖拽View界面渲染刷新后不會(huì)自動(dòng)回到起始位置
- Android使用百度語(yǔ)音識(shí)別的示例代碼
- Android中ListView + CheckBox實(shí)現(xiàn)單選、多選效果
- iOS 水波紋動(dòng)畫的實(shí)現(xiàn)效果
- Android listview ExpandableListView實(shí)現(xiàn)多選,單選,全選,edittext實(shí)現(xiàn)批量輸入的實(shí)例代碼
- Android ListView 子控件onClick正確獲取position的方法
- Android ListView 和ScroolView 出現(xiàn)onmeasure空指針的解決辦法
相關(guān)文章
Android自定義View控件實(shí)現(xiàn)刷新效果
這篇文章主要介紹了Android自定義View控件實(shí)現(xiàn)刷新效果的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11
Android 實(shí)現(xiàn)帶角標(biāo)的ImageView(微博,QQ消息提示)
下面小編就為大家分享一篇Android 實(shí)現(xiàn)帶角標(biāo)的ImageView(微博,QQ消息提示),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android中實(shí)現(xiàn)Runnable接口簡(jiǎn)單例子
這篇文章主要介紹了Android中實(shí)現(xiàn)Runnable接口簡(jiǎn)單例子,著重點(diǎn)在如何實(shí)現(xiàn)run()方法,需要的朋友可以參考下2014-06-06
Android中監(jiān)聽(tīng)未接來(lái)電的2種方法
這篇文章主要介紹了Android中監(jiān)聽(tīng)未接來(lái)電的2種方法,本文講解了使用廣播接收器 BrocastReceiver和使用 PhoneStateListener二種方法,需要的朋友可以參考下2015-04-04
Android?ViewModel創(chuàng)建不受橫豎屏切換影響原理詳解
這篇文章主要為大家介紹了Android?ViewModel創(chuàng)建不受橫豎屏切換影響原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
詳解Android封裝一個(gè)全局的BaseActivity
這篇文章主要介紹了詳解Android封裝一個(gè)全局的BaseActivity,對(duì)封裝感興趣的同學(xué),可以參考下2021-04-04
Android獲取當(dāng)前已連接的wifi信號(hào)強(qiáng)度的方法
這篇文章主要介紹了Android獲取當(dāng)前已連接的wifi信號(hào)強(qiáng)度的方法,主要通過(guò)系統(tǒng)自帶的WifiInfo類實(shí)現(xiàn),需要的朋友可以參考下2014-09-09

