一个按钮添加到ListView每个元素 [英] Add a Button to a ListView for each Element

查看:207
本文介绍了一个按钮添加到ListView每个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个购物车应用程序,我想填充的String []产品被称为项目。每个项目都有一个ID名称和价格。我遇到的问题是,我想创建一个自定义的ListView,但我不能让它在我的Andr​​oid设备上运行。它总是死机每次我使用一个客户的ListView时间。也许我不这样做是对还是我需要创建一个客户适配器呢?我曾尝试这些方法但也不是成功的。这里是我有什么,我目前使用simple_list_item_check布局,但我只是实现了,看看它是否会工作,其中,做。 Android的简单的布局做,但我的自定义不会。任何人都可以引导我正确的方向?

CustomerView.activity

 进口android.support.v7.app.ActionBarActivity;
    进口android.os.Bundle;
    进口android.view.Menu;
    进口android.view.MenuItem;
    进口android.view.View;
    进口android.widget.AdapterView;
    进口android.widget.ArrayAdapter;
    进口android.widget.ListAdapter;
    进口android.widget.ListView;
    进口android.widget.Toast;
    进口android.widget.CheckedTextView;
    公共类CustomerView扩展ActionBarActivity {@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_customer_view);    产品[] =项目{
            新产品(1,牛奶,21.50)
            新产品(2,黄油,15.99)
            新产品(3,酸奶,14.90)
            新产品(四,牙膏,7.99)
            新产品(5,冰淇淋,​​10.00)    };    最后ListAdapter theAdapter =新ArrayAdapter<>(这一点,
            android.R.layout.simple_list_item_checked,项目);    ListView控件customerlist =(ListView控件)findViewById(R.id.customerList);
    customerlist.setTextFilterEnabled(真);
    customerlist.setAdapter(theAdapter);
    customerlist.setOnItemClickListener(新AdapterView.OnItemClickListener(){
        @覆盖
        公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
            CheckedTextView检查=(CheckedTextView)视图。
            check.setChecked(check.isChecked()!);
            字符串itemselected =你感动+
                  将String.valueOf(theAdapter.getItem(位置));            Toast.makeText(CustomerView.this,itemselected,Toast.LENGTH_SHORT).show();        }
    });}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.menu_customer_view,菜单);
    返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //处理动作栏项目点击这里。操作栏会
    //自动处理上点击主页/向上按钮,只要
    //你在AndroidManifest.xml中指定一个父活动。
    INT ID = item.getItemId();    // noinspection SimplifiableIfStatement
    如果(ID == R.id.action_settings){
        返回true;
    }    返回super.onOptionsItemSelected(项目);
}
}

Product.java

 公共类产品{私人诠释身份证;
私人字符串名称;
私人双人价格;
选择私人布尔;
公共产品(INT ID,字符串名称,双击价格){
    this.id = ID;
    this.name =名称;
    this.price =价格;}@覆盖
公共字符串的toString(){
    返回this.id ++ this.name +[$+ this.price +];
}
}

row_layout.xml

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=横向
工具:上下文=shoppingcart.cop4331.com.shoppingcart.CustomerView>
<按钮
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=加入购物车
    机器人:重力=右
    />
<的TextView
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:ID =@ + ID / textview1
    机器人:TEXTSIZE =20SP/>    < / LinearLayout中>


解决方案

更改行:

 最后ListAdapter theAdapter =新ArrayAdapter<>(这一点,
        android.R.layout.simple_list_item_checked,项目);

 最后ListAdapter theAdapter =新ArrayAdapter<>(这一点,
           R.layout.row_layout,R.id.textview1,项目);

您使用错误的ArrayAdapter构造:)
如果根视图不是一个TextView(你的情况根视图的LinearLayout),你必须通过TextView的ID

I am creating a shopping cart app and I am trying to populate a String[] Product which is called items. Each item has an ID a Name and a Price. The issue I am having is that I am trying to make a custom ListView but I can not get it to run on my android device. It keeps crashing every time I use a customer ListView. Maybe I am not doing it right or I need to create a customer adapter as well? I have tried these methods but neither were successful. Here is what I have, I currently am using the simple_list_item_check layout but I just implemented that to see if it would work, in which that does. The android simple layouts do, but my custom won't. Can anyone lead me in the right direction?

CustomerView.activity

    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.Toast;
    import android.widget.CheckedTextView;


    public class CustomerView extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_customer_view);

    Product[] items = {
            new Product(1, "Milk", 21.50),
            new Product(2, "Butter", 15.99),
            new Product(3, "Yogurt", 14.90),
            new Product(4, "Toothpaste", 7.99),
            new Product(5, "Ice Cream", 10.00),

    };

    final ListAdapter theAdapter = new ArrayAdapter<>(this,
            android.R.layout.simple_list_item_checked, items);

    ListView customerlist = (ListView) findViewById(R.id.customerList);
    customerlist.setTextFilterEnabled(true);
    customerlist.setAdapter(theAdapter);
    customerlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            CheckedTextView check = (CheckedTextView)view;
            check.setChecked(!check.isChecked());
            String itemselected = "You Touched " +
                  String.valueOf(theAdapter.getItem(position));

            Toast.makeText(CustomerView.this, itemselected, Toast.LENGTH_SHORT).show();

        }
    });

}





@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_customer_view, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

Product.java

    public class Product {

private int id;
private String name;
private double price;
private boolean selected;


public Product(int id, String name, double price) {
    this.id = id;
    this.name = name;
    this.price = price;

}

@Override
public String toString() {
    return this.id + " " + this.name + " [$" + this.price + "]";
}
}

row_layout.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="shoppingcart.cop4331.com.shoppingcart.CustomerView">


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add to Cart"
    android:gravity="right"
    />


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textview1"
    android:textSize="20sp"/>



    </LinearLayout>

解决方案

Change line:

final ListAdapter theAdapter = new ArrayAdapter<>(this,
        android.R.layout.simple_list_item_checked, items);

to:

final ListAdapter theAdapter = new ArrayAdapter<>(this,
           R.layout.row_layout , R.id.textview1, items);

You use wrong ArrayAdapter constructor :) If root view is not a TextView (in your case root view is LinearLayout) you have to pass TextView id       

这篇关于一个按钮添加到ListView每个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆