1

setOnItemClickListenerメソッド内のListItemレイアウトで定義されたTextViewのgetText()値を取得しようとしています。

基本的に、使用するリストビュー/アダプターに関係なく、選択した連絡先を取得できるようにデータ(Id、LookupKey、DisplayName、またはPhoneNumber)をログに記録したいと思います。

データに応じて可視性を設定したシンプルなTextViewとImageviewを表示します。レイアウトは次のとおりです:contact_entry.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:baselineAligned="false"
android:layout_width="fill_parent" android:padding="5px">
            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:textSize="16px"
                android:text="@+id/contactEntryText" android:id="@+id/contactEntryText">
            </TextView>
            <TableRow android:paddingLeft="5px" android:visibility="visible"
                android:paddingRight="5px" android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/indicator_logo_table"
                android:gravity="right">
                <ImageView android:layout_width="wrap_content"
                    android:id="@+id/imageView1"
                    android:layout_height="fill_parent"
                    android:src="@drawable/indicator">
                </ImageView>
            </TableRow>
</LinearLayout>

私が使用するアダプターは以下のように定義されています:

SimpleAdapterWithImageView adapter = new SimpleAdapterWithImageView(
            this, R.layout.contact_entry, cursor, fields,
            new int[] { R.id.contactEntryText });
    mContactList.setAdapter(adapter);

(これは、ContactsContract ContentProviderから連絡先のDisplay_Nameを表示するためのものであると推測できます。)

そして最後にmContactList.setOnItemClickListener..です。

mContactList.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

LinearLayout linear = (LinearLayout) view.findViewById(R.layout.contact_entry);
Log.i("done","linear layout"); // it's not done at all ! findViewById returns null;

//this part is currently useless
TextView displayed_name = (TextView) linear.findViewById(R.id.contactEntryText);
Log.i("done","Displayed name = linear.findViewById contactentrytext");


 Toast.makeText(getApplicationContext(),
             // displayed_name.getText(), //
             //view.getContentDescription(), //
             Toast.LENGTH_SHORT).show();
    }       

ご覧のとおり、私は今のところトーストを表示しようとしているだけなので、最終的には名前が意図の追加として付けられます。

つまり、過去36時間(ため息)で得た最良の結果は、2番目のアイテムをクリックした場合でも、トーストに表示された最初の連絡先の名前を表示することです...

TextView displayed_name = (TextView) findViewById(R.id.contactEntryText);
Toast.makeText(getApplicationContext(),displayed_name.getText(),
                          Toast.LENGTH_SHORT).show();
                    }

私はここで提供されているチュートリアルグーグルのようなものを試してきました(私は間違っていますか?私はAndroidに慣れていないので、顔を平手打ちすることを躊躇しないでください...)実際にビュー全体をTextViewにキャストします.. 。

さらに、view.getChildAt()などの他のトリック...何時間も。

それほど難しいことではないと確信していますが、私は輪になって走っています!私のアダプターのせいではないと思いますが、誰かがそれが原因である可能性があると思うなら、私は別の投稿でコードを提供することができます。

前もって感謝します !

4

1 に答える 1

1
LinearLayout linear = (LinearLayout) view.findViewById(R.layout.contact_entry);

R.layoutここに間違いがあります-で使用することはできませんfindViewById。通常、そこで使用するか、レイアウトを膨らませてR.id置き換える必要があります。ただし、この場合は、 from引数 findViewByIdが必要です。view

引用されたコードを次のコードに置き換えます。
LinearLayout linear = (LinearLayout) view;

于 2011-03-17T15:37:12.357 に答える