我是Android新手。我是第一次使用CustomAdapter。我有一个活动及其对应的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);

    }
    });
}
}

最佳答案

希望您通过传递捆绑从某些活动中调用add2cart活动。检查该活动是否正确创建了捆绑包。
并将值“ sme”传递给CartAdapter.java,可以将其声明为静态String并使用“ 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

10-02 00:08