好吧,我坚持整天尝试在列表视图中添加复选框。我有两部分Android - 在ListView中的多个选择
- 如果我点击列表框中的任何项目,它会加载一些东西(这部分已经实现)。
- 每个项目都有一个复选框。我希望能够检查任何10个或任意数量的项目,以便即使滚动浏览列表,选择也不会丢失。
任何人都可以请帮助我吗?我检查的一个解决方案是this solution,但我不想重构整个代码,除非有必要。
MAINACTIVITY:
public class ShowList extends Activity{
static final String LIST_KEY_ID = "bookid";
static final String LIST_KEY_NAME = "bookname";
static final String LIST_KEY_WRITER = "writer";
ListView list;
MylibmanList adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_list);
setupActionBar();
mylibmandbhandler db = new mylibmandbhandler(this);
List<mylibman> allfld = db.getAllRecord();
db.close();
ArrayList<HashMap<String, String>> allbookList = new ArrayList<HashMap<String, String>>();
for (mylibman cn : allfld) {
HashMap<String, String> map = new HashMap<String, String>();
map.put(LIST_KEY_ID, Integer.toString(cn.getBookid()));
map.put(LIST_KEY_NAME, cn.getBookname());
map.put(LIST_KEY_WRITER, cn.getWriter());
allbookList.add(map);
}
list=(ListView)findViewById(R.id.list);
adapter=new MylibmanList(this, allbookList);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
batchid.clear();
HashMap<String, String> list_hashMap = (HashMap<String, String>) parent.getItemAtPosition(position);
String currlist = list_hashMap.get(LIST_KEY_ID);
Intent returnIntent = new Intent();
returnIntent.putExtra("bookid",currlist);
setResult(RESULT_OK,returnIntent);
finish();
}
});
适配器类:
public class MylibmanList extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public MylibmanList(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
static class ViewHolder {
TextView title;
TextView artist;
TextView duration;
CheckBox check;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.listrow_row, null);
ViewHolder holder = new ViewHolder();
holder.title = (TextView)vi.findViewById(R.id.title);
holder.artist = (TextView)vi.findViewById(R.id.artist);
holder.duration = (TextView)vi.findViewById(R.id.duration);
holder.check = (CheckBox)vi.findViewById(R.id.check);
holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d("test","t3"); // Don't know what to do
}
});
vi.setTag(holder);
holder.check.setTag(data.get(position));
HashMap<String, String> book = new HashMap<String, String>();
book = data.get(position);
ViewHolder holderfin = (ViewHolder) vi.getTag();
holderfin.title.setText(book.get(ShowList.LIST_KEY_NAME));
holderfin.artist.setText(book.get(ShowList.LIST_KEY_WRITER));
holderfin.duration.setText(book.get(ShowList.LIST_KEY_ID));
holderfin.check.setChecked(false);
return vi;
}
}
XML:
<?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="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:background="@drawable/image_bg"
android:layout_marginRight="5dip">
<!--
<ImageView
android:id="@+id/list_image"
android:contentDescription="@string/bookimage"
android:layout_width="50dip"
android:layout_height="50dip"/>
-->
</LinearLayout>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:textColor="#040404"
android:typeface="sans"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/artist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:textColor="#343434"
android:textSize="13sp"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail" />
<TextView
android:id="@+id/duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/title"
android:gravity="right"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:layout_marginRight="5dip"
android:textSize="12sp"
android:textColor="#10bcc9"
android:textStyle="bold"/>
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
android:layout_marginRight="10dp" />
</RelativeLayout>
东西可以添加listrow_row的XML做可以帮助你和你的第一个问题也是,要检查上的复选框上点击复选框时,或者当你点击'onItemClick'或者你可以考虑'onItemLongClick'?请更精确地说你想要什么 – Coderji
对不起,我忘了..加入 – abdfahim
我希望能够从列表视图中选择10或任何随机行(比如说我有100行),每次我检查一个checbox时,有一个“持续时间”ArrayList ..现在,如果我检查一些复选框,并向下滚动,从最后一个视图中的所有选择都会丢失(我猜listview是这样的) – abdfahim