2016-05-09 69 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