0

半分しか理解していない他の人のコードをコピーして、各要素に 3 つの TextView と CheckBox を含むリストビューを作成することに成功しました。

コードには、次の 2 つの xml ファイルが含まれます。最初の「customrowview.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="fill_parent"
  android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="142dp"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            <TextView android:id="@+id/text1"         

                android:layout_width="match_parent"         
                android:layout_height="fill_parent"/>
            <TextView android:id="@+id/text2"         

                android:layout_width="match_parent"         
                android:layout_height="fill_parent"/>
            <TextView android:id="@+id/text3" 

                android:layout_width="match_parent" 
                android:layout_height="wrap_content"/>

        </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right" >



    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />
        <Button
            android:id="@+id/editbut"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Edit" />
        </LinearLayout>
    </LinearLayout>



</LinearLayout>

次に「customlistview.xml」:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ListView android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000fff"
    android:layout_weight="2"
    android:drawSelectorOnTop="false">
    </ListView>
<TextView  android:id="@id/android:empty"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFff00"
    android:text="No data"
    />
</LinearLayout>

次の方法でリストにアクセスできます。

listView = (ListView) findViewById(R.id.mylist);

コードには以下も含まれます。

    SimpleAdapter adapter = new SimpleAdapter(
            this,
            list,
            R.layout.custom_row_view,
            new String[] {"label","daysordate","time"},
            new int[] {R.id.text1,R.id.text3, R.id.text2}
            );
listView.setAdapter(adapter);

今私がやりたいのは、 listView.setlistenerforbuttonwithinlist() のようなものです!

しかし、それを行う方法を解決することはできません。以前にSOに関連する質問があったことは知っていますが、答えを理解できません:-(

編集: azgolfersの回答を見た後...次のようにカスタムアダプターを作成しました:

public class myadapter extends SimpleAdapter
{
    LayoutInflater mLayoutInflater;


    public void myadapter(Context context) 
    {
        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View view = null;

        if (convertView != null)
        {
            view = convertView;
        } 
        else 
        {      
            view = mLayoutInflater.inflate(R.layout.custom_row_view, null);
        }

        Button buttonEdit = (Button) view.findViewById(R.id.editbut);

        buttonEdit.setOnClickListener(new OnClickListener()
        {   
            public void onClick(View arg0)
            {
                Log.i("xx","Button pressed!");
            }
        });

        return super.getView(position, convertView, parent);
    }

    public myadapter(Context context, List<? extends Map<String, ?>> data,int resource, String[] from, int[] to) 
    {
        super(context, data, resource, from, to);
        // TODO Auto-generated constructor stub
    }

}

残念ながら、これは行でクラッシュします

view = mLayoutInflater.inflate(R.layout.custom_row_view, null);

ヌルポインタ例外で...理由がわからない:-(

4

2 に答える 2

4

最初に、おそらく ArrayAdapter から独自の Adapter を拡張する必要があります。ArrayAdapter には getView というメソッドがあり、特定の位置でリストビュー行の UI をオーバーライドして提供する必要があります。例えば

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView != null) {
            view = convertView;
        } else {
            view = mLayoutInflater.inflate(R.layout.custom_row_view, null);
        }
        Button buttonEdit = (Button) view.findViewById(R.id.editbut);
        buttonEdit.setOnClickListener(...);
    }

getView() では、行の UI を構築しているため、ここでボタンにクリック ハンドラーを設定する機会があります。

また、これをボタンの xml タグに追加する必要もあります。

android:focusable="false"
android:focusableInTouchMode="false" 

そうしないと、リストビュー内のボタンが押されたときに OnClick イベントが発生しません。

于 2012-07-15T14:42:29.227 に答える
1

変えようとする

 view = mLayoutInflater.inflate(R.layout.custom_row_view, null);

 view = mLayoutInflater.inflate(R.layout.custom_row_view, parent, false);

幸運を!

于 2012-07-16T11:21:45.593 に答える