java - Android-获取customListview中第一行的值(共享首选项)

标签 java android null android-custom-view

我有一个自定义 ListView ,每行都有一个编辑文本。但是在下一个 Activity 中单击按钮时,我想获取编辑文本的所有值并作为电子邮件发送。但是我只获取第一行的值。如何获取所有行中编辑文本的值?。我的 Activity 是 add2cart.java 。适配器是 cartadapter.java 并且我有 中的按钮buy_ltr.java。我将编辑文本的值保存在 cartAdapter.java 的共享首选项中。并在 itz Activity 中获取该值并将其作为 bundle 传递给 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();

关于java - Android-获取customListview中第一行的值(共享首选项),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28424294/

相关文章:

java - 执行字符串作为方法?

android - HTTP Post 请求在真实设备上工作而不在 Android 模拟器上工作

sql - 难以定义分区的平均值

android - 应用程序从已签名的 APK 而不是 Android Studio 崩溃

java - 我需要我的 textView 在一行中适合屏幕。 android 中是否有类似 autoResize 的功能?

mysql - SQL : Replacing NULL with 0 in monthly count table

list - 使用 golang 的列表中最后一个元素的第 Kth

java - Hibernate Envers - 获取日期之间的修订和对象

java - 合并从 <util :properties> with Properties defined from <props> element 加载的属性

java - ZipEntry.STORED 对于已经压缩的文件?