我想显示一个带有复选框的列表视图,如
checkbox listitem1
checkbox listitem2
checkbox listitem3
.
.
.
.
如果单击listview中的任何listitem,则选中相应的复选框将为true。我试过下面的代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox android:text=""
android:id="@+id/list_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
></CheckBox>
<TextView
android:id="@+id/songname"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="10px"
android:layout_marginLeft="60px"/>
<TextView
android:id="@+id/artist"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="30px"
android:layout_marginLeft="60px"/>
</RelativeLayout>
类文件是
list.setOnItemClickListener(this);
list.setAdapter(new EfficientAdapter(this));
private static class EfficientAdapter extends BaseAdapter
{
private LayoutInflater mInflater;
public EfficientAdapter(Context context)
{
mInflater = LayoutInflater.from(context);
}
public int getCount()
{
return title.length;
}
public Object getItem(int position)
{
return 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.selectsongs, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.songname);
holder.artist = (TextView) convertView.findViewById(R.id.artist);
holder.check = (CheckBox) convertView.findViewById(R.id.list_checkbox);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(title[position]);
holder.artist.setText(artist[position]);
return convertView;
}
static class ViewHolder
{
TextView title,artist;
CheckBox check;
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
Log.e("name",title[position]);
}
但是在这个列表视图中的OnClickItemClickListener不起作用。当我点击列表视图中的listitem上的复选框时,复选框checkable为true。因此,请告诉我如何使用复选框显示列表视图,并且当我点击listitem时,listitem复选框checkable也是如此。
最诚挚的问候。
提前致谢
答案 0 :(得分:3)
最好使用ChechBoxPreference完成列表。
使用首选项的主要优点是您不需要编写代码来保存值,您可以轻松获取任何活动中的值。该值作为键对值存储在android首选项中。您可以使用'KeyName'来引用该值。
以下链接将帮助您了解此事:
答案 1 :(得分:0)
我有同样的问题。我刚刚在2天前修好了它:
在:public View getView
中 cb =(CheckBox)row.findViewById(R.id.CheckBox01);
cb.setChecked(etat[position]);
final int xt=position;
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
if(arg0.isChecked())
{
etat[xt]=true;
//Update the state of Checkbox to the: "tabEtat"
Etat.getInstance().setAddIdEtat(String.valueOf(xt));
}
}
else
{
//Update the state of Checkbox to the : "tabEtat"
Etat.getInstance().setDeleteIdEtat(String.valueOf(xt));
}
}
}
});
答案 2 :(得分:0)
经过两个小时的努力,我得到了解决方案。
/**
*This is Adapter class
*/
public class CustomListAdapter extends BaseAdapter {
private String[] stringArray;
private Context mContext;
private LayoutInflater inflator;
int checkbox;
/**
*
* @param context
* @param stringArray
*/
public CustomListAdapter(Context context, String[] stringArray)
{
this.mContext=context;
this.stringArray=stringArray;
this.inflator= (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount()
{
return stringArray.length;
}
@Override
public Object getItem(int position)
{
return position;
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
final MainListHolder mHolder;
View v = convertView;
if (convertView == null)
{
mHolder = new MainListHolder();
v = inflator.inflate(android.R.layout.simple_list_item_multiple_choice, null);
mHolder.txt=(CheckedTextView) v.findViewById(android.R.id.text1);
v.setTag(mHolder);
}
else
{
mHolder = (MainListHolder) v.getTag();
}
mHolder.txt.setText(stringArray[position]);
mHolder.txt.setTextSize(12);
mHolder.txt.setTextColor(Color.YELLOW);
/**
* When checkbox image is set By setImageFromResourceCheckBox(int id) method...Otherwise it takes default
*/
if(checkbox!=0)
mHolder.txt.setCheckMarkDrawable(R.drawable.android_button);
mHolder.txt.setPadding(5, 5, 5, 5);
return v;
}
class MainListHolder
{
private CheckedTextView txt;
}
/***
* Setting Image for Checkbox
* @param id
*
*/
public void setImageFromResourceCheckBox(int id)
{
this.checkbox=id;
}
}
活动类应该是这样的。
public class MyActivity extends ListActivity implements OnItemClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CustomListAdapter c=new CustomListAdapter(this,GENRES);
// c.setImageForCheckBox(R.drawable.android_button);//Image for checkbox
setListAdapter(c);
final ListView listView = getListView();
listView.setItemsCanFocus(false);
// listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);// For single mOde
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setItemChecked(2, true);
listView.setOnItemClickListener(this);
}
private static String[] GENRES = new String[] {
"Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
"Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
};
@Override
public void onItemClick(AdapterView<?> adapter, View arg1, int arg2, long arg3)
{
SparseBooleanArray sp=getListView().getCheckedItemPositions();
String str="";
for(int i=0;i<sp.size();i++)
{
str+=GENRES[sp.keyAt(i)]+",";
}
Toast.makeText(this, ""+str, Toast.LENGTH_SHORT).show();
}
}