I am currently working on android project where I am using a custom list view which TextViews for each item. I am settings the displayable text by using myTextView.setText("my text") and then setting the TextView tag by using myTextView.setTag("my tag").
Once its in the list view I then want to allow the user to click on the item within the list view and retrieve the textview text as well as the tag.
I’ve tried TextView textView = (TextView)getListAdapter().getItem(position);
and “TextView textView = (TextView)getListView().getItem(position); but it keeps saying that it can’t can’t from String to TextView.
How can I get the tag and the text from the text view.
Thanks for any help you can provide.
UPDATE 1
As requested this is a ListActivity that I am using and below is the code for the item click event
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//get selected items
//String selectedValue = (String) getListAdapter().getItem(position);
//TextView textView = (TextView)appArrayAdapter.getItem(position);
TextView textView = (TextView)getListView().getChildAt(position);
String selectedValue = textView.getText().toString();
String selectedPackage = textView.getTag().toString();
Intent intent = new Intent();
intent.putExtra("appName", selectedValue);
intent.putExtra("packageName", selectedPackage );
setResult(0, intent);
finish();
}
And the below code is the code that sets the list adapter
final ArrayList<ResolveInfo> list = (ArrayList<ResolveInfo>)pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
ArrayList<String> arrayList = new ArrayList<String>();
imageList = new ArrayList<Drawable>();
packageName = new ArrayList<String>();
for(int i = 0; i != list.size(); i++)
{
String text = list.get(i).activityInfo.applicationInfo.loadLabel(pm).toString();
String appPackage = list.get(i).activityInfo.packageName;
arrayList.add(text);
Drawable imageId = list.get(i).activityInfo.applicationInfo.loadIcon(pm);
packageName.add(appPackage);
imageList.add(imageId);
}
appArrayAdapter = new AppArrayAdapter(this, arrayList);
//setListAdapter(new AppArrayAdapter(this, arrayList));
setListAdapter(appArrayAdapter);
}
And the below code is the appArrayExtender class
public class AppArrayAdapter extends ArrayAdapter<String>
{
private final Context context;
private final ArrayList<String> arrayList;
//private final ArrayList<Drawable> imageList;
public AppArrayAdapter(Context context, ArrayList<String> arrayList/*, ArrayList<Drawable> imageList*/)
{
super(context, R.layout.select_apps, arrayList);
this.context = context;
this.arrayList = arrayList;
//this.imageList = imageList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.select_apps, parent, false);
TextView textView = (TextView)rowView.findViewById(R.id.label);
ImageView imageView = (ImageView)rowView.findViewById(R.id.logo);
textView.setText(arrayList.get(position));
textView.setTag(packageName.get(position));
imageView.setImageDrawable(imageList.get(position));
//imageView.setImageResource(R.drawable.icon);
return rowView;
}
}
And below is the XML for the layout
<?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="wrap_content"
android:padding="5dp" >
<ImageView
android:id="@+id/logo"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginLeft="5px"
android:layout_marginRight="20px"
android:layout_marginTop="5px" >
</ImageView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label" >
</TextView>
</LinearLayout>
If you want the text from the TextView, use the information passed into the
onClickrather than all that extra code. The framework passes in the view that was clicked on (the View v parameter). If it is a single TextView, you can dov.getText().toString(), if it is a more complex layout, you can usev.findViewById(R.id.TextView1)to get the proper TextView and the usegetText().toString()on it: