我目前正在开发 android 项目,我正在使用自定义列表视图,其中每个项目都有 TextViews。我通过使用设置可显示文本myTextView.setText("my text")
,然后使用设置 TextView 标记myTextView.setTag("my tag")
。
一旦它在列表视图中,我希望允许用户单击列表视图中的项目并检索 textview 文本以及标签。
我试过TextView textView = (TextView)getListAdapter().getItem(position);
了,``TextView textView = (TextView)getListView().getItem(position); 但它一直说它不能从String到TextView。
如何从文本视图中获取标签和文本。
感谢您的任何帮助,您可以提供。
更新 1 根据要求,这是我正在使用的 ListActivity,下面是项目单击事件的代码
@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();
}
下面的代码是设置列表适配器的代码
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);
}
下面的代码是 appArrayExtender 类
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;
}
}
下面是布局的 XML
<?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>