2016-05-09 72 views
-1

所以我的问题是我创建了一个ListView和使用简单的游标适配器填充它。所以,现在当我点击我的列表视图中的一个项目时,它会将我带到另一个活动,该活动假设向我显示所点击的项目的详细信息。关于这方面的最佳方法是什么?这是我填充列表视图的功能。我应该发送什么到下一个活动?我正在考虑发送职位,但之后那不起作用,因为在下一个活动中,我将不得不使用职位访问数据库,但这不准确,因为数据库可能删除了行,这可能会返回一行不同的数据。任何想法都会得到真正的赞赏。填充与列表视图从数据库

private void populateListView(){ 

    Cursor cursor = myDatabase.getAllData(); 
    String[] fromfieldNames = new String[]{StudentDBOpenHelper.ITEM_NAME_COLUMN}; 
    int[] toViewIDs = new int[] {R.id.textView_itemName}; 
    SimpleCursorAdapter myCursorAdapter; 
    myCursorAdapter = new SimpleCursorAdapter(getActivity(), 
      R.layout.indvidualview_layout,cursor,fromfieldNames,toViewIDs,0); 
    ListView myList = (ListView) getActivity().findViewById(R.id.courseListXML); 
    myList.setAdapter(myCursorAdapter); 


    myList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 


      Intent nextActivity = new Intent(getActivity(), CourseWorkItemActivity.class); 

      nextActivity.putExtra("Item", position); 

      startActivity(nextActivity); 

     } 
    }); 
} 

回答

1

我建议发送数据,从游标中提取作为意图的额外。下面是一个例子(更复杂一点,你想要什么,作为Itemclick我显示一个中间对话框,选择编辑或股票(地方产品进入商店)选项的过道: - !

productlist_csr = shopperdb.getProductsAsCursor(); 
     currentpca = new ProductsCursorAdapter(this, productlist_csr, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); 
     productlistview.setAdapter(currentpca); 

     productlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       AlertDialog.Builder okdialog = new AlertDialog.Builder(productlistview_context); 
       okdialog.setTitle(R.string.productlistentryclicktitle); 
       okdialog.setMessage(R.string.productlistentryclickmessage001); 
       okdialog.setCancelable(true); 
       final int pos = position; 
       okdialog.setNegativeButton(R.string.standardstockproductlist, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         Intent intent = new Intent(productlistview_context, AddProductToShopActivity.class); 
         productlist_csr.moveToPosition(pos); 
         intent.putExtra("Caller", THIS_ACTIVITY); 
         intent.putExtra("PRODUCTID", productlist_csr.getLong(ShopperDBHelper.PRODUCTS_COLUMN_ID_INDEX)); 
         intent.putExtra("ProductName", productlist_csr.getString(ShopperDBHelper.PRODUCTS_COLUMN_NAME_INDEX)); 
         intent.putExtra("ProductNotes", productlist_csr.getString(ShopperDBHelper.PRODUCTS_COLUMN_NOTES_INDEX)); 
         startActivity(intent); 
         dialog.cancel(); 
        } 
       }); 
       okdialog.setPositiveButton(R.string.standardedittext, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         Intent intent = new Intent(productlistview_context, ProductAddActivity.class); 
         intent.putExtra("Caller", THIS_ACTIVITY + "Update"); 
         productlist_csr.moveToPosition(pos); 
         intent.putExtra("ProductName", productlist_csr.getString(ShopperDBHelper.PRODUCTS_COLUMN_NAME_INDEX)); 
         intent.putExtra("ProductNotes", productlist_csr.getString(ShopperDBHelper.PRODUCTS_COLUMN_NOTES_INDEX)); 
         intent.putExtra("PRODUCTID", productlist_csr.getLong(ShopperDBHelper.PRODUCTS_COLUMN_ID_INDEX)); 
         startActivity(intent); 
         dialog.cancel(); 
        } 
       }); 
       okdialog.setNeutralButton(R.string.standardbacktext, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.cancel(); 
        } 
       }); 
       okdialog.show(); 
      } 
     }); 

ShopperDBHelper.PRODUCTS_COLUMN_?????_INDEX相当于各列的偏移量光标。我使用的意图额外来电通知主叫活动的启动活动(例如股票可以从产品,商店或过道被调用),以便它可以采取相应的行动。

在上述position用于向移动到相应的光标行(但是,我相信这不是实际需要的,因为光标已经被定位,但是这样做只是为了防止)。

我还使用自定义光标但是这不应该,我相信,是一个问题(我从来没有用过simpleCursor)。

+0

我会试试这个!谢谢。 – Carlitos

+1

有一点需要注意,就是从开始活动的回报,如果该活动改变了数据库,你应该重做查询和重置适配器通过'changeCursor','swapCursor'或'notifyDataSetCahnged'使用新的/修改的游标(我已经使用swapCursor并在onresume方法中执行此操作)。例如'currentproductsperaisleecursor = shopperdb.getProductsperAisle(currentaisleid); //交换该光标 current_productsperaislecursoradapter.swapCursor(currentproductsperaisleecursor);' – MikeT