チェックボックス付きのカスタムリストビューから選択したアイテムを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();
}
}
}
少し早いですがお礼を