-1
我在我的customlistview的每一行中都有一个edittext,用户输入产品数量(在adapter.java页面中)。其他值从database.Id中设置。我想要获取edittext的值在每行按钮(Activity.java)上单击。我尝试使用sharedpreference.But得到空指针异常,行Bundle qbdl = new Bundle();。Android - 从customListview中获取值
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
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)
其价值要访问:在您的适配器您的EditText视图
方法,然后通过标签一样得到你的按钮侦听价值在'btn'按钮上单击? – 2015-02-09 06:17:57
adepter中的名称没有意义,所以请让我知道哪个变量保持数量值? – 2015-02-09 06:33:35
使用'SharedPreferences'设置Button上的值单击 – 2015-02-09 07:15:13