2012-07-09 49 views
2

我已經創建了一個ListView包含了CheckedTextView。 我正在通過一個自定義的ArrayAdapter在此ListView中提供元素。顯示 列表罰款,但該複選框沒有響應,當我選擇列表項CheckedTextView不能在自定義ListView中工作

代碼:

MainActivity類別:

public class MainActivity extends ListActivity { 

    ArrayList<String> m_list; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     new AsyncHandler(this).execute(); 
    } 


    public class AsyncHandler extends AsyncTask { 

     Context context; 

     public AsyncHandler(Context c) { 
      context = c; 
     } 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      Toast.makeText(context, "In onPreExecute()", Toast.LENGTH_SHORT) 
        .show(); 
     } 

     @Override 
     protected Object doInBackground(Object... arg0) { 
      getList(); 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Object result) { 
      // super.onPostExecute(result); 
      setListAdapter(new ElementAdapter(context, m_list)); 
     } 

     private void getList() { 
      m_list = new ArrayList<String>(); 
      m_list.add("Ele 1"); 
      m_list.add("Ele 2"); 
      m_list.add("Ele 3"); 
      m_list.add("Ele 4"); 
     } 
    } 

} 

適配器類

public class ElementAdapter extends ArrayAdapter implements OnClickListener { 

    ArrayList<String> items = new ArrayList<String>(); 
    Context context; 
    CheckedTextView tvElement; 

    public ElementAdapter(Context c, ArrayList<String> elements) { 
     super(c, R.layout.row, elements); 
     this.items = elements; 
     this.context = c; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(context.LAYOUT_INFLATER_SERVICE); 
     View view = inflater.inflate(R.layout.row, parent, false); 
     tvElement = (CheckedTextView) view 
       .findViewById(R.id.ctvElements); 
     tvElement.setText(items.get(position)); 
     tvElement.setOnClickListener(this); 
     return view; 
    } 

    @Override 
    public void onClick(View view) { 
     switch(view.getId()){ 
     case R.id.ctvElements: 
      if(!tvElement.isChecked()){ 
       tvElement.setChecked(true); 
       tvElement.setCheckMarkDrawable(android.R.drawable.checkbox_on_background); 
      }else{ 
       tvElement.setChecked(false); 
       tvElement.setCheckMarkDrawable(android.R.drawable.checkbox_off_background); 
      } 
      notifyDataSetChanged(); 
     } 
    } 
} 

行佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:orientation="horizontal"> 

    <CheckedTextView android:id="@+id/ctvElements" 
     android:paddingLeft="20dip" android:paddingRight="20dip" 
     android:paddingTop="10dip" android:paddingBottom="10dip" 
     android:orientation="vertical" android:layout_width="fill_parent" 
     android:layout_height="?android:attr/listPreferredItemHeight" 
     android:gravity="center_vertical" android:checkMark="@android:drawable/checkbox_off_background" 
     android:focusable="false" /> 

    <!-- <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:focusable="false" /> <TextView 
     android:id="@+id/tvElement" android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:text="Element" android:textSize="25sp" /> --> 

</LinearLayout> 
+0

檢查這個http://www.marvinlabs.com/2010/10/custom-listview-ability-check- items/ – Ran 2012-07-09 17:08:10

回答

4

如果您只想爲每行添加複選標記,請使用ListView.setChoiceMode()。選中標記將響應該行中任何地方的點擊,而無需添加任何onClickListeners或自定義適配器。

添加到您的onCreate:

ListView listView = (ListView) findViewById(android.R.id.list); 
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 

然後使用內置ArrayAdapter:

@Override 
    protected void onPostExecute(Object result) { 
     // super.onPostExecute(result); 
     setListAdapter(new ArrayAdapter<String>(context, R.layout.row, m_list)); 
    } 

您可以使用默認選中的行佈局android.R.layout.simple_list_item_checked

否則,如果你想要一些定製的ListView行更改row.xml這樣:

<CheckedTextView android:id="@android:id/text1" 
     android:layout_height="?android:attr/listPreferredItemHeight" 
     android:layout_width="fill_parent" 
     android:checkMark="@android:drawable/checkbox_off_background" 
     android:gravity="center_vertical" 
     android:paddingLeft="20dip" 
     android:paddingRight="20dip" 
     android:paddingTop="10dip" 
     android:paddingBottom="10dip" 
     /> 
+0

我可以問你,如果我們應用過濾器不會保存你的位置爲什麼? – 2016-08-22 09:15:18

相關問題