3

我正在尝试获取列表标题,我想要的是当我在主页上输入 Edittext 然后单击确定按钮后添加到列表中。现在我不知道将我从主页活动上的 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();
   }
}
4

2 回答 2

9

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

或者干脆你可以adapter.notifyDataSetChanged()在这之后写db.addTaskList(addtasktitle);陈述

于 2013-07-03T07:02:06.150 回答
-1

调用这个方法 adapter.notifyDatasetChanged()

如果你想为导航抽屉做,在draweropen方法中做

于 2016-06-14T20:39:04.773 回答