我需要从listview获取价值,但我还无法找到任何解决方案,我创建listview看起来像一个表,并希望在我点击row / 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;
}
}
2.BillListAdapter.java
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;
}
}
第3。 list_bill_layout.xml(循环的项目)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_height="wrap_content"
android:textSize="14dp"
android:padding="5dp"
android:background="#EEE"
android:textColor="#333"
android:layout_width="150dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:id="@+id/txtBillNo"
android:text="Bill No"
/>
<TextView
android:layout_height="wrap_content"
android:textSize="14dp"
android:padding="5dp"
android:background="#EEE"
android:textColor="#333"
android:layout_width="100dp"
android:id="@+id/txtBillType"
android:text="Bill Type"
android:focusable="false"
android:focusableInTouchMode="false"
/>
<TextView
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:textSize="14dp"
android:padding="5dp"
android:background="#EEE"
android:textColor="#333"
android:layout_width="100dp"
android:id="@+id/txtBillTable"
/>
<TextView
android:layout_height="wrap_content"
android:textSize="14dp"
android:padding="5dp"
android:background="#EEE"
android:textColor="#333"
android:layout_width="100dp"
android:id="@+id/txtBillQty"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="0"
/>
<TextView
android:layout_height="wrap_content"
android:textSize="14dp"
android:padding="5dp"
android:background="#EEE"
android:textColor="#333"
android:layout_width="150dp"
android:id="@+id/txtBillAmount"
android:text="Amount"
android:focusable="false"
android:focusableInTouchMode="false"
/>
<TextView
android:layout_height="wrap_content"
android:textSize="14dp"
android:padding="5dp"
android:background="#EEE"
android:textColor="#333"
android:layout_width="150dp"
android:id="@+id/txtBillStatus"
android:text="Status"
android:focusable="false"
android:focusableInTouchMode="false"
/>
<TextView
android:layout_height="wrap_content"
android:textSize="14dp"
android:padding="5dp"
android:background="#EEE"
android:textColor="#333"
android:layout_width="180dp"
android:id="@+id/txtBillDate"
android:focusable="false"
android:focusableInTouchMode="false"
/>
</LinearLayout>
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");
}
});
4.2函数getListBill()的Index.java片段
private ArrayList getListBill()
{
SQLiteDatabase mydatabase = openOrCreateDatabase("posDb",MODE_PRIVATE,null);
Cursor resultSet = mydatabase.rawQuery("SELECT * FROM bills",null);
ArrayList<Bill> results = new ArrayList<Bill>();
if (resultSet.moveToFirst()) {
do {
Bill billsData = new Bill();
billsData.set_bill_no(resultSet.getString(1));
billsData.set_type(resultSet.getString(2));
billsData.set_table(resultSet.getString(3));
billsData.set_qty(resultSet.getInt(4));
billsData.set_amount(resultSet.getString(5));
billsData.set_status(resultSet.getString(6));
//put them into results
results.add(billsData);
} while (resultSet.moveToNext());
}
return results;
}
答案 0 :(得分:1)
你可以得到这样的价值:
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.
答案 1 :(得分:1)
方式如下 -
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();
}
});
答案 2 :(得分:0)
试
Bill bill = (Bill) a.getItemAtPosition(position);