Search code examples
javaandroidandroid-adapterbaseadapterlistadapter

Android-Access string from bundle to customadapter class


I am new in Android. I am working with CustomAdapter for the first time. I have an activity and its corresponding customAdapter class. My doubt is how to access the

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

from add2cart.java to CartAdapter class. I tried declaring

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

in CartAdapter class. But error in 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);

    }
    });
}
}

Solution

  • Hoping that you are calling add2cart activity from some activity by passing bundle. check in that activity whether bundle is properly created. And to pass value "sme" to CartAdapter.java, either declare it as static String and fetch it in CartAdapter using "add2cart.sme" or pass it in constructor of 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