当我将自定义列表视图中的所选项目添加到android中的arraylist复选框时,该行显示错误。
selectedItems.add(adapter.getItem(position));
ArrayList类型中的方法add(String)不适用于参数(List)
这是我的代码
public class HomePage extends Activity {
private ListView listView1;
ListAdapter adapter;
@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 , "DCIM"),
new List(R.drawable.ic_pic, "Videos"),
new List(R.drawable.ic_pic, "Music"),
new List(R.drawable.ic_pic, "Mobile Uploads"),
new List(R.drawable.ic_pic, "Personal Folder")
};
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);
}
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(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) {
//Log.i("clicked", ""+holder.checkbox.isChecked());
checkvalue=holder.checkbox.isChecked();
if(checkvalue=holder.checkbox.isChecked()==true){
startNewActivity();
}
}
});
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;
}
private void startNewActivity() {
Button btn= (Button)findViewById(R.id.add);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SparseBooleanArray checked = listView1.getCheckedItemPositions();
final ArrayList<String> selectedItems = new ArrayList<String>();
for (int i = 0; i < checked.size(); i++) {
// Item position in adapter
int position = checked.keyAt(i);
if (checked.valueAt(i))
**selectedItems.add(adapter.getItem(position));**
}
String[] outputStrArr = new String[selectedItems.size()];
for (int i = 0; i < selectedItems.size(); i++) {
outputStrArr[i] = selectedItems.get(i);
}
Intent intent = new Intent(getApplicationContext(),
CheckedValues.class);
Bundle b = new Bundle();
b.putStringArray("selectedItems", outputStrArr);
intent.putExtras(b);
startActivity(intent);
}
});
Toast.makeText(getApplicationContext(), "clicked View "+checkvalue, Toast.LENGTH_SHORT).show();
}
}
}
提前感谢
答案 0 :(得分:0)
我认为错误就在这一行:
selectedItems.add( adapter.getItem(position));
您已声明适配器管理List
个对象的数组。您声明selectedItems
为ArrayList<String>
。您无法将List
类型的对象添加到期望ArrayList
元素的String
。你可能想要这样的东西:
selectedItems.add((String) adapter.getItem(position).get(1));
请注意,声明聚合类会更好:
class Data {
int resourceId;
String name;
}
然后编译器可以强制执行类型安全,并且在访问列表元素时不必进行强制转换。您还可以消除资源ID的自动装箱。
答案 1 :(得分:0)
您的错误发生在此行
selectedItems.add( adapter.getItem(position));
您的商品是您自己的列表商品,因此您无法将其添加到ArrayList<String>
。
干杯