ListActivity的适配器

时间:2011-04-29 16:00:38

标签: android

我的ListActivity有问题,希望有人可以帮助我。 我创建了Adapter

public class MyListAdapter extends BaseAdapter {

private ArrayList<String> lista= new ArrayList<String>();
private LayoutInflater myInflater;

public MyListAdapter(Context context){
    myInflater= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public void addItem(final String item){
    lista.add(item);
    notifyDataSetChanged();
}

public int getCount() {
    return lista.size();
}

public Object getItem(int position) {
    return lista.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder= null;
    if (convertView == null){
        convertView= myInflater.inflate(R.layout.index, null);
        holder= new ViewHolder();
        holder.textView= (TextView)convertView.findViewById(R.id.empty);
        holder.checkBox= (CheckBox) convertView.findViewById(R.id.Check);
        convertView.setTag(holder);
    }else{
        holder=(ViewHolder)convertView.getTag();
    }
    holder.textView.setText(lista.get(position));
    holder.textView.setOnClickListener(new OnClickListener() {

        public void onClick(View view) {
            Intent myIntent= new Intent (view.getContext(),Chronometer.class);
            myIntent.putExtra(name, value)

        }
    });
    return convertView;
}

}

但是现在我在列表元素中遇到了onClick的问题。我有两个问题 - 当我onClick我希望传递一个对象时...但是在这里我没有只有textViewcheckbox的对象 - 第二,当我放startActivity时,它不是识别器

我这样称呼我的适配器:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        [...]
        MyListAdapter myAdapter= new MyListAdapter(getBaseContext());

          for (Meansurement elem : procesos) {
              myAdapter.addItem(elem.getName());
           }
        }
        setListAdapter(myAdapter);
   }

1 个答案:

答案 0 :(得分:0)

在列表中设置onItemClickListener,而不是在单个文本视图上设置onclick:

lista.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> list, View view, int position, long id) {
        Object item = list.getItemAtPosition(position);
        Intent i = new Intent(YourActivity.this, OtherActivity.class);
        i.putExtra(ApplicationConstants.EXTRA_ID, item.id);
        startActivityForResult(i, INTENT_EDIT_ACCOUNT);
      }
    });