从bundle到customadapter类的Android-Access字符串

时间:2015-01-21 05:00:33

标签: java android android-adapter baseadapter listadapter

我是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);

    }
    });
}
}

1 个答案:

答案 0 :(得分:2)

希望您通过传递bundle来调用某些活动的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