How to Refresh Custom Adapter List instantly in Android?

Shweta picture Shweta · Jul 3, 2013 · Viewed 21.5k times · Source

I am trying to get a list title, What I want is when I type in Edittext on home page then add into list after clicking on Ok button. Right now I don't know where to put my refresh method that I am calling from customAdapter class on Homepage Activity.

Please view my HomePage Activity:

public class Main_Activity extends Activity implements OnClickListener {
Button ok;
EditText addTasklist;
ListView list_tasklistname;
TodoTask_Database db;
CustomAdapter cAdapter;
List<Tasks> list = new ArrayList<Tasks>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
    ok = (Button)findViewById(R.id.add);
    ok.setOnClickListener(this);
    list_tasklistname = (ListView)findViewById(R.id.listview);
    db = new TodoTask_Database(getApplicationContext());
    list = db.getAllTaskList();
    CustomAdapter adapter = new CustomAdapter(Main_Activity.this, R.layout.tasklist_row, list);
    list_tasklistname.setAdapter(adapter);
    /*list_tasklistname.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View v, int position,
                long id) {
        }
    });*/
    db.close();
}
}
@Override
public void onClick(View v) {
    switch(v.getId()) {
    case R.id.add:
        addTasklist = (EditText)findViewById(R.id.addTasklist);
        if(addTasklist!=null) {
             String addtasktitle = addTasklist.getText().toString();
             db = new TodoTask_Database(getApplicationContext());
             db.addTaskList(addtasktitle);
        }
        break;
    }
}
}

Custom Adapter:

 public class CustomAdapter extends ArrayAdapter<Tasks> {

    private List<Tasks> dataitem;
    private Activity activity;

public CustomAdapter(Activity a, int textViewResourceId, List<Tasks> items) {
    super(a, textViewResourceId, items);
    this.dataitem = items;
    this.activity = a;
}

public static class ViewHolder{
    public TextView tasklistTitle;
    public TextView createdDate;
    public CheckBox completedflag;
}

   @Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    ViewHolder holder;
    if (v == null) {
        LayoutInflater vi =
            (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.tasklist_row, null);
        holder = new ViewHolder();
        holder.tasklistTitle = (TextView) v.findViewById(R.id.tasklistTitle);
        holder.createdDate = (TextView) v.findViewById(R.id.createdDate);
        holder.completedflag = (CheckBox) v.findViewById(R.id.completedflag);
        v.setTag(holder);
    }
    else
        holder=(ViewHolder)v.getTag();

    final Tasks custom = dataitem.get(position);
    if (custom != null) {
        holder.tasklistTitle.setText(custom.getTaskListTitle());
        holder.createdDate.setText(custom.getTaskListCreated());
        holder.completedflag.setText(custom.getTaskListCompletedFlag());
    }
    return v;
}
  // refresh Adapter Method calling in Homepage Activity

   public synchronized void refresAdapter(List<Tasks> dataitems) {   
    dataitem.clear();
    dataitem.addAll(dataitems);
    notifyDataSetChanged();
   }
}

Answer

sfdc-neta picture sfdc-neta · Jul 3, 2013

In onclick method you may call your refresh method refresAdapter(List<Tasks> dataitems) after db.addTaskList(addtasktitle); statement.

Or simply you may write adapter.notifyDataSetChanged() after this db.addTaskList(addtasktitle); statement