嗨,我正在使用带有复选框的自定义列表视图编写程序。我的意图是在单击按钮(添加)时启动一个新活动,并且新活动显示从列表视图中选择的值。当我尝试添加值时出现错误
发生错误的行是
selectedItems.add(adapter.getItem(位置));
如果我对此行发表评论,那么我可以选中该框,但是当我单击“添加”按钮时它会显示错误。
这是我的完整代码。
我的主要活动
主页.java
public class HomePage extends Activity {
private ListView listView1;
ListAdapter adapter;
Button btn;
SparseBooleanArray checkedValue;
ArrayList<List> selectedItems;
String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
List device_data[] = new List[]
{
new List(R.drawable.ic_pic , "Actvity1"),
new List(R.drawable.ic_pic, "Actvity2"),
new List(R.drawable.ic_pic, "Actvity3"),
new List(R.drawable.ic_pic, "Actvity4"),
new List(R.drawable.ic_pic, "Actvity5")
};
ListAdapter adapter = new ListAdapter(this, R.layout.list_viewrow, device_data);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.listview_header, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
Log.i("check box status", ""+ListAdapter.ListHolder.class);
btn= (Button)findViewById(R.id.add);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String[] outputStrArr = new String[selectedItems.size()];
checkedValue= listView1.getCheckedItemPositions();
for (int i = 0; i < selectedItems.size(); i++) {
outputStrArr[i] = selectedItems.get(i).toString();
}
Intent intent = new Intent(getApplicationContext(),
CheckedValues.class);
Bundle b = new Bundle();
b.putStringArray("selectedItems", outputStrArr);
// Add the bundle to the intent.
intent.putExtras(b);
startActivity(intent);
}
});
}
private class ListAdapter extends ArrayAdapter<List> {
Context context;
int layoutResourceId;
boolean checkvalue;
List data[] = null;
public ListAdapter(Context context, int layoutResourceId, List[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
final ListHolder holder;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ListHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
holder.checkbox=(CheckBox) row.findViewById(R.id.check);
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
selectedItems.add(adapter.getItem(position));
}
});
row.setTag(holder);
}
else
{
holder = (ListHolder)row.getTag();
}
List list = data[position];
holder.txtTitle.setText(list.title);
holder.imgIcon.setImageResource(list.icon);
return row;
}
class ListHolder
{
ImageView imgIcon;
TextView txtTitle;
CheckBox checkbox;
}
}
}
和我的第二个活动
CheckedValues.java
public class CheckedValues extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checked_values);
Bundle b = getIntent().getExtras();
String[] resultArr = b.getStringArray("selectedItems");
ListView lv = (ListView) findViewById(R.id.outputList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, resultArr);
lv.setAdapter(adapter);
}
}
提前致谢..