当前位置 : 主页 > 手机开发 > android >

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

来源:互联网 收集:自由互联 发布时间:2021-06-11
我想获得一个列表标题,当我在主页上键入Edittext然后在单击“确定”按钮后添加到列表中时,我想要的是什么.现在我不知道从主页Activity上的customAdapter类调用我的刷新方法的位置. 请查看
我想获得一个列表标题,当我在主页上键入Edittext然后在单击“确定”按钮后添加到列表中时,我想要的是什么.现在我不知道从主页Activity上的customAdapter类调用我的刷新方法的位置.

请查看我的HomePage活动:

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();
   }
}
在onclick方法中,您可以在db.addTaskList(addtasktitle)之后调用您的刷新方法refresAdapter(List< Tasks> dataitems);声明.

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

网友评论