2011-02-11 60 views
0

我有一個包含TextView列表的ListView,並且在我的onItemClick()方法中,我需要獲取被單擊的TextView的字符串值。我該怎麼做呢?需要訪問ListView的TextView子文本

這裏是我的onItemClick功能:

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
        long arg3) { 
       Intent startNewActivity=new Intent(getBaseContext(),DetailCurrencyActivity.class); 
       startActivity(startNewActivity); 



      } 

更新1: 這是自定義列表適配器上的列表視圖被填充:

//custom list adapter 
    public class listRecordAdapter extends BaseAdapter{ 
     private JSONObject[] listOf_Records=null; 
     private Context context; 
     private TextView ticker,value,value2,changeval; 

     public listRecordAdapter(Context context,JSONObject[] listOf_Records){ 
      Log.i("Concerts", "artistEventAdapter: constructor"); 
      this.listOf_Records=listOf_Records; 
      this.context=context; 
     } 

     @Override 
     public int getCount() {   
      return listOf_Records.length; 
     } 

     @Override 
     public Object getItem(int position) {   
      return listOf_Records[position]; 
     } 

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

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      Log.i("CurrencyActivity", "artistListAdapter: getView"); 
      if(convertView==null){ 
       convertView=LayoutInflater.from(context).inflate(R.layout.forex_listview_item, null); 
      } 
      try{ 

       ticker=(TextView)convertView.findViewById(R.id.ticker); 
       ticker.setText(this.listOf_Records[position].getString("ticker")); 
       Log.i("CurrentActivity", "listRecordAdapter: getView ticker->"+ticker.getText()); 

       value=(TextView)convertView.findViewById(R.id.value);   
       value.setText(this.listOf_Records[position].getString("value")); 
       Log.i("CurrentActivity", "listRecordAdapter: getView value->"+value.getText()); 

       value2=(TextView)convertView.findViewById(R.id.value2);   
       value2.setText(this.listOf_Records[position].getString("value2")); 
       Log.i("CurrentActivity", "listRecordAdapter: getView value2->"+value2.getText()); 

       changeval=(TextView)convertView.findViewById(R.id.changeval);   
       changeval.setText(this.listOf_Records[position].getString("changeval")); 
       Log.i("CurrentActivity", "listRecordAdapter: getView changeval->"+changeval.getText()); 
      }catch(Exception ex){ 
       Log.i("CurrencyActivity", "listRecordAdapter: getView exiting with exception message->"+ex.getMessage()); 
      } 
      return convertView; 
     } 

    } 
+0

安置自己的列表實現。有很多方法可以完成它。 – user432209 2011-02-12 00:00:52

回答

0

getView方法爲您提供了訪問數據的多種方法。假設你使用某種陣列適配器,首先爲你提供數據給ListView。第一個int position爲您提供了原始列表中的索引。有關詳細信息,請參見[getView文檔] [1]。這可能是獲取信息的最簡單方法。

否則,您可以嘗試找到與您提供的View項目。

[1]:http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html#onItemClick(android.widget.AdapterView,android.view.View,整型,長)

0
TextView tv = (TextView) arg1; 
String myString = tv.getText().toString(); 

ARG1(因爲你有它存在至少)是對被點擊的行的根視圖的引用。

+0

請記住,只有當你的`ListView`項目只包含一個`TextView`時,如果你的每個項目有更復雜的佈局,那麼你將得到一個`ClassCastException`。 – 2011-02-12 00:10:52

+0

他說他有。 – Jems 2011-02-12 00:11:54