1

各行に次の項目を含むカスタム リスト ビューを作成しようとしています。

  • クリック可能な画像
  • 文章
  • クリック可能な画像

他のスレッドで見つけた多くのカスタム アダプターを試しましたが、今まで成功しませんでした。クリック可能な画像は両方とも、その行にあるテキストを使用します。

これが私の行レイアウトです:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="Minus"
        android:src="@drawable/minus" />

    <EditText
        android:id="@+id/item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:layout_weight="1"
        android:clickable="false"
        android:ems="10"
        android:focusable="false"
        android:gravity="center_vertical|center_horizontal"
        android:inputType="text" >

    </EditText>
    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="Plus"
        android:src="@drawable/plus" />
</LinearLayout>

ここに私のリストビューがあります:

<ListView
    android:id="@+id/listViewcart"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

</ListView>

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

</ListView> 

これが私の現在のカスタムアダプターです:

public class ListAdapter extends ArrayAdapter<Item> {
    public ListAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        // TODO Auto-generated constructor stub
    }

    private List<Item> items;

    public ListAdapter(Context context, int resource, List<Item> items) {
        super(context, resource, items);
        this.items = items;
    }

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

        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.shoppingcart_row, null);
        }

        Item p = items.get(position);

        if (p != null) {
            TextView tt = (TextView) v.findViewById(R.id.item);

            if (tt != null) {
                tt.setText(p.getText());
            }
        }

        return v;
    }
}

そして、これが私のメインクラスでそれを呼び出す方法です:

List test = new ArrayList<String>();
try {
    test.add("test1");
    test.add("test2");
    test.add("test3");

    ListView cart = (ListView) findViewById(R.id.listViewcart); 

    ListAdapter customAdapter = new ListAdapter(this, R.layout.shoppingcart_row, test);

    cart.setAdapter(customAdapter);
}
catch (Exception e) {
    e.printStackTrace();
    System.out.println(test);
}

NPEを受けています。念のためテストリストを印刷したところ、追加する要素が 3 つあることがわかります。問題はカスタム アダプターのどこかにあると思いますが、わかりません。

また、List の代わりに ArrayList を使用してみましたが、カスタム アダプターを変更して動作させることはできません。誰かが私がここで間違ったことを見ることができますか? ありがとうございました。

編集:

logcat エラー ログ:

java.lang.NullPointerException
   at com.example.hrana.za.vkushti.ShoppingCart.additems(ShoppingCart.java:109)
   at com.example.hrana.za.vkushti.ShoppingCart.onCreate(ShoppingCart.java:49)
   at android.app.Activity.performCreate(Activity.java:5008)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
   at android.app.ActivityThread.access$600(ActivityThread.java:130)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4745)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
   at dalvik.system.NativeStart.main(Native Method)
[test1, test2, test3]

編集2

additems() メソッド:

public void additems()
{       
    List test = new ArrayList<String>();
    try{
        test.add("test1");
        test.add("test2");
        test.add("test3");

        ListView cart = (ListView) findViewById(R.id.listViewcart); 

        ListAdapter customAdapter = new ListAdapter(this, R.layout.shoppingcart_row, test);

        cart.setAdapter(customAdapter); // line 109
    }
    catch (Exception e){
        e.printStackTrace();
        System.out.println(test);
    }
}
4

3 に答える 3

0

これを試して:

メインクラス:List<String> test = new ArrayList<String>();

アダプター内:private List<String> items;

于 2013-03-19T16:47:01.540 に答える
0

エラーが発生する行は、基本的に、ListView. これは、 の ID を混同している可能性があるためですListView。このエラーでは、cartnullです。

Activity正しい XML ファイルを使用するように設定してください。

setContentView(R.layout.shoppingcart);

この行を変更

ListView cart = (ListView) findViewById(R.id.listViewcart);

ListView cart = (ListView) findViewById(android.R.id.list);

レイアウトの XMLListViewを this に変更し、other を削除しListViewます。

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

</ListView>

ではなくListAdapterを使用するようにカスタム アダプタを変更します。アダプターをインスタンス化すると、無効なキャスト エラーが発生します。StringItem

ArrayList<String>の代わりに を使用したアダプターを次に示しますList<Item>

public class ListAdapter extends ArrayAdapter<String> {
    public ListAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        // TODO Auto-generated constructor stub
    }

    private ArrayList<String> items;

    public ListAdapter(Context context, int resource, List<String> items) {
        super(context, resource, items);
        this.items = items;
    }

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

        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.shoppingcart_row, null);
        }

        String p = items.get(position);

        if (p != null) {
            TextView tt = (TextView) v.findViewById(R.id.item);

            if (tt != null) {
                tt.setText(p.getText());
            }
        }

        return v;
    }
}
于 2013-03-19T17:03:43.957 に答える