使用android中的alertbox弹出窗口从gridview更新数据库

时间:2012-10-05 04:34:28

标签: android android-alertdialog android-gridview

我正在使用Android中的网格视图为商店开发客户详细信息。我使用我的数据库获取了客户的详细信息。我需要使用警报框更新客户处理的股票,以便在执行交易时输入库存数量。

customergrid.xml

  <?xml version="1.0" encoding="utf-8"?>

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/tab1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical" >

      <GridView
          android:id="@+id/grid"
          android:layout_width="fill_parent"
          android:layout_height="357dp"
          android:numColumns="1"
          android:stretchMode="columnWidth" />

      <Button
          android:id="@+id/cancel"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="cancel" />

  </LinearLayout>

并且,对于每一行,我使用了自定义的Xml文件..

customerrow.xml

 <?xml version="1.0" encoding="utf-8"?>
 <TableLayout  xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" >
     <TableRow>
         <TextView
             android:layout_width="50px"
             android:layout_height="wrap_content"
             android:id="@+id/row_ID"
             android:padding="5px"
             android:layout_weight="1" />
         <TextView 
             android:layout_width="50px"
             android:layout_height="wrap_content"
             android:id="@+id/key_ID"
             android:padding="5px"
             android:layout_weight="1" />
         <TextView 
             android:layout_width="50px"
             android:layout_height="wrap_content"
             android:id="@+id/key_Description"
             android:padding="5px"
             android:layout_weight="1" />
     </TableRow>
 </TableLayout>

我在Oncreate()方法上使用了customergrid.xml,并在Grid创建中使用了customerrow.xml,如下面的代码所示。

 public void FillGrid() {
     DatabaseHelper sqdb = new DatabaseHelper(this);
     sqdb.openDataBase();
     Cursor cursor;
     GridView grid = (GridView) findViewById(R.id.grvData);
     cursor = sqdb.getGridData();
     sqdb.close();
     if (cursor != null) {
         startManagingCursor(cursor); 
         SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
             R.layout.customerrow, cursor, new String[] { CustomerActivity.KEY_ROW_ID,
             CustomerActivity.KEY_ID, CustomerActivity.KEY_DESCRIPTION }, new int[] {
             R.id.txtGrv_id, R.id.txtGrvid, R.id.txtGrvDescription } );
         adapter.setViewResource(R.layout.gridlayout);
         grid.setAdapter(adapter);
 }

最后,我使用带有编辑框的警报框来获取客户库存数量的输入。我用它来做这个任务..

grid.setOnItemClickListener(new  OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) {
        AlertDialog.Builder b=new AlertDialog.Builder(MarkSheet.this);
        b.setTitle("Employee Details");
        LayoutInflater li=LayoutInflater.from(MarkSheet.this);
        View v=li.inflate(R.layout.dialog, null);
        b.setIcon(android.R.drawable.ic_input_get);
        b.setView(v);
        final TextView txtName=(TextView)v.findViewById(R.id.txtName);
        final EditText Quantity=(EditText)v.findViewById(R.id.Quantity);

        // Here I need to update the table while clicking the positive button, if I click negative button I need to cancel the Dialog box..

     }
 });

任何人帮我完成这项任务..

2 个答案:

答案 0 :(得分:1)

尝试此链接,它可能会帮助您..

  

http://www.mkyong.com/android/android-gridview-example/

答案 1 :(得分:0)

您可以找到更新数据库表here的代码和警告框here的概念

您需要从单击“ok”按钮的代码中调用sqlite的update方法。