-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