Reputation: 957
I need to get the value of a TextView which is in a certain position of my ListView. How to do it?
MyJava Code:
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
TextView txtTechCharacteristic = (TextView) findViewById(R.id.techCharacteristic);
TextView txtTechCharacteristicName = (TextView) findViewById(R.id.techCharacteristicName);
}
Here Are my TextView ->.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent" android:layout_height="wrap_content">
<include layout="@layout/simple_back_action_bar" />
</RelativeLayout>
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:id="@+id/techCharacteristic"
android:textSize="20sp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView android:id="@+id/techCharacteristicName"
android:textSize="15sp"
android:layout_width="wrap_content"
android:layout_height="fill_parent"/>
</LinearLayout>
The code to fill the listView is done within the asynchronous method.Here it is:
public class PopulateTechCharacteristicList extends
AsyncTask<Integer, String, Integer> {
ProgressDialog progress;
Context context;
public PopulateTechCharacteristicList(Context context) {
this.context = context;
}
protected void onPreExecute() {
progress = ProgressDialog.show(TechCharacteristicList.this,
getResources().getString(R.string.Wait), getResources()
.getString(R.string.LoadingOperations));
}
protected Integer doInBackground(Integer... paramss) {
ArrayList<TechCharacteristic> arrayTechChar = new ArrayList<TechCharacteristic>();
TechCharacteristicWSQueries techCharWSQueries = new TechCharacteristicWSQueries();
try {
arrayTechChar = techCharWSQueries
.selectTechCharacteristicByAsset(asset);
for (TechCharacteristic strAux : arrayTechChar) {
HashMap<String, String> temp = new HashMap<String, String>();
temp.put("cod", strAux.getTechCharacteristic() + " - "
+ strAux.getTechCharacteristicName());
temp.put("value",
"Valor: " + strAux.getTechCharacteristicValue());
list.add(temp);
}
} catch (QueryException e) {
e.printStackTrace();
return 0;
}
return 1;
}
protected void onPostExecute(Integer result) {
if (result == 1) {
SimpleAdapter adapter = new SimpleAdapter(
TechCharacteristicList.this, list,
R.layout.techcharacteristic_rows, new String[] { "cod",
"value" }, new int[] { R.id.techCharacteristic,
R.id.techCharacteristicName });
setListAdapter(adapter);
progress.dismiss();
}
}
}
Upvotes: 3
Views: 8042
Reputation: 24848
onListItemClick()
Get list (server response) object reference then get data at item click from position
index.
Upvotes: 1
Reputation: 1851
A listview recycles its view when you scroll down and re uses its views. I will not store the details of previous views in memory. Better you can keep you data in an arraylist or array and then in onListItemClick
method get the position and get the data from that position of arraylist/array
Upvotes: 0
Reputation: 410
onclick already passes the parent view as "v"
Just use
TextView txtTechCharacteristic = (TextView) v.findViewById(R.id.techCharacteristic);
String txt = txtTechCharacteristic.getText();
Upvotes: 6
Reputation: 11314
You will have to find for your text view through the ListViews selected items parentGroup view that is the argument View v
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if(v!=null)
{
TextView txtTechCharacteristic = (TextView) v.findViewById(R.id.techCharacteristic);
TextView txtTechCharacteristicName = (TextView) v.findViewById(R.id.techCharacteristicName);
}
}
Upvotes: 0
Reputation: 133560
Try the below
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
View view = (View)v.getParent();
TextView txtTechCharacteristic = (TextView) view.findViewById(R.id.techCharacteristic);
TextView txtTechCharacteristicName = (TextView) view.findViewById(R.id.techCharacteristicName);
}
Use getText()
to get the text from textview's.
Upvotes: 0