我正在尝试与ListView中的CheckBox实例进行交互。我创建了一个CheckBox数组,将ListView中的CheckBox项复制到它,然后创建了一个公共方法 - checkIfNull - 来获取这个数组,但是它表示数组中的所有元素都是null。你能帮我解决这个问题吗?下面是我的ListAtapter代码,它扩展了BaseAdapte,主要活动和listview的布局。
提前谢谢
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow android:id="@+id/tableRow1"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<CheckBox android:layout_height="wrap_content" android:layout_width="wrap_content"
android:id="@+id/checkBox1" />
<TwoLineListItem android:id="@+id/twoLineListItem1"
android:layout_weight="1"
android:paddingTop="10px"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:id="@+id/textView1"
android:text="TextView"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView android:id="@+id/textView2"
android:text="TextView"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:paddingTop="30px"/>
</TwoLineListItem>
<TextView android:text="TextView"
android:id="@+id/textView3"
android:layout_weight="1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:paddingTop="10px"/>
<RadioButton android:id="@+id/radioButton1"
android:gravity="center|center_vertical"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
public class ListViewSampleMain extends ListActivity {
private String[] list1 = {"Lionel Messi", "Kai Hansen", "Jim Carrey","Christoper Nolan",
"ABC", "Persona", "Shawdow and Collosuss"};
private String[] list2 = {"Argentina", "Germany", "Canada", "U.S.A.",
"Unknown", "Japan", "Ico"};
private String[] list3 = {"March 2nd", "January 22th", "December 15th", "Februray 1st",
"May 4th", "August 23th", "July 17th"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createListViewWithListActivity();
}
private void createListViewWithListActivity(){
ListAdapter listAdapter = new ListAdapter(this, list1, list2, list3);
this.setListAdapter(listAdapter);
listAdapter.checkIfNull();
}
public class ListAdapter extends BaseAdapter {
private Context mContext;
private String[] list1;
private String[] list2;
private String[] list3;
private static LayoutInflater layoutInflater = null;
private CheckBox[] cb_list;
public ListAdapter(Context context,
String[] list1, String[] list2, String[] list3){
this.mContext = context;
this.list1 = list1;
this.list2 = list2;
this.list3 = list3;
ListAdapter.layoutInflater =
(LayoutInflater)this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
cb_list = new CheckBox[list1.length];
}
public int getCount() {
// TODO Auto-generated method stub
return list1.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder viewHolder;
// To interact with OnClickListener()
final int pos = position;
if(convertView == null){ // check if convertView exists
view = layoutInflater.inflate(R.layout.listview_layout, null);
viewHolder = new ViewHolder();
viewHolder.tv1 = (TextView)view.findViewById(R.id.textView1);
viewHolder.tv2 = (TextView)view.findViewById(R.id.textView2);
viewHolder.tv3 = (TextView)view.findViewById(R.id.textView3);
viewHolder.cb = (CheckBox)view.findViewById(R.id.checkBox1);
viewHolder.cb.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Toast.makeText(mContext, "You clicked: "+pos, Toast.LENGTH_SHORT).show();
}
});
cb_list[position] = (CheckBox) view.findViewById(R.id.checkBox1);
view.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder)view.getTag();
}
viewHolder.tv1.setText(this.list1[position]);
viewHolder.tv2.setText(this.list2[position]);
viewHolder.tv3.setText(this.list3[position]);
return view;
}
public void checkIfNull(){
for(int i=0; i<cb_list.length; i++){
if (cb_list[i] == null)
Log.d("checkIfNull method", "cb_list["+i+"] is null");
else if (cb_list[i] != null)
Log.d("checkIfNull method", "cb_list["+i+"] is not null");
}
}
public static class ViewHolder{
public TextView tv1;
public TextView tv2;
public TextView tv3;
public CheckBox cb;
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createListViewWithListActivity();
}
private void createListViewWithListActivity(){
ListAdapter listAdapter = new ListAdapter(this, list1, list2, list3);
this.setListAdapter(listAdapter);
listAdapter.checkIfNull();
}
public ListAdapter(Context context,
String[] list1, String[] list2, String[] list3){
this.mContext = context;
this.list1 = list1;
this.list2 = list2;
this.list3 = list3;
ListAdapter.layoutInflater =
(LayoutInflater)this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
cb_list = new CheckBox[list1.length];
}
public int getCount() {
// TODO Auto-generated method stub
return list1.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder viewHolder;
// To interact with OnClickListener()
final int pos = position;
if(convertView == null){ // check if convertView exists
view = layoutInflater.inflate(R.layout.listview_layout, null);
viewHolder = new ViewHolder();
viewHolder.tv1 = (TextView)view.findViewById(R.id.textView1);
viewHolder.tv2 = (TextView)view.findViewById(R.id.textView2);
viewHolder.tv3 = (TextView)view.findViewById(R.id.textView3);
viewHolder.cb = (CheckBox)view.findViewById(R.id.checkBox1);
viewHolder.cb.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Toast.makeText(mContext, "You clicked: "+pos, Toast.LENGTH_SHORT).show();
}
});
cb_list[position] = (CheckBox) view.findViewById(R.id.checkBox1);
view.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder)view.getTag();
}
viewHolder.tv1.setText(this.list1[position]);
viewHolder.tv2.setText(this.list2[position]);
viewHolder.tv3.setText(this.list3[position]);
return view;
}
public void checkIfNull(){
for(int i=0; i<cb_list.length; i++){
if (cb_list[i] == null)
Log.d("checkIfNull method", "cb_list["+i+"] is null");
else if (cb_list[i] != null)
Log.d("checkIfNull method", "cb_list["+i+"] is not null");
}
}
public static class ViewHolder{
public TextView tv1;
public TextView tv2;
public TextView tv3;
public CheckBox cb;
}
答案 0 :(得分:0)
为什么要选中null的复选框?您将检查复选框是“true”选中还是“false”未选中。