2013-11-26 78 views
0

我得到了一个自定义的ListView,并且我使用一个自定义的List适配器将三个TextView放入列表中!我怎样才能添加一个复选框与每个列表项目,并使每个复选框检查列表项目单击?CheckBox在Android上的自定义ListView中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/name" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:textColor="#7a4b9d" 
     android:textSize="15sp" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/calorie" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="sdf" 
     android:textColor="#ffffff" /> 

    <TextView 
     android:id="@+id/price" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="sdf" 
     android:textColor="#ffffff" /> 
    <CheckBox 
     android:id="@+id/chkBox1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="CheckBox" /> 

</LinearLayout> 

MainActivity

public class Main_Activity extends Fragment implements OnClickListener { 

    private ListView lv; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     ViewGroup root = (ViewGroup) inflater.inflate(R.layout.main_activity, null); 


     ArrayList<SearchResults> searchResults = GetSearchResults(); 

     final ListView lv = (ListView) root.findViewById(R.id.list_two); 
     lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
     lv.setTextFilterEnabled(true); 
     lv.setAdapter(new MyCustomBaseAdapter(getActivity(), searchResults)); 

     lv.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> a, View v, int position, 
        long id) { 

            boolean result = searchResults.get(position).isSolved(); 
       if (result) { 
        searchResults.get(position).setSolved(false); 
       } else { 
        searchResults.get(position).setSolved(true); 
       } 

       Toast.makeText(getActivity(),"You have chosen: " + " " + 
       fullObject.getPhone(), Toast.LENGTH_SHORT).show(); 

      } 
     }); 

     return root; 

    } 

    private ArrayList<SearchResults> GetSearchResults() { 
     ArrayList<SearchResults> results = new ArrayList<SearchResults>(); 

     SearchResults sr = new SearchResults(); 
     sr.setName("Apple"); 
     sr.setCalorie("35"); 
     sr.setPrice("5"); 
     results.add(sr); 

     return results; 
    } 


} 

定制底座适配器

public class MyCustomBaseAdapter extends BaseAdapter { 
    private static ArrayList<SearchResults> searchArrayList; 

    private LayoutInflater mInflater; 

    public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) { 
     searchArrayList = results; 
     mInflater = LayoutInflater.from(context); 
    } 

    public int getCount() { 
     return searchArrayList.size(); 
    } 

    public Object getItem(int position) { 
     return searchArrayList.get(position); 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.custom_row_view, null); 
      holder = new ViewHolder(); 
      holder.txtName = (TextView) convertView.findViewById(R.id.name); 
      holder.txtCalorie = (TextView) convertView 
        .findViewById(R.id.calorie); 
      holder.txtPrice = (TextView) convertView.findViewById(R.id.price); 
    holder.chkBox1 = (CheckBox)convertView.findViewById(R.id.chkBox1); 


     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    holder.txtName.setText(searchArrayList.get(position).getName()); 
    holder.txtCalorie.setText(searchArrayList.get(position) 
      .getCalorie()); 
    holder.txtPrice.setText(searchArrayList.get(position).getPrice()); 
    holder.chkBox1.setChecked(searchArrayList.get(position).isSolved()); 


    return convertView; 
} 

static class ViewHolder { 
    CheckBox chkBox1; 
    TextView txtName; 
    TextView txtCalorie; 
    TextView txtPrice; 
} 

搜索结果

public boolean isSolved() { 
    // TODO Auto-generated method stub 
    return b; 
} 

public void setSolved(boolean b) { 
    // TODO Auto-generated method stub 
    this.b = b; 
} 
+0

究竟是什么问题? – SimonSays

+0

如何将CheckBox添加到自定义列表中? @SimonSays –

回答

2

您必须首先定义custom_row_view.xml文件的复选框观点,

<CheckBox android:id="@+id/chkBox1" 
android:layout_width=... 
android:layout_height=... /> 

然后,无线本地环路在你MyCustomBaseAdapter类或者在你的情况下,持有人称之为参考,像

CheckBox chkBox1; 

然后,

holder.chkBox1 = = (CheckBox)convertView.findViewById(R.id.chkBox1); 

您可以定义一个布尔您的SearchResults类中的值可以处理chkBox检查和使用

holder.chkBox1.setChecked(searchArrayList.get(position).isSolved()); 

东西在这些线路上,你会好到哪里去:)

编辑:请记住,改变布尔值的信息搜索结果的实例上itemClick在。

boolean result = searchResults.get(position).isSolved(); 
if (result) { 
    searchResults.get(position).setSolved(false); 
} else { 
    searchResults.get(position).setSolved(true); 
} 
lv.getAdapter().notifyDataSetChanged(); 
+0

在更改searchResults模型后调用'adapter.notifyDataSetChanged();'。 –

+0

是否有任何条件,如果结果(...){} else {}? –

+0

'结果'本身就是一个条件。如果结果为真,这意味着复选框被选中,因此您将其设置为false,反之亦然 –