user2456977
user2456977

Reputation: 3964

ListView text changing but not adapter data

I added 3 items to my ListView. Each item says "Test". When I click on an item, I clear that item's text. However, when I check the variable in the "watches" log, the item is still named "Test" even though I removed the text.

watches log:

adapter2.getItem(1) = {java.lang.String@830045070536}"Test"
list.getItemAtPosition(1) = {java.lang.String@830045070536}"Test"

I want the ListView to get updated and make the item blank after being clicked

for example the watches log should look like this:

adapter2.getItem(1) = {java.lang.String@830045070536}""
list.getItemAtPosition(1) = {java.lang.String@830045070536}""

Can some one please tell me why my this is happening and offer a solution?

Edited Code

public class ListView2 extends Fragment {

    ListView list;
    ArrayAdapter<String> adapter2;
    List<String> array_list;

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.listview2, container, false);

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        array_list = new ArrayList<String>();

        array_list.add("Test");
        array_list.add("Test");
        array_list.add("Test");

        //Build Adapter
        adapter2 = new ArrayAdapter<String>(getActivity(), R.layout.items, array_list);

        //Configure the list view
        list = (ListView) getActivity().findViewById(R.id.listview2);
        list.setAdapter(adapter2);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

                TextView txt = (TextView)view.findViewById(R.id.txt);
                if(txt.getText().toString().equals("Test")) {
                    txt.setText("");
                }else{
                    txt.setText("Test");
                }
                adapter2.notifyDataSetChanged();
            }
        });

    }
}

Upvotes: 0

Views: 1394

Answers (2)

Arkar Aung
Arkar Aung

Reputation: 3584

Trying to update TextView directly is not the good way. You can do it by using remove() and insert() methods to update your ArrayAdapter. For a reference, please check it here ,

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

            if(adapter2.getItem(position).equals("Test")) {
                adapter2.remove("Test");
                adapter2.insert("", position);
            }else{
                adapter2.remove("");
                adapter2.insert("Test", position);
            }
            adapter2.notifyDataSetChanged();
        }
    });

UPDATE

This is another way to update ArrayAdapter without removing and inserting entry to same position as you mention in comment.

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
        if(arrayList.get(position).equals("Test")) {
            arrayList.set(position, "");
        }else{
            arrayList.set(position, "Test");
        }
        adapter2.notifyDataSetChanged();
    }
}

Hope it will be useful for you.

Upvotes: 2

varun bhardwaj
varun bhardwaj

Reputation: 1522

On the item click listner you must be getting the view of the row, use that row object and find the view in that row i.e a textview in your case. Set the text of that view to be blank and also change the data set item on that position so that view and data are in sync. In this way there is no need to update the whole list and saves the time.

If any of the above point is not clear please mention.

Upvotes: 0

Related Questions