Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过单击listview android studio上的项目获得多个值_Android_Listview - Fatal编程技术网

如何通过单击listview android studio上的项目获得多个值

如何通过单击listview android studio上的项目获得多个值,android,listview,Android,Listview,我需要从listview中获取值,但我还找不到任何解决方案,我创建listview使其看起来像一个表,并希望在单击行/listview项时获取值 这是我的代码: 1。Bill.java public class Bill { private int _id; private String _bill_no; private String _type; private String _table; private int _qty; private

我需要从listview中获取值,但我还找不到任何解决方案,我创建listview使其看起来像一个表,并希望在单击行/listview项时获取值

这是我的代码:

1。Bill.java

public class Bill {

    private int _id;
    private String _bill_no;
    private String _type;
    private String _table;
    private int _qty;
    private String _amount;
    private String _status;

    public Bill() {

    }

    public Bill(int id, String bill_no, String type, String table, int qty, String amount, String status) {
        this._id = id;
        this._bill_no = bill_no;
        this._type = type;
        this._table = table;
        this._qty = qty;
        this._amount = amount;
        this._status = status;
    }

    public Bill(String bill_no, String type, String table, int qty, String amount, String status) {
        this._bill_no = bill_no;
        this._type = type;
        this._table = table;
        this._qty = qty;
        this._amount = amount;
        this._status = status;
    }

    public int getID() {
        return this._id;
    }
    public void setID(int id) {
        this._id = id;
    }

    public String get_bill_no()
    {
        return this._bill_no;
    }
    public void set_bill_no(String bill_no)
    {
        this._bill_no = bill_no;
    }

    public String get_type()
    {
        return this._type;
    }
    public void set_type(String type)
    {
        this._type = type;
    }

    public String get_table()
    {
        return this._table;
    }
    public void set_table(String table)
    {
        this._table = table;
    }

    public int get_qty()
    {
        return this._qty;
    }
    public void set_qty(int qty)
    {
        this._qty = qty;
    }

    public String get_amount()
    {
        return this._amount;
    }
    public void set_amount(String amount)
    {
        this._amount = amount;
    }

    public String get_status()
    {
        return this._status;
    }
    public void set_status(String status)
    {
        this._status = status;
    }

}
public class BillListAdapter extends BaseAdapter {
    private ArrayList<Bill> listData;
    private LayoutInflater layoutInflater;

    public BillListAdapter(Context aContext, ArrayList<Bill> listData) {
        this.listData = listData;
        layoutInflater = LayoutInflater.from(aContext);
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_bill_layout, null);
            holder = new ViewHolder();
            holder.txtBillNo = (TextView) convertView.findViewById(R.id.txtBillId);
            holder.txtBillNo = (TextView) convertView.findViewById(R.id.txtBillNo);
            holder.txtBillType = (TextView) convertView.findViewById(R.id.txtBillType);
            holder.txtBillTable = (TextView) convertView.findViewById(R.id.txtBillTable);
            holder.txtBillQty = (TextView) convertView.findViewById(R.id.txtBillQty);
            holder.txtBillAmount = (TextView) convertView.findViewById(R.id.txtBillAmount);
            holder.txtBillStatus = (TextView) convertView.findViewById(R.id.txtBillStatus);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.txtBillId.setText(listData.get(position).getID());
        holder.txtBillNo.setText(listData.get(position).get_bill_no());
        holder.txtBillType.setText(listData.get(position).get_type());
        holder.txtBillTable.setText(listData.get(position).get_table());
        //holder.txtBillQty.setText(listData.get(position).get_qty());
        holder.txtBillAmount.setText(listData.get(position).get_amount());
        holder.txtBillStatus.setText(listData.get(position).get_status());

        return convertView;
    }

    static class ViewHolder {
        TextView txtBillId;
        TextView txtBillNo;
        TextView txtBillType;
        TextView txtBillTable;
        TextView txtBillQty;
        TextView txtBillAmount;
        TextView txtBillStatus;
    }
}
2.BillListAdapter.java

public class Bill {

    private int _id;
    private String _bill_no;
    private String _type;
    private String _table;
    private int _qty;
    private String _amount;
    private String _status;

    public Bill() {

    }

    public Bill(int id, String bill_no, String type, String table, int qty, String amount, String status) {
        this._id = id;
        this._bill_no = bill_no;
        this._type = type;
        this._table = table;
        this._qty = qty;
        this._amount = amount;
        this._status = status;
    }

    public Bill(String bill_no, String type, String table, int qty, String amount, String status) {
        this._bill_no = bill_no;
        this._type = type;
        this._table = table;
        this._qty = qty;
        this._amount = amount;
        this._status = status;
    }

    public int getID() {
        return this._id;
    }
    public void setID(int id) {
        this._id = id;
    }

    public String get_bill_no()
    {
        return this._bill_no;
    }
    public void set_bill_no(String bill_no)
    {
        this._bill_no = bill_no;
    }

    public String get_type()
    {
        return this._type;
    }
    public void set_type(String type)
    {
        this._type = type;
    }

    public String get_table()
    {
        return this._table;
    }
    public void set_table(String table)
    {
        this._table = table;
    }

    public int get_qty()
    {
        return this._qty;
    }
    public void set_qty(int qty)
    {
        this._qty = qty;
    }

    public String get_amount()
    {
        return this._amount;
    }
    public void set_amount(String amount)
    {
        this._amount = amount;
    }

    public String get_status()
    {
        return this._status;
    }
    public void set_status(String status)
    {
        this._status = status;
    }

}
public class BillListAdapter extends BaseAdapter {
    private ArrayList<Bill> listData;
    private LayoutInflater layoutInflater;

    public BillListAdapter(Context aContext, ArrayList<Bill> listData) {
        this.listData = listData;
        layoutInflater = LayoutInflater.from(aContext);
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_bill_layout, null);
            holder = new ViewHolder();
            holder.txtBillNo = (TextView) convertView.findViewById(R.id.txtBillId);
            holder.txtBillNo = (TextView) convertView.findViewById(R.id.txtBillNo);
            holder.txtBillType = (TextView) convertView.findViewById(R.id.txtBillType);
            holder.txtBillTable = (TextView) convertView.findViewById(R.id.txtBillTable);
            holder.txtBillQty = (TextView) convertView.findViewById(R.id.txtBillQty);
            holder.txtBillAmount = (TextView) convertView.findViewById(R.id.txtBillAmount);
            holder.txtBillStatus = (TextView) convertView.findViewById(R.id.txtBillStatus);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.txtBillId.setText(listData.get(position).getID());
        holder.txtBillNo.setText(listData.get(position).get_bill_no());
        holder.txtBillType.setText(listData.get(position).get_type());
        holder.txtBillTable.setText(listData.get(position).get_table());
        //holder.txtBillQty.setText(listData.get(position).get_qty());
        holder.txtBillAmount.setText(listData.get(position).get_amount());
        holder.txtBillStatus.setText(listData.get(position).get_status());

        return convertView;
    }

    static class ViewHolder {
        TextView txtBillId;
        TextView txtBillNo;
        TextView txtBillType;
        TextView txtBillTable;
        TextView txtBillQty;
        TextView txtBillAmount;
        TextView txtBillStatus;
    }
}
公共类BillListAdapter扩展了BaseAdapter{
私有数组列表数据;
私人停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场;
公共BillListAdapter(上下文、文本、ArrayList listData){
this.listData=listData;
layoutInflater=layoutInflater.from(文本);
}
@凌驾
public int getCount(){
返回listData.size();
}
@凌驾
公共对象getItem(int位置){
返回listData.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视窗座;
if(convertView==null){
convertView=LayoutFlater.充气(R.layout.list\u bill\u layout,null);
holder=新的ViewHolder();
holder.txtBillNo=(TextView)convertView.findViewById(R.id.txtBillId);
holder.txtBillNo=(TextView)convertView.findViewById(R.id.txtBillNo);
holder.txtBillType=(TextView)convertView.findViewById(R.id.txtBillType);
holder.txtBillTable=(TextView)convertView.findViewById(R.id.txtBillTable);
holder.txtBillQty=(TextView)convertView.findViewById(R.id.txtBillQty);
holder.txtBillAmount=(TextView)convertView.findViewById(R.id.txtBillAmount);
holder.txtBillStatus=(TextView)convertView.findViewById(R.id.txtBillStatus);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
holder.txtBillId.setText(listData.get(position.getID());
holder.txtBillNo.setText(listData.get(position.get_bill_no());
holder.txtBillType.setText(listData.get(position.get_type());
holder.txtBillTable.setText(listData.get(position.get_table());
//holder.txtBillQty.setText(listData.get(position.get_qty());
holder.txtBillAmount.setText(listData.get(position.get_amount());
holder.txtBillStatus.setText(listData.get(position.get_status());
返回视图;
}
静态类视窗夹{
TextView txtBillId;
TextView txtBillNo;
text视图txtBillType;
TextView txtBillTable;
TextView txtBillQty;
text查看txtBillAmount;
text查看txtBillStatus;
}
}
3。list\u bill\u layout.xml(循环的项目)


4.1。ListView的Index.java片段

ArrayList list_bill = getListBill();
        final ListView listview_bill = (ListView) findViewById(R.id.order_listing);
        listview_bill.setAdapter(new BillListAdapter(this, list_bill));

        listview_bill.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                //how to get the value ?
                Log.d(TAG, "click bill");

            }
        });
ArrayList\u bill=getListBill();
最终ListView ListView\u bill=(ListView)findViewById(R.id.order\u列表);
listview_bill.setAdapter(新的BillListAdapter(这个,列表_bill));
listview_bill.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共视图单击(适配器视图a、视图v、内部位置、长id){
//如何获得价值?
Log.d(标记“点击账单”);
}
});
函数getListBill()的4.2 Index.java片段

private ArrayList getListBill()
{
SQLiteDatabase mydatabase=openOrCreateDatabase(“posDb”,MODE_PRIVATE,null);
Cursor resultSet=mydatabase.rawQuery(“从票据中选择*项”,空);
ArrayList结果=新建ArrayList();
if(resultSet.moveToFirst()){
做{
账单数据=新账单();
billsData.set_bill_no(resultSet.getString(1));
billsData.set_类型(resultSet.getString(2));
billsData.set_表(resultSet.getString(3));
billsData.set_数量(结果集getInt(4));
billsData.set_金额(resultSet.getString(5));
billsData.set_状态(resultSet.getString(6));
//把它们变成结果
结果。添加(billsData);
}while(resultSet.moveToNext());
}
返回结果;
}

您可以获得如下值:

ArrayList<Bill> list_bill = getListBill();
    final ListView listview_bill = (ListView) findViewById(R.id.order_listing);
    listview_bill.setAdapter(new BillListAdapter(this, list_bill));

    listview_bill.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            //how to get the value ?
            Log.d(TAG, "click bill");
            Bill mBill = list_bill.get(position); 
            // Now you can get what you want from mBill object.

        }
    });

   //And in your getListBill method use ArrayList<Bill> instead of only ArrayList.
ArrayList\u bill=getListBill();
最终ListView ListView\u bill=(ListView)findViewById(R.id.order\u列表);
listview_bill.setAdapter(新的BillListAdapter(这个,列表_bill));
listview_bill.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共视图单击(适配器视图a、视图v、内部位置、长id){
//如何获得价值?
Log.d(标记“点击账单”);
Bill mBill=list\u Bill.get(位置);
//现在,您可以从mBill对象获得所需的内容。
}
});
//在getListBill方法中,使用ArrayList而不是仅使用ArrayList。

方法如下-

 listview_bill.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                   Bill billsData=(Bill ) a.getItemAtPosition(position);
                   //data= billsData.get_amount();
             }
     });
listview\u bill.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共视图单击(适配器视图a、视图v、内部位置、长id){
Bill billsData=(Bill)a.getItemAtPosition(位置);
//data=billsData.get_amount();
}
});
试试看


我尝试了这个方法:但是得到了错误:java.lang.NullPointerException:尝试在一个空对象引用上调用虚拟方法'void android.widget.TextView.setText(int)”,这里的snipper:Bill billsData=(Bill)a.getItemAtPosition(position);字符串bill_no=billsData.get_bill_no();Log.d(标签“点击票据ID:+票据编号”);怎么了?BillListAdapter ba=新的BillListAdapter(这个,列表\账单);listview_bill.setAdapter(ba);在内部单击try-using-like-it-Bill-billsData=ba.getItem(位置);给我错误:E
Bill bill = (Bill) a.getItemAtPosition(position);