gpt4 book ai didi

java - Android-从 bundle 访问字符串到自定义适配器类

转载 作者:行者123 更新时间:2023-12-02 05:03:08 25 4
gpt4 key购买 nike

我是 Android 新手。我是第一次使用 CustomAdapter。我有一个 Activity 及其相应的 customAdapter 类。我的疑问是如何访问

String sme= bundl.getString("dtls");

从 add2cart.java 到 CartAdapter 类。我尝试声明

Bundle bundl = getIntent().getExtras();
String sme= bundl.getString("dtls");

在 CartAdapter 类中。但是 getIntent()

出错

add2cart.java

public class add2cart extends Activity{
public static ListView adlstvw;
Button btn,remove_btn;
SQLiteDatabase mydb;
public String sme,bl;
@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();

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+"'", 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);
}
}

CartAdapter.java

public class CartAdapter extends BaseAdapter{


private final String[] pname;
private final String[] price;
private Context cntxt;
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+"'");

Cursor cr = mydb.rawQuery("SELECT * FROM add2cart", 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);

}
});
}
}

最佳答案

希望您通过传递包来从某个 Activity 调用 add2cart Activity 。检查该 Activity 是否正确创建了包。要将值“sme”传递给 CartAdapter.java,可以将其声明为静态字符串并使用“add2cart.sme”在 CartAdapter 中获取它,或者将其传递到 CartAdapter 的构造函数中

public add2cart extends Activity{

public static String sme;

}

public CartAdapter extends BaseAdapter{

String some = add2cart.sme ;

}

OR

public add2cart extends Activity{
String sme;

//pass it in constructor

CartAdapter adapter = new CartAdapter(this,pname,price, sme);
adlstvw.setAdapter(adapter);

}

//fetch it in CartAdapter constructor

关于java - Android-从 bundle 访问字符串到自定义适配器类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28059940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com