2017-02-06 4 views
-2

リストビューから値を取得する必要がありますが、まだ解決策を見つけることができません。リストビューをテーブルのように作成し、行/ 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。リストビュー

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"); 

      } 
     }); 

機能getListBill(4.2 Index.javaスニペットの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; 
    } 

答えて

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. 
+0

を試してみてください:エラー:(83、43)エラー:互換性のない型:オブジェクトがビル – Gabriel

+0

に変換することはできません、それは働きます!ありがとう! @Piyushそれは私の前に夢中になる! – Gabriel

+0

@BayuWibawa素晴らしい!お楽しみください.. – Piyush

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(); 
      } 
    }); 
+0

私はこれを試してみましたが、エラーが発生しました:java.lang.NullPointerException:nullオブジェクト参照で仮想メソッド 'void android.widget.TextView.setText(int)'を呼び出そうとしました ここでスニッパー: Bill billsData =(Bill)a.getItemAtPosition(position); 文字列bill_no = billsData.get_bill_no(); Log.d(TAG、クリック課金ID: "+ bill_no); 何が間違っていましたか? – Gabriel

+0

BillListAdapter ba =新しいBillListAdapter(this、list_bill); listview_bill.setAdapter(ba); クリックしてみると好きです。 Bill billsData = ba.getItem(position); – Rasel

0

私にエラーを与えて

Bill bill = (Bill) a.getItemAtPosition(position); 
関連する問題