How to change the contents of listview on item click?

11.5k Views Asked by At

I have a listview populated using a String array as follows:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(),android.R.layout.simple_list_item_1,res);
getListView().setAdapter(adapter);

I want to change the contents of listview using OnItemClickListener.

3

There are 3 best solutions below

0
On BEST ANSWER

Depending on how you want to make changes, do you want the user to make changes by giving input or you want to make changes through code directly. The following setOnItemClickListener set the new values to the variable. For taking input from the user, you need to populate it in the fragment or new activity, take the input from the user and commit those changes, refresh the list and show the updated data.

Model class:

public class Item {

    private String name = "";

    public Item() {
    }

    public Item(String name){
        this.name = name;
    }

    public String getItemName() {
        return name;
    }

    public void setItemName(String name) {
        this.name = name;
    }

}

Adapter class:

public class ItemAdapter extends ArrayAdapter<Item> {

    private List<Item> itemList = new ArrayList<Item>();
    Context context;

    @Override
    public void add(Item object) {
        itemList.add(object);
        super.add(object);
    }

    public ItemAdapter(List<Item> rList, Context context) {
        super(context, android.R.layout.simple_list_item_1);
        this.context = context;
        this.itemList = rList;
    }

    private class ViewHolder {
        TextView txtItemName;
    }

    public int getCount() {
        return this.itemList.size();
    }

    public Item getItem(int index) {
        return this.itemList.get(index);
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = new ViewHolder();

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item, parent, false);

            holder.txtItemName = (TextView) convertView.findViewById(R.id.txtItemName);

            convertView.setTag(holder);
        } else

            holder = (ViewHolder) convertView.getTag();

        Item item = getItem(position);

        holder.txtItemName.setText(item.getItemName());

        return convertView;
    }

}

Activity class:

public class ItemActivity extends Activity {

    private ListView listView;
    private ItemAdapter itemListAdapter;
    private List<Item> itemList = new ArrayList<Item>();

    @Override
    public void onCreate(Bundle savedInstanceState) {

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.list);

        listView = (ListView) findViewById(R.id.listView);

        itemListAdapter = new ItemAdapter(itemList, this);
        listView.setAdapter(itemListAdapter);
        listView.setFastScrollEnabled(true);

        ////////////////////////////////////////////////
        // add some items
        itemList.add(new Item("Charlie"));
        itemList.add(new Item("Jenny"));

        //add new items and changes to adapter
        itemListAdapter.notifyDataSetChanged();

        ////////////////////////////////////////////////

        listView.setOnItemClickListener(new OnItemClickListener(){
            @Override

            public void onItemClick(AdapterView<?> parent, View view, int position, long id){

                String beforeName = itemListAdapter.getItem(position).getItemName().toString();

                String changedName = "Thomas";
                itemListAdapter.getItem(position).setItemName(changedName);

            }

        });
    }
}
0
On

If you want to populate listview with different array then make a set get method in adapter view and set the array using set method in adapter then call adapter.notifyDataSetChanged() method. You would have a reference of adapter in onItemClickListenr() which you can use for calling and setting array.

0
On

Really nice piece of work. The onItemClickListener didn't work for me (android 8) until I added

itemListAdapter.notifyDataSetChanged();

after the change.