无法将项添加到自定义arrayadapter

时间:2011-04-19 04:48:59

标签: android

  

可能重复:
  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();
                                        }
    });
}

}

1 个答案:

答案 0 :(得分:2)

我相信你错过了将适配器分配给ListView的行。你需要myListView.setAdapter(aa);你实际拥有它,但它被注释掉了! ;)

希望有所帮助