如何从gridview中的edittext获取值

时间:2013-11-20 12:11:00

标签: android gridview android-edittext

我在项目中使用了网格视图

我的活动布局文件

<GridView
    android:id="@+id/gridView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="start"
    android:numColumns="1"
    android:stretchMode="spacingWidth"
    android:layout_below="@+id/spinner1"
    android:layout_marginLeft="20dip"
    android:layout_marginRight="10dip"
    android:layout_marginTop="20dip"
    android:visibility="visible"    >
    </GridView>

我的网格布局(grid_change_prices.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="320dp"
    android:layout_height="match_parent"
    android:clickable="false"
>
     <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:background="#ff0000" >


        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="1dip"
            android:background="#ffffff"
            android:paddingTop="0dip" >

            <TextView
        android:id="@+id/cplabel"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:paddingLeft="5dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

   <EditText
       android:id="@+id/cpprice"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:hint="@string/add_price"
       android:layout_marginTop="0dp"
       android:textSize="12dp"
       android:inputType="number" />
      </TableRow>
    </TableLayout>


</RelativeLayout>

我的模拟器输出 检查此网址 http://i.stack.imgur.com/Uc6cs.png

我的代码文件

public void fillgrid(int categoryid,String car){
gridView = (GridView) findViewById(R.id.gridView);
final DatabaseClass2 db = new DatabaseClass2(this);
    db.openDataBase();
Cursor c = db.getprice(categoryid); 
startManagingCursor(c);
String[] fromColumns = {"_id",DatabaseClass2.colPrice};
int[] toViews = {R.id.cplabel, R.id.cpprice};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
        R.layout.grid_change_prices, c, fromColumns, toViews);
        //c.moveToNext();
gridView.setAdapter(adapter); }

现在我的查询是 当我单击修改按钮时,我想用新值更新我的数据库 我试图通过使用.gettext()从编辑文本中获取值。 但它只返回第一个edittext的值

我如何从其他edittexts获取值,也包含行ID 所以我用相同设计的新价格更新数据库..?

2 个答案:

答案 0 :(得分:1)

为什么使用GridView进行如此简单的工作? 如果你使用ListView更好,那么填充列表和从其他EditText获取值会更容易。

我可以给你一个想法......只要看看这段代码我在做什么:

public class RecordExpenseAdapter extends ArrayAdapter<HashMap<String, String>>
    {

        Intent intent;
        Context context;
        int layoutResourceId;
        ArrayList<HashMap<String, String>> RecordList = new ArrayList<HashMap<String, String>>();           
        public RecordExpenseAdapter(Context context, int layoutResourceId,ArrayList<HashMap<String, String>> RecordList) 
        {
            super(context, layoutResourceId, RecordList);
            this.layoutResourceId = layoutResourceId;
            this.context = context;
            this.RecordList = RecordList;
            this.intent = new Intent(context, ScanReceipt.class);
            //Log.d("ASD:","comp intialize");
       }    

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            ImageHolder holder = null;

            try{


        if(row == null)
        {
        Log.d("ASD:","insdie getview");
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new ImageHolder();
        holder.categoryName = (TextView)row.findViewById(R.id.textView1);
        holder.date = (TextView)row.findViewById(R.id.textView2);
        holder.amount = (TextView)row.findViewById(R.id.textView3);
        holder.record_rid = (TextView)row.findViewById(R.id.ridRecord);     
        holder.cid=(TextView)row.findViewById(R.id.cid);
        holder.vendor=(TextView)row.findViewById(R.id.vendor);
        holder.description=(TextView)row.findViewById(R.id.desc);
        holder.attachURL=(TextView)row.findViewById(R.id.userFile);
        holder.attachImage=(ImageView)row.findViewById(R.id.attachment);
        holder.scanReceipt = (ImageView)row.findViewById(R.id.imageView2);

        row.setTag(holder);
        holder.scanReceipt.setTag(position);

        holder.scanReceipt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try{
                    Log.e("Scan Receipt Button List", "1");
                    ImageView iv = (ImageView) v;
                    Log.e("Scan Receipt Button List", "2");
                    int pos = (Integer) iv.getTag();
                    Log.e("Scan Receipt Button List", "3");
                    ScanReceipt.rid = RecordList.get(pos).get("rid").toString();
                    Log.e("Scan Receipt Button List", "4");
                    //Toast.makeText(context, "Button Clicked", Toast.LENGTH_LONG).show();
                    RecordExpenseAdapter.this.doIt();
                }catch(Exception e){
                    Log.e("Error on click on Scan Receipt Button List", e.getMessage().toString());
                }
            }
        });

//      if(picture.get("userfile")=="no")
//      {
//          holder.attachImage.setVisibility(View.INVISIBLE);
//      }
//      else
//      {
//          holder.attachImage.setVisibility(View.VISIBLE);
//      }
        row.setTag(holder);

        }
        else
        {
        holder = (ImageHolder)row.getTag();
        }

        HashMap<String, String> picture = RecordList.get(position);

        holder.categoryName.setText(picture.get("category_name"));
        holder.date.setText(picture.get("date"));
        holder.amount.setText(picture.get("amount"));
        holder.record_rid.setText(picture.get("rid"));
        holder.cid.setText(picture.get("cid"));
        holder.vendor.setText(picture.get("vendor"));
        holder.description.setText(picture.get("description"));
        holder.attachURL.setText(picture.get("image"));

        Log.e("Record Expense Adapter 0 : IMAGE", picture.get("image").toString());
        Log.e("RID : " + position, picture.get("rid").toString());

    //  Log.e("error value", picture.get("userfile"));

        //Log.e("error value", "value is error");

//      if(picture.get("userfile")=="no")
//      {
//          holder.attachImage.setVisibility(View.INVISIBLE);   
//      }
//          
//      else
//      {
//          holder.attachImage.setVisibility(View.VISIBLE); 
//      }
        if(picture.get("image").toString().trim().equalsIgnoreCase("no")){
            holder.scanReceipt.setVisibility(View.VISIBLE);
            holder.attachImage.setVisibility(View.INVISIBLE);
        }else{
            holder.scanReceipt.setVisibility(View.GONE);
            holder.attachImage.setVisibility(View.VISIBLE);
        }
            }
            catch(Exception e){

            }
        return row; 
        }

public void doIt(){
    Log.e("Adapter 0", "In do It");

    context.startActivity(intent);
}

        static class ImageHolder
        {
                TextView categoryName;
                TextView date;
                TextView amount;
                TextView record_rid;
                TextView vendor;
                TextView cid;
                TextView description;
                TextView attachURL;             
                ImageView attachImage,scanReceipt;
        }

}

在上面的代码中,我在ImageView scanReceipt上实现了单击侦听器。每次都是相同的imageview,即为每一行夸大相同的布局,这意味着所有行都会为相同的id充气,但是这个点击事件仍将为不同的行执行不同的任务。

您需要做的是:只需使用EditText,因为我正在使用ImageView。在我实现点击监听器时实现TextWatcher。

你必须使用自定义列表这样说 -

List<CustomClass> list = new ArrayList<CustomClass>();

在这个自定义类中,放置公共变量,以便您可以通过CustomClass的对象访问它们,并将该对象添加到上面创建的列表中。

获得此列表中的值后,您可以按照自己的方式使用它。

我希望它会有所帮助...... :)

答案 1 :(得分:0)

@Prakash 想念家伙

现在,我将textwatcher附加到edittext字段并设法获取rowid 在这个

的帮助下
price_text.setTag(c.getPosition());

并在afterTextChanged

中检索该位置
price_text.getTag()

但是现在我怎么能保存这些价值,就像你在上面发表的那样......