'The method add(String) in the type ArrayList<String> is not applicable for the arguments (List)

when i add the selected items from custom listview with checkbox to arraylist in android,there is an error showing in the line .

selectedItems.add(adapter.getItem(position));

The method add(String) in the type ArrayList is not applicable for the arguments (List)

This is my code

  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();
        }

}

}

Thank in advance



Solution 1:[1]

I assume that the error is at this line:

selectedItems.add( adapter.getItem(position));

You've declared the adapter to manage an array of List objects. You declared selectedItems to be an ArrayList<String>. You can't add an object of type List to an ArrayList that is expecting String elements. You probably want something like:

selectedItems.add((String) adapter.getItem(position).get(1));

Note that you would be much better off declaring an aggregation class:

class Data {
    int resourceId;
    String name;
}

Then the compiler could enforce type safety and you wouldn't have to cast when accessing elements of the list. You would also eliminate the autoboxing of the resource IDs.

Solution 2:[2]

your error occurs in this line

selectedItems.add( adapter.getItem(position));

Your item is your own List-Item and so you cant add it to a ArrayList<String>.

Cheers

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ted Hopp
Solution 2 ZeusNet