Android - 从customListview获取值

时间:2015-02-09 06:04:33

标签: java android android-edittext custom-lists

我在customlistview的每一行都有一个edittext,用户输入产品数量(在adapter.java页面中)。其他值是从database设置的。我想获取按钮上每行的edittext值( Activity.java)click.I尝试使用sharedpreference.But得到空指针异常行捆绑qbdl = new Bundle();

MainActivity.java

public class add2cart extends Activity{
public static ListView adlstvw;
ListView adlstvw;
Button btn,remove_btn;
SQLiteDatabase mydb;
public static String sme,dl;
public String bl;
SharedPreferences sp;
SharedPreferences.Editor spe;
String qty=CartAdapter.s2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add2crt);
    adlstvw=(ListView)findViewById(R.id.lstvw_add2crt);
    btn=(Button)findViewById(R.id.place_order);

    Bundle bundl = getIntent().getExtras();
    Bundle bodl=getIntent().getExtras();

    if (bodl!=null) {
        dl=bodl.getString("dts");
    }


    if (bundl != null) {
     sme= bundl.getString("dtls");

     }


    mydb=add2cart.this.openOrCreateDatabase("addcart", MODE_PRIVATE, null);
    mydb.execSQL("CREATE TABLE IF NOT EXISTS add2cart(usr TEXT,img BLOB,pnme TEXT,prate NUMERIC,pqty NUMERIC,ptotl NUMERIC)");
    Cursor cr = mydb.rawQuery("SELECT * FROM add2cart WHERE usr='"+sme+"' OR usr='"+dl+"'", null);
    String [] pname = new String[cr.getCount()];
    String [] price = new String[cr.getCount()];

    int i = 0;
    while(cr.moveToNext())
    {
        String name = cr.getString(cr.getColumnIndex("pnme"));
        String prprice = cr.getString(cr.getColumnIndex("prate"));
        pname[i] = name;
        price[i] = prprice;
        i++;
    }
    CartAdapter cart=new CartAdapter(this,pname,price);
    adlstvw.setAdapter(cart);

    btn.setOnClickListener(new OnClickListener() {

         @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


            /////////////////////////
             String qty=sp.getString("quty", null);


            Intent in=new Intent(add2cart.this,buy_ltr.class); 
            Bundle bod=new Bundle();
            Bundle bndl = new Bundle();

             ////////////////
            Bundle qbdl=new Bundle();
            qbdl.putString("prqt", qty);
            in.putExtras(qbdl);


            bod.putString("kew", dl);
            in.putExtras(bod);
            bndl.putString("som",sme); 
            in.putExtras(bndl);
            startActivity(in);

        }
    });

}

}

CartAdapter.java

public class CartAdapter extends BaseAdapter{


private final String[] pname;
private final String[] price;
String s2;
String abc=add2cart.sme;
String cba=add2cart.dl;
private Context cntxt;

 /////////////////
SharedPreferences sp;
SharedPreferences.Editor spe;
 /////////////////

 public CartAdapter(Context c,String [] pname,String [] price)
 {

     cntxt=c;
     this.pname=pname;
     this.price=price;
 }
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return pname.length;
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    View List;
    LayoutInflater mLayoutInflater=(LayoutInflater)cntxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView==null) {
List=new View(cntxt);
List=mLayoutInflater.inflate(R.layout.add2crt_sub,parent, false);

 }
else {
List=(View)convertView;
}

Button button=(Button)List.findViewById(R.id.remove);
button.setTag(position);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub


        int position = (Integer)v.getTag();
        SQLiteDatabase mydb=cntxt.openOrCreateDatabase("addcart",Context.MODE_PRIVATE, null);
        String pnam = pname[position];

        mydb.execSQL("DELETE FROM add2cart WHERE pnme ='"+pnam+"' AND (usr='"+cba+"' OR usr='"+abc+"')");

        Cursor cr = mydb.rawQuery("SELECT * FROM add2cart WHERE usr='"+cba+"' OR usr='"+abc+"'", null);
        String [] pname = new String[cr.getCount()];
        String [] price = new String[cr.getCount()]; 


        int i = 0;
        while(cr.moveToNext())
        {
            String name = cr.getString(cr.getColumnIndex("pnme"));
            String prprice = cr.getString(cr.getColumnIndex("prate"));
            pname[i] = name;
            price[i] = prprice;
            i++;
        }
        CartAdapter cart=new CartAdapter(cntxt,pname,price);
        add2cart.adlstvw.setAdapter(cart);

    }
    });
 TextView nametxt=(TextView)List.findViewById(R.id.prdt_nme);
 final TextView pricetxt=(TextView)List.findViewById(R.id.prdt_rate);
 final TextView totltxt=(TextView)List.findViewById(R.id.prdt_totl);
 final EditText edittext=(EditText)List.findViewById(R.id.prdt_qnty);
 edittext.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub


          /////////////////////////
        sp=PreferenceManager.getDefaultSharedPreferences(cntxt);
        spe=sp.edit();
        String s1=pricetxt.getText().toString();
        s2=edittext.getText().toString();
        spe.putString("quty", s2);


        int i1=0;
        if (!s1.equals("")) {
        i1=Integer.parseInt(s1);    
        }

        int i2=0;
        if (!s2.equals("")) {
        i2=Integer.parseInt(s2);    
        }

        int res=i1*i2;
        totltxt.setText(Integer.toString(res));


    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub


    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }
});

 nametxt.setText(pname[position].toString());
 pricetxt.setText(price[position]);

    return List;
}


}

logcat的

02-09 04:13:50.705: E/AndroidRuntime(8978): FATAL EXCEPTION: main
02-09 04:13:50.705: E/AndroidRuntime(8978): Process: com.power.it.solar, PID: 8978
02-09 04:13:50.705: E/AndroidRuntime(8978): java.lang.NullPointerException
02-09 04:13:50.705: E/AndroidRuntime(8978):     at com.power.it.solar.add2cart$1.onClick(add2cart.java:76)
02-09 04:13:50.705: E/AndroidRuntime(8978):     at android.view.View.performClick(View.java:4424)
02-09 04:13:50.705: E/AndroidRuntime(8978):     at android.view.View$PerformClick.run(View.java:18383)
02-09 04:13:50.705: E/AndroidRuntime(8978):     at android.os.Handler.handleCallback(Handler.java:733)
02-09 04:13:50.705: E/AndroidRuntime(8978):     at android.os.Handler.dispatchMessage(Handler.java:95)
02-09 04:13:50.705: E/AndroidRuntime(8978):     at android.os.Looper.loop(Looper.java:137)
02-09 04:13:50.705: E/AndroidRuntime(8978):     at android.app.ActivityThread.main(ActivityThread.java:4998)
02-09 04:13:50.705: E/AndroidRuntime(8978):     at java.lang.reflect.Method.invokeNative(Native Method)
02-09 04:13:50.705: E/AndroidRuntime(8978):     at java.lang.reflect.Method.invoke(Method.java:515)
02-09 04:13:50.705: E/AndroidRuntime(8978):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-09 04:13:50.705: E/AndroidRuntime(8978):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-09 04:13:50.705: E/AndroidRuntime(8978):     at dalvik.system.NativeStart.main(Native Method)

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为你可以使用:

et.setTag("tagname"); 
在适配器的edittext视图上的

方法然后通过标记获取按钮侦听器的值:

if(v.gettag().equals("tagname")){
//Write your code
}