0

私は現在、アイテムごとに TextViews を使用するカスタム リスト ビューを使用している Android プロジェクトに取り組んでいます。を使用して表示可能なテキストをmyTextView.setText("my text")設定し、次に を使用して TextView タグを設定していmyTextView.setTag("my tag")ます。

リストビューに入ったら、ユーザーがリストビュー内のアイテムをクリックして、テキストビューのテキストとタグを取得できるようにします。

私は試してみましたTextView textView = (TextView)getListAdapter().getItem(position); ``TextView textView = (TextView)getListView().getItem(position); しかし、StringからTextViewにはできないと言い続けています。

テキスト ビューからタグとテキストを取得するにはどうすればよいですか。

ご協力いただきありがとうございます。

UPDATE 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>
4

2 に答える 2

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
} 
于 2012-06-18T02:46:42.827 に答える
0

リスト ビューがスクロールされると、消えたばかりのビュー アイテムが引数 convertView としてアダプター メソッドの getView に渡されるため、7 アイテムに Tag fe を設定し、それが最初の原因 7 に変換されるよりも、7 番目はもはや賢明ではなく、最初のアイテムのタグが間違っています。

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も参考にしてください。

于 2012-06-17T23:52:43.930 に答える