2013-06-29 130 views
0

我正在從基礎適配器切換到cursorAdapter。所以,我建立了我的CursorAdapter類Android CursorAdapter ListView未填充

public class OrderListAdapterCursor extends CursorAdapter{ 
private GetInventoryColor getInvColor = new GetInventoryColor(); 

public OrderListAdapterCursor(Context context, Cursor c, String x) { 
    super(context, c); 
    // TODO Auto-generated constructor stub 
} 


@Override 
public void bindView(View vi, final Context context, Cursor cursor) { 
    // TODO Auto-generated method stub 
    final ViewHolder holder = new ViewHolder(); 
    holder.thumb_image =(ImageView)vi.findViewById(R.id.ivOrderPicture); // thumb image 
    holder.tvmainOrder= (TextView)vi.findViewById(R.id.tvMainOrder); // titleOrder 
    holder.tvPrice = (TextView)vi.findViewById(R.id.tvOrderPrice); // price 
    holder.tvItemID = (TextView)vi.findViewById(R.id.tvItemID); // itemID 
    holder.tvItemCode = (TextView)vi.findViewById(R.id.tvItemCode); // item Code 

    holder.myLinearLayout = (LinearLayout) vi.findViewById(R.id.myLinearLayout); 
    holder.mylinearForRed = (LinearLayout)vi.findViewById(R.id.mylinearForRed); 
    holder.btnQuantity = (TextView)vi.findViewById(R.id.tvQuantity); 

    holder.tvmainOrder.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(0)))); 
     //tvmainOrder.setTypeface(modGen.typeFaceArial); 
    holder.tvItemID.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2)))); 
     //tvItemID.setTypeface(modGen.typeFaceArial); 
    holder.tvItemCode.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1)))); 
     //tvItemCode.setTypeface(modGen.typeFaceArial); 
    holder.tvPrice.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(3)))); 
    holder.btnQuantity.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(4)))); 

     //btnQuantity.setTag(position); 
     //btnInfo.setTag(position); 

    holder.thumb_image.setTag(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2)))); 
    holder.thumb_image.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       Object as = holder.thumb_image.getTag(); 
       final Integer myPosition = Integer.valueOf(as.toString()); 
       Toast.makeText(context, ""+myPosition, Toast.LENGTH_LONG).show(); 
       //String myItemID = data.get(myPosition).orderitemID; 
       //((TransactionPage) mainContext).ShowProductCatalogItem(myItemID, "0"); 
      } 
     }); 

     // String background = data.get(position).orderColor; 
     String background = getInvColor.getItemIDColor(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2))), context); 

     if (background.equalsIgnoreCase("RED")){ 
      holder.myLinearLayout.setBackgroundColor(Color.RED); 
     } 
     else if (background.equalsIgnoreCase("YELLOW")){ 
      holder.myLinearLayout.setBackgroundColor(Color.YELLOW); 
     } 
     else 
     { 
      holder.myLinearLayout.setBackgroundColor(Color.GREEN); 
     } 


     //tvStocksQty.setText(data.get(position).stocksQty); 
     LinearLayout mylinearForRed = (LinearLayout)vi.findViewById(R.id.mylinearForRed); 
     String qty1 = cursor.getString(cursor.getColumnIndex(cursor.getColumnName(4))); 

      if (qty1 == null ||qty1.equalsIgnoreCase("")){ 
       mylinearForRed.setVisibility(View.INVISIBLE); 
       //int resmyID =R.drawable.ribbutton_counter; 
       //mylinearForRed.setBackgroundResource(resmyID); 
      }else{ 
       mylinearForRed.setVisibility(View.VISIBLE); 
       //int resmyID =R.drawable.ribbutton_counter; 
       //mylinearForRed.setBackgroundResource(resmyID); 
      } 

} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 
    View retView = inflater.inflate(R.layout.order_list_style, parent, false); 
    return retView; 
} 

    static class ViewHolder 
    { 
     public ImageView thumb_image; 
     public TextView tvmainOrder; 
     public TextView tvPrice; 
     public TextView tvItemID; 
     public TextView tvItemCode; 
     public TextView btnQuantity; 
     public LinearLayout myLinearLayout; 
     public LinearLayout mylinearForRed; 
    } 

    } 

當我是新來這個光標適配器使用此代碼和查詢

selectQuery = "SELECT item_tb.dDesc, item_tb.itemcode, item_tb.ID as _id, price_tb.dPrice, transaction_tb.qtyOrdered FROM item_tb " + 
       "INNER JOIN price_tb ON item_tb.ID = price_tb.itemID " + 
       "INNER JOIN category_tb ON category_tb.ID = item_tb.categoryID " + 
       "LEFT JOIN transaction_tb ON item_tb.ID = transaction_tb.itemID " + 
       "WHERE category_tb.dDesc LIKE '"+ category +"%' " + 
       "AND (item_tb.dDesc LIKE '%" + sort +"%' OR " + 
       "item_tb.itemCode LIKE '" + sort + "%') " + 
       "AND item_tb.isPublish = '1'" + 
       "ORDER BY item_tb.dDesc ASC"; 

    mySQLiteAdapter = new SQLiteAdapter(context); 
    mySQLiteAdapter.openToRead(); 

    final Cursor cursor =mySQLiteAdapter.read(selectQuery); 

    //new Handler().post(new Runnable() { 
    // public void run() { 

      orderlistcursor= new OrderListAdapterCursor(context, cursor, sort); 
      listViewSearchPanel.setAdapter(orderlistcursor); 
      cursor.close(); 
      mySQLiteAdapter.close(); 

對不起結合它。我很確定遊標有數據,因爲使用基本適配器包含900多個數據。我的問題是爲什麼,這個listview沒有綁定。

有什麼我錯過?

謝謝你們

+1

我看到你關閉光標後,分配它可能有一些問題? – DevZer0

+0

w8先生讓我檢查 – Androyds

+0

@ DevZer0的工作,但先生,我們必須關閉遊標權? – Androyds

回答

2

你的問題,是因爲你關閉cursor將其分配給適配器之後,

orderlistcursor= new OrderListAdapterCursor(context, cursor, sort); 
listViewSearchPanel.setAdapter(orderlistcursor); 
cursor.close(); 
mySQLiteAdapter.close(); 

破壞光標在後一階段onces你使用它,在一個onDestroy()方法來完成。

+0

是這樣的問題 – Androyds

4

你不應該在setAdapter之後關閉遊標。使光標成爲Activity的成員變量,並在Activity的onDestroy()方法中關閉它。