Possible Duplicate:
Can't add item to custom arrayadapter




仍然无法将其填充到我的列表视图中。我不确定为什么它根本不这样做。任何帮助将不胜感激。谢谢

编辑

适配器

public class TTAdapterView extends ArrayAdapter<String> {

private final Activity context1;
private  ArrayList<String> items;

public TTAdapterView(Activity context, ArrayList<String> items){
    super(context,R.layout.row,items);
    this.context1 = context;
    this.items = items;
}
static class ViewHolder{
    public TextView task;
    public Chronometer time;
    public CheckBox complete;

}

@Override
public View getView(int position, View convertView, ViewGroup parent){

    ViewHolder holder;
    View rowView = convertView;

    if(rowView==null){
        LayoutInflater vi = context1.getLayoutInflater();
        rowView = vi.inflate(R.layout.row, null, true);
        holder = new ViewHolder();
        holder.task = (TextView) rowView.findViewById(R.id.textView1);
        holder.time = (Chronometer) rowView.findViewById(R.id.chronometer1);
        holder.complete = (CheckBox) rowView.findViewById(R.id.checkBox1);
        String taskText = items.get(position);
        holder.task.setText(taskText);
        rowView.setTag(holder);
    }
    else{
        holder = (ViewHolder) rowView.getTag();
    }

    return rowView;
}


主要活动

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button addButton;
    addButton=(Button)findViewById(R.id.button1);
    ListView myListView= (ListView)findViewById(R.id.listView1);
    final EditText myEditText= (EditText)findViewById(R.id.editText1) ;
    //this.setListAdapter(new ArrayAdapter<String>(this,R.layout.row));


    final ArrayList<String> items = new ArrayList<String>();
    items.add(0, "cheese");

    //this.setListAdapter(new TTAdapterView(this,items));
    final TTAdapterView aa = new TTAdapterView(this,items);
    //final ArrayAdapter<String> aa = new ArrayAdapter<String>(
   // aa = new ArrayAdapter<String>(this, 0);
    //myListView.setAdapter(aa);


    addButton.setOnClickListener(new OnClickListener(){
        public void onClick(View v){

            aa.add(myEditText.getText().toString());
            //taskitems.add(count, myEditText.getText().toString());
            aa.notifyDataSetChanged();
            myEditText.setText("");
            myEditText.requestFocus();
                                        }
    });
}


}

最佳答案

我相信您缺少将适配器分配给ListView的行。您需要myListView.setAdapter(aa);,您实际上已将其保存在其中,但已将其注释掉了! ;)

希望能有所帮助

10-05 22:19