Search code examples
androidlistviewandroid-intentnotifydatasetchanged

Unable to refresh the changed data in listview using notifyDataSetChanged


I have a main activity which has a listview which loads first and from this main activity I am launching a new activity where I'm trying to get the edittext value to my main activity by passing intent. Everything is working fine but when I add the editext value to the listview its adding but when I go to the second activity and add again and comes back to the main activity the previous added data is no more seen and the newly added value is updating the listview.

Can anyone help me out with this?

This is my Adapter:

public class AdapterListViewData extends BaseAdapter{

    private LayoutInflater mInflater;
    private Context context; 
    private ArrayList<DataShow> listData = new ArrayList<DataShow>(); 

    public AdapterListViewData(Context context,ArrayList<DataShow> listData) {

        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        this.listData = listData;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return listData.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        HolderListAdapter holderListAdapter; 

        if(convertView == null)
        {

            convertView = mInflater.inflate(R.layout.adapter_listview, null);

            holderListAdapter = new HolderListAdapter();

            holderListAdapter.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
            holderListAdapter.txtDetail = (TextView) convertView.findViewById(R.id.txtDetail);
            holderListAdapter.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);

            convertView.setTag(holderListAdapter);
        }else{
            holderListAdapter = (HolderListAdapter) convertView.getTag();
        }

        holderListAdapter.txtTitle.setText(listData.get(position).getTitle());
        holderListAdapter.txtDetail.setText(listData.get(position).getDetail());

        holderListAdapter.checkBox.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context,"CheckBox "+ position +" check!!",Toast.LENGTH_SHORT).show();
            }
        });

        convertView.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context,"List "+ position +" click!!",Toast.LENGTH_SHORT).show();
            }
        });

        return convertView;
    }
}

MainActivity:

public class CustomListViewActivity extends Activity {

    private AdapterListViewData adapterListViewData; 
    private ArrayList<DataShow> listData = new ArrayList<DataShow>();
    private ListView listViewData;
    String AddedTask ;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent intent = getIntent();



        listViewData = (ListView)findViewById(R.id.listViewData);
        if (intent.hasExtra("NewTask")) {
             AddedTask = this.getIntent().getExtras().getString("NewTask");
            listData.add(new DataShow(AddedTask,""));

       }
        adapterListViewData = new AdapterListViewData(getBaseContext(),listData);
        listViewData.setAdapter(adapterListViewData);
        adapterListViewData.notifyDataSetChanged();
     }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar actions click
        switch (item.getItemId()) {
        case R.id.action_settings:
            return true;
        case R.id.action_add_task:
            Intent i = new Intent(CustomListViewActivity.this, AddTask.class);
            startActivity(i);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }


}

DataShow:

public class DataShow {
    private String title;
    private String detail;

    public DataShow(String title,String detail) {
        // TODO Auto-generated constructor stub
        this.title = title;
        this.detail = detail;
    }

    public String getTitle(){
        return this.title;
    }

    public String getDetail(){
        return this.detail;
    }
}

Holder:

public class HolderListAdapter {
    TextView txtTitle;
    TextView txtDetail;
    CheckBox checkBox;
}

Second Activity:

public class AddTask extends Activity {
Button addtask;
      @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.add_task);

            // get action bar   
            ActionBar actionBar = getActionBar();

            // Enabling Up / Back navigation
            actionBar.setDisplayHomeAsUpEnabled(true);

            addtask = (Button) findViewById(R.id.btnaddlist);
            findViewById(R.id.btnaddlist).setOnClickListener(
                      new View.OnClickListener() {
                          public void onClick(View arg0) {
                              EditText edit = (EditText) findViewById(R.id.tskname);
                              Intent i = new Intent(AddTask.this,
                                      CustomListViewActivity.class);
                              //Bundle bundle = new Bundle();

                              String TaskName = edit.getText().toString();
                              //bundle.putString("NewTask", TaskName);
                              i.putExtra("NewTask", TaskName);
                              //i.putExtras(bundle);
                              startActivity(i);
                          }
                      });

          } 

        }

Solution

  • Your old data got removed becaus it is not transmitted to the new activity that you start after creating the new item.

    You should use startActivityForResult to create new task and call Add the new task to the list adapterListViewData in onActivityResult and call notifyDataSetChanged in it.