我目前正在开发android项目,我正在使用自定义列表视图,每个项目都有TextViews。我使用myTextView.setText("my text")
设置可显示文本,然后使用myTextView.setTag("my tag")
设置TextView标记。
在列表视图中,我希望允许用户单击列表视图中的项目并检索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>
答案 0 :(得分:2)
如果您希望文本来自 TextView,请使用传递到onClick
的信息,而不是所有额外的代码。框架在单击的视图中传递(View v参数)。如果它是单个TextView,您可以执行v.getText().toString()
,如果它是更复杂的布局,您可以使用v.findViewById(R.id.TextView1)
来获取正确的TextView并使用getText().toString()
:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
TextView textView = (TextView) v.findViewById(R.id.yourRowLayoutWidget); // get the widget contained in the layout
String selectedValue = textView.getText().toString(); // get the value of the widget into a string
// do what you will with the string
}
答案 1 :(得分:0)
当列表视图滚动时,刚刚消失的视图项作为参数convertView传递给适配器方法的getView,所以你设置Tag f.e.在7项并且它被转换为第1个原因7th不再是wisivle并且第1项将有错误标记;
我建议您扩展BaseAdapter并使用您需要的所有数据存储一些对象。
看起来像这样
public class DialogsAdapter extends BaseAdapter {
private final LayoutInflater mInflater;
private List<YourObjectToStoreData> mData ;
public DialogsAdapter(Context context, List<YourObjectToStoreData> data
) {
this.mInflater = LayoutInflater.from(context);
mData = data;
}
...
另请查看Holder Pattern它可能有用