2011-04-29 68 views
0

我有一個問題,我的ListActivity,我希望有人能幫助我。 我創建了我AdapterListActivity的適配器

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); 
    } 

回答

0

設置列表上的onItemClickListener,而不是對個人textviews設置一個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); 
     } 
    }); 
+0

感謝,但我不能做到這一點lista.setOnItemClickListener,它不是識別器 – user721399 2011-04-29 16:40:26

+0

哎呀,我沒有看到你使用的是ListActivity,那麼你只需要重寫onListItemClick(...)。例如http://www.tutorialforandroid.com/2009/01/clicking-items-in-listactivity.html – dmon 2011-04-29 18:16:54