Android上的自定义ListView中的CheckBox

时间:2013-11-26 21:53:22

标签: android android-listview

我有一个自定义ListView,我使用自定义列表适配器将三个TextView放入List中!如何添加每个列表项的CheckBox并使列表项上的每个CheckBox单击?

<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;
}

1 个答案:

答案 0 :(得分:2)

您必须首先在custom_row_view.xml文件中定义CheckBox视图,

<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上更改searchResult实例中的boolean值。

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