Android:我正在尝试构建自定义列表视图,每行包含3个项目

Android:我正在尝试构建自定义列表视图,每行包含3个项目,android,listview,Android,Listview,我正在尝试构建自定义列表视图,其中每行包含以下项目: 可点击图像 正文 可点击图像 我尝试了许多在其他线程中找到的自定义适配器,但直到现在都没有成功。两个可单击的图像都将使用该行上的文本 这是我的行布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:layout_width="wrap_co

我正在尝试构建自定义列表视图,其中每行包含以下项目:

  • 可点击图像
  • 正文
  • 可点击图像
我尝试了许多在其他线程中找到的自定义适配器,但直到现在都没有成功。两个可单击的图像都将使用该行上的文本

这是我的行布局:

<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>
编辑2

additems()方法:

public void additems()
{       
列表测试=新建ArrayList();
试一试{
测试。添加(“测试1”);
测试。添加(“测试2”);
测试。添加(“测试3”);
ListView购物车=(ListView)findViewById(R.id.listViewcart);
ListAdapter customAdapter=新ListAdapter(此,R.layout.shoppingcart_行,测试);
cart.setAdapter(customAdapter);//第109行
}
捕获(例外e){
e、 printStackTrace();
系统输出打印LN(测试);
}
}
试试这个:

在主类中:
List test=new ArrayList()


在适配器中:
私有列表项

它得到错误的那一行基本上是说它没有找到您的
列表视图。这是因为您可能会混淆
列表视图的ID
。在此错误中,购物车为空

设置
活动
以使用正确的XML文件

setContentView(R.layout.shoppingcart);
换行

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

现在使用
ListView
将布局的XML更改为此,并删除另一个
ListView

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

</ListView>

在调用addItems()之前,确保在活动中调用了setContentView(R.layout.layout)

你在哪一行得到了NPE?我刚刚在我的问题中添加了我的logcat错误。你能发布
additems
方法并显示哪一行是第109行吗?我刚刚进行了另一次编辑并将它们包含在其中。你的意思是要有两个
列表视图
吗?好的,行
ShoppingCart.java:109
?您还尝试过
tt.setText(items.get(position.toString())是的,我试过了,它不起作用。第109行是cart.setAdapter(customAdapter);Yuhuuuuuu!!!我把你的解决方案和柯克的一个结合在一起,现在它起作用了!!!非常感谢,我有这个。在我的例子中,它是:setContentView(R.layout.shoppingcart_行);具有
列表视图的XML布局的名称是什么?shoppingcart.XML-这是我根据答案更新的列表视图,但请确保您的活动正在使用此XML文件<代码>设置内容视图(R.layout.shoppingcart)在我的代码中我使用了:setContentView(R.layout.shoppingcart_行);现在,我按照您的建议进行了更改,收到了一个错误:java.lang.ClassCastException:java.lang.String无法转换为android.content.ClipData$ItemOkay,您现在有进展了
R.layout.shoppingcart\u行
不正确,因为它在您的ListView中使用,但是
R.layout.shoppingcart
用于实际显示您的
ListView
。您需要更改自定义适配器。我将对我的答案进行编辑
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]
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);
    }
}
setContentView(R.layout.shoppingcart);
ListView cart = (ListView) findViewById(R.id.listViewcart);
ListView cart = (ListView) findViewById(android.R.id.list);
<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

</ListView>
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;
    }
}