user1726619
user1726619

Reputation: 941

ListView with custom adapter not getting updated

I want to update the list view whenever any item is updated or deleted.I have tried many options,but none of them is working.This is the code of the list view:

public class HelloBubblesActivity extends Activity {
    private com.warting.bubbles.DiscussArrayAdapter adapter;
    private ListView lv;
    private LoremIpsum ipsum;
    private EditText editText1;
    private static Random random;
    Runnable m_handlerTask;
    TextView tv ;
    LinearLayout ll;
    Handler m_handler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discuss);

        lv = (ListView) findViewById(R.id.listView1);

        adapter = new DiscussArrayAdapter(getApplicationContext(), R.layout.listitem_discuss);

        lv.setAdapter(adapter);

        ll = (LinearLayout)findViewById(R.id.wrapper);
        m_handler = new Handler();
        addItems();
        ReduceTimeIteration();

    }

     public void ReduceTimeIteration()
     {
            adapter.clear();
            ((BaseAdapter) lv.getAdapter()).notifyDataSetChanged();
            adapter.notifyDataSetChanged();
            adapter.notifyDataSetInvalidated();
            lv.invalidate();
            lv.requestLayout();
            Log.d("Adapter: ", "Adapter is changed");
     }
    private String addItems() {
        String a = "Hello bubbles";
        adapter.add(new OneComment(true,a));
        return a;
    }
}

This is the code of the custom adapter:

public class DiscussArrayAdapter extends ArrayAdapter<OneComment> {

    private TextView countryName;
    private List<OneComment> countries = new ArrayList<OneComment>();
    private LinearLayout wrapper;
    static View row ;

    @Override
    public void add(OneComment object) {
        countries.add(object);
        super.add(object);
    }

    public DiscussArrayAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

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

    public OneComment getItem(int index) {
        return this.countries.get(index);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        row = convertView;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.listitem_discuss, parent, false);
        }

        wrapper = (LinearLayout) row.findViewById(R.id.wrapper);

        OneComment coment = getItem(position);

        countryName = (TextView) row.findViewById(R.id.comment);

        countryName.setText(coment.comment);

        countryName.setBackgroundResource(coment.left ? R.drawable.bubble_yellow : R.drawable.bubble_green);
        wrapper.setGravity(coment.left ? Gravity.LEFT : Gravity.RIGHT);

        return row;
    }

    public Bitmap decodeToBitmap(byte[] decodedByte) {
        return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
    }

}

In the list view, i want to remove all the views using adapter.clear() and then want to update the list view.But even after calling adapter.clear() , the list view remains the same.Please help me. Thanks in advance.

Upvotes: 0

Views: 1696

Answers (1)

Carnal
Carnal

Reputation: 22064

I suggest you create you adapter in the following way:

// declared in activity
private List<OneComment> countries = new ArrayList<OneComment>(); 

// in onCreate()
adapter = new DiscussArrayAdapter(getApplicationContext(), R.layout.listitem_discuss, countries);

And then call countries.clear(); adapter.notifyDataSetChanged(); to clear the list view. Because you are basically using now your "countries" (list) to tell which size the adapter has, and which item to get upon that list.

Upvotes: 3

Related Questions