Need to access the text of a TextView child of a ListView
I have a ListView that contains a list of TextViews, and in my onItemClick() method, I need to get at the string value of the TextView that was clicked. How do I do this?
Here is my onItemClick function:
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent startNewActivity=new Intent(getBaseContext(),DetailCurrencyActivity.class);
startActivity(startNewActivity);
}
Update 1: This is the custom list adapter where the listview gets populated:
//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].开发者_如何学PythongetString("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;
}
}
The getView
method gives you a number of ways of a accessing the data. Presumably you used some sort of array adapter to give you data to the ListView
in the first place. The first int, position
, gives you the index into your original list. See the [getView docs][1] for details. That is probably the easiest way to get the information.
Otherwise, you could try to find the item with the View
you are provided.
[1]: http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html#onItemClick(android.widget.AdapterView, android.view.View, int, long)
TextView tv = (TextView) arg1;
String myString = tv.getText().toString();
arg1 ( as you have it there at least) is a reference to the root View of the row that was clicked.
精彩评论