如何获取点击列表项的值

时间:2015-10-06 18:10:29

标签: android

我只想获取点击列表项的值, 即如果列表包含apple,ball,cat,然后单击apple然后它给出值为apple,在下面的代码中我使用内部类基适配器来显示数据。Please check this image contains list

public class RecordsActivity  extends Activity implements   
OnItemClickListener
{

ListView listProduct;
ListAdapter adapter;
TextView UserName,Date;

ArrayList<String> billDate = new ArrayList<String>();
ArrayList<String> billNo = new ArrayList<String>();
ArrayList<String >partyName=new ArrayList<String>();
ArrayList<String> billAmount = new ArrayList<String>();
ArrayList<String >receipt=new ArrayList<String>();
ArrayList<String >balance=new ArrayList<String>();
ArrayList<String> week = new ArrayList<String>();
ArrayList<String> amount = new ArrayList<String>();

String get_name,get_date,Name;
String GetAmnt;
Dialog dialog;
AlertDialog alertDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_records);
    UserName=(TextView) findViewById(R.id.txt_disuser);
    Intent usr=getIntent();
    get_name=usr.getStringExtra("user");
    UserName.setText(get_name);

    Date=(TextView) findViewById(R.id.txt_disdate);
    Intent dt=getIntent();
    get_date=dt.getStringExtra("date");
    Date.setText(get_date);

    Intent id =getIntent();
    Name=id.getStringExtra("name");



    listProduct=(ListView) findViewById(R.id.ListViewFilledRecrd);
    listProduct.setAdapter(adapter);
    /*listProduct.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3)
        {
            listProduct.setClickable(true);
            String data = (String)listProduct.getItemAtPosition(arg2);
            Toast.makeText(getApplicationContext(),data,   
Toast.LENGTH_SHORT).show();

        }
    });*/



 }



protected void onResume()
{
    getAllProduct();
    super.onResume();
 }

public void getAllProduct(){

    SQLiteDatabase db = this.getDB();
    Cursor c;

        //c=db.rawQuery("SELECT * FROM RECORDS", null);
        c=db.rawQuery("SELECT * FROM RECORDS WHERE PARTY_NAME = '" + Name +    
 "' ", null);
        String   
   BILL_DATE,BILL_NO,PARTY_NAME,BILL_AMOUNT,RECEIPT,BALANCE,WEEK,AMOUNT;

        if(c.moveToFirst())
        {
            do
            {           

                BILL_DATE = c.getString(1);
                BILL_NO=c.getString(2);
                PARTY_NAME=c.getString(3);
                BILL_AMOUNT=c.getString(4);
                RECEIPT=c.getString(5);
                BALANCE=c.getString(6);
                WEEK=c.getString(7);
                AMOUNT=c.getString(8);


                billDate.add(BILL_DATE);
                billNo.add(BILL_NO);
                partyName.add(PARTY_NAME);
                billAmount.add(BILL_AMOUNT);
                receipt.add("RECEIPT");
                balance.add(BALANCE);
                week.add(WEEK);
                amount.add(AMOUNT);

              } while (c.moveToNext());

              }

                DisplyRecords dsptProd=new 

       DisplyRecords(RecordsActivity.this,billDate,billNo,partyName,
       billAmount,receipt,balance,week,amount);
                listProduct.setAdapter(dsptProd);
                listProduct.setOnItemClickListener(this);
                c.close();
                db.close();
           }

  private SQLiteDatabase getDB()
  {
      String DB_NAME = "odsr.db";
     return openOrCreateDatabase(DB_NAME, SQLiteDatabase.OPEN_READWRITE,  
     null);
  }



@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO Auto-generated method stub

    }
 }




class DisplyRecords extends BaseAdapter
{

Context c;  

ArrayList<String > BILLDATE;
ArrayList<String>BILLNO;
ArrayList<String> PARTYNAME;
ArrayList<String>BILLAMOUNT;
ArrayList<String>RECEIPT;
ArrayList<String> BALANCE;
ArrayList<String>WEEK;
ArrayList<String>AMOUNT;

public DisplyRecords(RecordsActivity recordsactivity,ArrayList<String>       
billDate, ArrayList<String> billNo, ArrayList<String>    
partyName,ArrayList<String> billAmount, ArrayList<String> receipt, 
ArrayList<String> balance, ArrayList<String> week,ArrayList<String> amount)
{

this.c=recordsactivity;
this.BILLDATE=billDate;
this.BILLNO=billNo;
this.PARTYNAME=partyName;
this.BILLAMOUNT=billAmount;
this.RECEIPT=receipt;
this.BALANCE=balance;
this.WEEK=week;
this.AMOUNT=amount;

     //TODO Auto-generated constructor stub
}

@Override
public int getCount() {
    // TODO Auto-generated method stub

    return BILLDATE.size();
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(final int pos, View row, ViewGroup parent)
{

    View child=row;
    LayoutInflater layoutinflater;
    if(child==null)
    {
        layoutinflater=(LayoutInflater)   
      c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        child = layoutinflater.inflate(R.layout.list_product,null);


    }
    TextView txt_billdate=(TextView) child.findViewById(R.id.txt_bill_date);
    TextView txt_billno=(TextView) child.findViewById(R.id.txt_bill_no);
    TextView txt_partyname=(TextView)   
    child.findViewById(R.id.txt_party_name);
    TextView txt_billamt=(TextView)   

     child.findViewById(R.id.txt_bill_amount);
     TextView txt_receipt=(TextView) child.findViewById(R.id.txt_receipt);
    TextView txt_balance=(TextView) child.findViewById(R.id.txt_balance);
    TextView txt_wk=(TextView) child.findViewById(R.id.txt_week);
    TextView txt_amount=(TextView) child.findViewById(R.id.txt_amount);

    txt_billdate.setText(BILLDATE.get(pos));
    txt_billno.setText(BILLNO.get(pos));
    txt_partyname.setText(PARTYNAME.get(pos));
    txt_billamt.setText(BILLAMOUNT.get(pos));
    txt_receipt.setText(RECEIPT.get(pos));
    txt_balance.setText(BALANCE.get(pos));
    txt_wk.setText(WEEK.get(pos));
    txt_amount.setText(AMOUNT.get(pos));




    // TODO Auto-generated method stub
    return child;

}


    }

1 个答案:

答案 0 :(得分:1)

这里有一些假设,你的实际字段不需要是ArrayList's,因为每个查询都从db中返回一个单行项,我用字符串创建一个主对象来表示数据......

所以我要这样做,你需要重新构建你的数据

首先,我会将您的数据更改为:

public class ParentObject {

    public String BILLDATE ;
    public String BILLNO ;
    public String PARTYNAME ;
    public String BILLAMOUNT ;
    public String RECEIPT ;
    public String BALANCE ;
    public String WEEK ;
    public String AMOUNT ;
}

<强>更新 然后我会将我的活动改为:

public class RecordsActivity  extends Activity implements  OnItemClickListener{

ListView listProduct;
ListAdapter adapter;
TextView UserName,Date;

// your new parent object ArrayList, much cleaner 
List<ParentObject> parentObjects; 

String get_name,get_date,Name;
String GetAmnt;
Dialog dialog;
AlertDialog alertDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
     // TODO Auto-generated method stub
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_records);
     UserName=(TextView) findViewById(R.id.txt_disuser);
     Intent usr=getIntent();
     get_name=usr.getStringExtra("user");
     UserName.setText(get_name);

     Date=(TextView) findViewById(R.id.txt_disdate);
     Intent dt=getIntent();
     get_date=dt.getStringExtra("date");
     Date.setText(get_date);

     Intent id =getIntent();
     Name=id.getStringExtra("name");

     listProduct=(ListView) findViewById(R.id.ListViewFilledRecrd);
     listProduct.setAdapter(adapter);
     listProduct.setOnItemClickListener(this);
 }

 protected void onResume(){
       getAllProduct();
      super.onResume();
 }

 public void getAllProduct(){
     SQLiteDatabase db = this.getDB();
     Cursor c;

    //c=db.rawQuery("SELECT * FROM RECORDS", null);
    c=db.rawQuery("SELECT * FROM RECORDS WHERE PARTY_NAME = '" + Name + "' ", null);
    String BILL_DATE,BILL_NO,PARTY_NAME,BILL_AMOUNT,RECEIPT,BALANCE,WEEK,AMOUNT;

    if(c.moveToFirst()){
        // initialize the parent arrayList
        parentObjects = new ArrayList<ParentObject>();

        do{           
           // initialize a single object
           ParentObject parentObject = new ParentObject();

           // fill in its data
            parentObject.BILL_DATE = c.getString(1);
            parentObject.BILL_NO=c.getString(2);
            parentObject.PARTY_NAME=c.getString(3);
            parentObject.BILL_AMOUNT=c.getString(4);
            parentObject.RECEIPT=c.getString(5);
            parentObject.BALANCE=c.getString(6);
            parentObject.WEEK=c.getString(7);
            parentObject.AMOUNT=c.getString(8);

            // add it to your list
            parentObjects.add(parentObject);

          } while (c.moveToNext());

          }

            // pass the List<ParentObject> into your adapter
            DisplyRecords dsptProd= new DisplyRecords(RecordsActivity.this, (ArrayList<ParentObject>)parentObjects);
            listProduct.setAdapter(dsptProd);
            listProduct.setOnItemClickListener(this);
            c.close();
            db.close();
       }

 private SQLiteDatabase getDB(){
     String DB_NAME = "odsr.db";
     return openOrCreateDatabase(DB_NAME, SQLiteDatabase.OPEN_READWRITE,  
    null);
 }

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
    long id) {
    // TODO Auto-generated method stub
        // access to all your data through the parentObject
             ParentObject data = (ParentObject) listProduct.getItemAtPosition(position);

        // access all fields for this parent

        Toast.makeText(RecordsActivity.this, "data: "+ data.BILL_DATE, + " ," + data.BILL_NO, " , etc", Toast.LENGTH_LONG).show();
    }
}

然后更新您的适配器以处理列表

class DisplyRecords extends BaseAdapter {

     Context c;  
     List<ParentObject> parentObjects;

     public DisplyRecords(Context context, ArrayList<ParentObject> parentObjects){

        this.c=context;
        this.parentObjects = parentObjects;
     }

     @Override
     public int getCount() {
      // TODO Auto-generated method stub

      return parentObjects.size();
     }

     @Override
     public Object getItem(int position) {
        return parentObjects.get(position); // always return this parent
     }

     @Override
     public long getItemId(int position) {
      // TODO Auto-generated method stub
        return position;
     }

     @Override
     public View getView(final int pos, View row, ViewGroup parent){


         // You should implement the ViewHolder pattern, this is not doing that...
         View child=row;
         LayoutInflater layoutinflater;
         if(child==null){
              layoutinflater=(LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              child = layoutinflater.inflate(R.layout.list_product,null);
         }
        TextView txt_billdate=(TextView) child.findViewById(R.id.txt_bill_date);
        TextView txt_billno=(TextView) child.findViewById(R.id.txt_bill_no);
        TextView txt_partyname=(TextView) child.findViewById(R.id.txt_party_name);
        TextView txt_billamt=(TextView) child.findViewById(R.id.txt_bill_amount);
        TextView txt_receipt=(TextView) child.findViewById(R.id.txt_receipt);
        TextView txt_balance=(TextView) child.findViewById(R.id.txt_balance);
        TextView txt_wk=(TextView) child.findViewById(R.id.txt_week);
        TextView txt_amount=(TextView) child.findViewById(R.id.txt_amount);

        ParentObject parentObject = parentObjects.get(pos);

        txt_billdate.setText(parentObject.BILLDATE);
        txt_billno.setText(parentObject.BILLNO.);
        txt_partyname.setText(parentObject.PARTYNAME);
        txt_billamt.setText(parentObject.BILLAMOUNT);
        txt_receipt.setText(parentObject.RECEIPT);
        txt_balance.setText(parentObject.BALANCE);
        txt_wk.setText(parentObject.WEEK);
        txt_amount.setText(parentObject.AMOUNT);

        // TODO Auto-generated method stub
    return child;
    }
}