请原谅我刚开始使用 Android 时可能听起来像新手的问题。
我试图完成的是创建一个自定义列表项布局,其中包含 2 个文本视图和一个用于选择多个项目的复选框。问题是复选框是“额外的”选择列表项,(例如)如果我选择 #1,那么 #9 和 #18 也会选择。好像 checkBox 实例正在回收自己,或者如果这有意义的话,可能共享相同的侦听器 ID。为简单起见,我已将代码缩小到主要组件。任何建议将不胜感激。
package com.untame.mobile.app;
import java.text.ChoiceFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import com.untame.mobile.app.*;
public class TestList extends ListActivity {
ArrayList<Map<String, String>> artistList;
private static String TAG = "TESTLIST!";
ListView listv;
LayoutInflater mInflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testlayout);
mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
artistList = new ArrayList<Map<String, String>>();
for(Integer loop = 0 ;loop < 21;loop++){
String loopI = loop.toString();
Map<String,String> hm = new HashMap<String, String>();
hm.put("artist", loopI);
hm.put("count", loopI);
artistList.add(hm);
}
// Loading artistNames in Background Thread
// new LoadArtistList().execute("extra_tracks");
listv = (ListView) getListView();
listv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
// listv.setItemsCanFocus(false);
final class MyListAdapter extends BaseAdapter{
ArrayList<Integer> ids;
public MyListAdapter(Context context) {
ids = new ArrayList<Integer>();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.testlistitem1, null, false);
convertView.setClickable(true);
holder = new ViewHolder(getApplicationContext());
holder.choose = (CheckBox) convertView.findViewById(R.id.testcheckBox1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return artistList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return artistList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
MyListAdapter adapter = new MyListAdapter(this);
setListAdapter(adapter);
}
}
ViewHolder.java
package com.untame.mobile.app;
import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
public class ViewHolder extends View {
public ViewHolder(Context context) {
super(context);
}
public CheckBox remove;
public CheckBox choose;
public TextView text2;
public TextView text1;
}
测试布局.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
testitem.xml
<?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="vertical" >
<com.untame.mobile.app.ArtistListCheckBox
android:id="@+id/testcheckBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="choose"
/>
</LinearLayout>