Android-获取自定义列表视图中第一行的值(共享首选项)

Android-getting value of first row in customListview(shared preference)

我有一个 customListview,每个 row.But 中都有一个编辑文本,在接下来 activity 中单击按钮时,我想获取编辑文本的所有值并作为 email.But 发送,我是 仅获取第一行的值。如何获取所有行中编辑文本的值?。我的activity是add2cart.java .adapter 是 cartadapter.java 并且我在 buy_ltr.java 中有按钮。我正在共享首选项中保存编辑文本的值在 cartAdapter.java。并在 itz activity 中获取该值并作为一个包传递给 buy_ltr.java

已编辑

CartAdapter.java

public class CartAdapter extends BaseAdapter{


private final String[] pname;
private final String[] price;
String s2;
String abc=add2cart.sme;
String cba=add2cart.dl;
private Context cntxt;
SharedPreferences sp;
SharedPreferences.Editor spe;


 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+"' AND (usr='"+cba+"' OR usr='"+abc+"')");

        Cursor cr = mydb.rawQuery("SELECT * FROM add2cart WHERE usr='"+cba+"' OR usr='"+abc+"'", 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);

    }
    });
 TextView nametxt=(TextView)List.findViewById(R.id.prdt_nme);
 final TextView pricetxt=(TextView)List.findViewById(R.id.prdt_rate);
 final TextView totltxt=(TextView)List.findViewById(R.id.prdt_totl);
 final EditText edittext=(EditText)List.findViewById(R.id.prdt_qnty);
 edittext.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub


        sp=PreferenceManager.getDefaultSharedPreferences(cntxt);
        spe=sp.edit();

        String s1=pricetxt.getText().toString();

   ///////////s2 holds value of edit text////////
        s2=edittext.getText().toString();

        spe.putString("quty", s2);
        spe.commit();

        int i1=0;
        if (!s1.equals("")) {
        i1=Integer.parseInt(s1);    
        }

        int i2=0;
        if (!s2.equals("")) {
        i2=Integer.parseInt(s2);    
        }

        int res=i1*i2;
        totltxt.setText(Integer.toString(res));

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }
});

 nametxt.setText(pname[position].toString());
 pricetxt.setText(price[position]);

    return List;
}


}

add2cart.java

public class add2cart extends Activity{
public static ListView adlstvw;
Button btn,remove_btn;
SQLiteDatabase mydb;
public static String sme,dl;
public String bl;


SharedPreferences sp;
SharedPreferences.Editor spe;
String qty;

@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();
    sp=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    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


    ////////////////////// getting value of edit text////////// 
             if(sp!=null){
             qty=sp.getString("quty", null);
             }

            Intent in=new Intent(add2cart.this,buy_ltr.class); 
            Bundle bod=new Bundle();
            Bundle bndl = new Bundle();
        ///////passing value of edit text to next activity//////////    

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

        }
    });

}

}

buy_ltr.java

String prqty=qbdl.getString("prqt");
btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

 Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
              String[] recipients = new String[]{"my@email.com", "",};
              emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
              emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "customer Order");
                 emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"name:"+edt1.getText().toString()+"\n"+"address:"+edt2.getText().toString()+"\n"+"city:"+edt3.getText().toString()+"\n"+"pin:"+edt4.getText().toString()+"\n"+"products:"+productnames+"\n"+"quantity:"+prqty);
              emailIntent.setType("text/plain");
              startActivity(Intent.createChooser(emailIntent, "Send mail..."));
              finish();

        }
    });

commit()方法也需要调用保存SharedPreferences

中的值
s2=edittext.getText().toString();
spe.putString("quty", s2);

spe.commit();
//OR if Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD
spe.apply();