如何在Android中立即刷新自定义适配器列表?

时间:2013-07-03 06:47:35

标签: android android-listview android-arrayadapter custom-adapter

我正在尝试获取列表标题,当我在主页上键入Edittext然后在单击“确定”按钮后添加到列表中时,我想要的是什么。现在我不知道从主页Activity上的customAdapter类调用我的刷新方法的位置。

请查看我的主页活动:

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;
    }
}
}

自定义适配器:

 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();
   }
}

2 个答案:

答案 0 :(得分:8)

onclick方法中,您可以在refresAdapter(List<Tasks> dataitems)语句后调用刷新方法db.addTaskList(addtasktitle);

或者只是你可以在adapter.notifyDataSetChanged()之后写db.addTaskList(addtasktitle);声明

答案 1 :(得分:-1)

调用此方法 adapter.notifyDatasetChanged()

如果您想为导航抽屉执行此操作,请在draweropen方法

中执行此操作