Android ListView 自訂用法(ListView 實現單選功能)

來源:互聯網
上載者:User

本來想發圖片的,可是太笨了不會發哇。。

個人化的listview 方法有很多,我還是喜歡自訂adapter 因為看起來比較條理清晰。

首先你要建立你的listview

<ListView android:layout_width="fill_parent"     android:cacheColorHint="#00000000"   android:id="@layout/userone"   android:dividerHeight="0.5dip"   android:layout_height="wrap_content"> </ListView>

android:cacheColorHint="#00000000"

這句的意思是底色設為透明。如果你是黑色的listview的話這句就沒必要了。這句是個人化其他顏色和風格的listview的時候

解決listview點擊不鬆開下拉的時候部分變黑的問題。

android:id="@layout/userone"
這句的意思是定義id 名稱,為什麼這麼定義了因為我在layout目錄下有一個userone.xml作為listview的每一項。個人化由此開始。

那是不是你的userone.xml就是跟你的listview每項都一樣了呢。沒這麼簡單哇。要進行代碼編寫。如下。

public class ModelActivity extends Activity {private ListView listview;public void onCreate(Bundle savedInstanceState) {listview = (ListView) findViewById(R.layout.userone);//你直接找listview的座標也哦哦listview.setAdapter(new MyAdapter(this));// 加入適配器-適配器就是簡單的說就是copy的工具,就是當很多一樣的布局可以進行複製。}}//需要重寫4個方法public class MyAdapter extends BaseAdapter {private LayoutInflater inflater;//這個是神馬?反正自訂介面都要用它。private View myView;public MyAdapter(Context c) {   this.inflater = LayoutInflater.from(c);  } public int getCount() {   return 5;//這個就是適配器複製的數量。返回1就1項。  }  public Object getItem(int position) {   return null;//返回每項的對象  }  public long getItemId(int position) {   return 0;//返回每項的id  }//getView為主要方法 public View getView(int position, View convertView, ViewGroup parent) { myView = inflater.inflate(R.layout.userone, null);return myView ;}}

//這樣自訂listview就成功啦,當然這是個簡單的啦。

上面的userone.xml進行自訂。

貼一下My Code

<?xml version="1.0" encoding="utf-8"?><LinearLayout android:id="@+id/LinearLayout01" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:orientation="horizontal" android:gravity="center_vertical" android:layout_height="75dip"> <TableLayout android:id="@+id/TableLayout01"  android:layout_width="fill_parent" android:layout_height="75dip"  android:layout_weight="1">  <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content"   android:layout_height="wrap_content">   <TextView android:id="@+id/TextView_model"    android:layout_weight="2" android:layout_width="fill_parent"    android:textColor="@color/ziti" android:gravity="center_vertical"    android:textSize="20sp" android:layout_height="35dip"    android:layout_marginLeft="10dip"></TextView>  </TableRow>  <TableRow android:id="@+id/TableRow02" android:layout_height="fill_parent"   android:layout_width="fill_parent">   <TextView android:id="@+id/TextView01" android:text="@string/ps_jiawu"    android:layout_width="fill_parent" android:layout_height="30dip"    android:textColor="@color/ziti" android:layout_weight="2"    android:textSize="13sp" android:gravity="center_vertical"    android:layout_marginLeft="10dip"></TextView>  </TableRow> </TableLayout> <CheckBox android:id="@+id/CheckBox01" android:layout_width="wrap_content"  android:layout_height="wrap_content"></CheckBox></LinearLayout>

下面你就修改getview方法啦

public View getView(int position, View convertView, ViewGroup parent) {   myView = inflater.inflate(R.layout.userone, null);   mCursor.moveToPosition(position);   final TextView textView = (TextView) myView     .findViewById(R.id.TextView_model);   final TextView textView01 = (TextView) myView     .findViewById(R.id.TextView01);   final CheckBox CheckBox01 = (CheckBox) myView     .findViewById(R.id.CheckBox01);int a = mCursor.getInt(8);//哈哈這裡就用到資料庫拉。我吧checkbox的狀態存在了資料庫中 2為選中,1為不選中。當然你也可以不存資料庫,用MyPreference來存取。也不錯哦   textView.setText(mCursor.getString(1));//getString(1)就是textview的顯示,從資料庫讀取的。   if (a == 2) {    CheckBox01.setChecked(true);   }   if (a == 1) {    CheckBox01.setChecked(false);   }   CheckBox01.setFocusable(false);   CheckBox01.setOnClickListener(new OnClickListener() {    public void onClick(View v) {     for (int num = 0; num < mCursor.getCount(); num++) {      if (mCursor.moveToPosition(num)        && mCursor.getInt(8) == 2) {       mDateBase.update(mCursor.getString(1), 1);      }     }     modelname = textView.getText().toString();     mDateBase.update(modelname, 2);     Toast.makeText(ModelActivity.this,       modelname + " " + getString(R.string.toast_defult),       Toast.LENGTH_SHORT).show();//發送訊息通知數據庫更新     Message message = new Message();     message.what = 1;     handler.sendMessage(message);    }   });   return myView;  }public Handler handler = new Handler() {  public void handleMessage(Message msg) {   switch (msg.what) {   case 1:    listview.invalidateViews();    mCursor.deactivate();    break;     }   super.handleMessage(msg);  } };

 

哦啦嘿嘿。資料庫不明白恩。。下次寫個資料庫吧

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.