1

ボタンを ListView 行で動作させようとしています。カスタム アダプターについて読んでいますが、動作させることができません。基本的に、4 つの TextView と 1 つのボタンを持つ List_item クラスがあり、別のクラスで ListView を使用しています。textViews の 1 つに id/Name があります。だから私が欲しいのは、その行のボタンを押したときです。TextViewの「名前」の値を取得してArrayListに追加し、別の行で別のボタンを押すと、「名前」の値を同じ配列リストに追加します。基本的に、ボタンを押したすべてのものをArrayListに追加します。それは可能ですか??。

追加するだけです。textView は私のデータベースによってフィードされています..返信を前もって感謝します。

List_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="10dp" >

 <TableRow
    android:id="@+id/tableRow1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false" >

    <TextView
        android:id="@+id/id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"
        android:visibility="gone" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:layout_weight="3"
        android:textAllCaps="true"
        android:textColor="#FFFFFF"
        android:textSize="30sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:id="@+id/price"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="right"
        android:paddingRight="15dp"
        android:textAllCaps="true"
        android:textColor="#FFFFFF"
        android:textSize="30sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <Button
        android:id="@+id/order"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="0"
        android:focusable="false"
        android:minHeight="0dp"
        android:minWidth="50dp"
        android:text="+"
        android:textStyle="bold" />

</TableRow>

<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/tableRow1"
    android:gravity="center_horizontal" >

    <TextView
        android:id="@+id/description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAllCaps="true"
        android:textColor="#FFFFFF"
        android:textSize="20sp"
        android:textStyle="bold"
        android:typeface="serif"
        android:visibility="gone" />

</TableRow>

</RelativeLayout>

私のmenu.xmlには

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="697dp" >
</ListView>

行のボタンを押したときに TextView " name " の値を取得したいと思います。

これは、ボタンを追加する前にlistViewを埋めるために使用した方法であり、正常に機能していました

   ListAdapter adapter = new SimpleAdapter (getCategoryItems.this,itemsList,R.layout.list_item, new String[]{ TAG_ID, TAG_NAME,TAG_PRICE, TAG_DESCRIPTION},new int[] { R.id.id, R.id.name, R.id.price, R.id.description});
setListAdapter(adapter);

しかし、ボタンを追加した後、値が取得されないため、再検索したところ、sampleAdapter をカスタム アダプターに変更する必要があることがわかりました。getView() 内にボタンを追加しますが、そのコツをつかむことができません。

マイボタン OnClickListener

Button order = (Button) view.findViewById(R.id.order);
order.setOnClickListener(new Button.OnClickListener() {
    @Override 
    public void onClick(View v) { 
        String name = ((TextView)view.findViewById(R.id.name)).getText().toString();
        Log.d("Selected : ", name);
    }
});
4

1 に答える 1

0

用のカスタム アダプタ クラスを作成する必要がありますListView。データベースからデータを取得しているので、 and を拡張してオーバーライドすることをお勧めしCursorAdapterます。私の経験から、これを正しく行う方法は文書化されていないため、ここでいくつかの指針を示します。newView()bindView()

では、 を呼び出すことによって提供される をnewView()使用する必要があります。これを呼び出した後、その特定の行の Button を取得して、その.LayoutInflatorgetLayoutInflator()ListActivityinflate()LayoutInflatorOnClickListener

では、Android が既存のオブジェクトの再利用を要求しているため、新しいビューをインbindView()フレートする必要がないことを除いて、ほとんど同じことを行います。View繰り返されるコードの量を減らすために、いくつかの追加のメソッドを作成したい場合があります。

コード例ではなく英語で説明されていますが、これがお役に立てば幸いです。

于 2013-02-17T00:54:03.657 に答える