2012-05-23 59 views
0

在我的應用程序中,我使用ListView來選擇要訂購的物品。當我選擇要訂購的物品時,它會被檢查並且它被添加到訂購頁面中。如果我回到主頁面並且如果再次進入項目列表,選定項目將被取消選中,但所選項目出現在訂購頁面中。 應選中所選項目,直到手動刪除。請幫我解決這個問題。所選項目在Android中未選中?

這裏是我的代碼:

 public View getView(final int position, View convertView, ViewGroup parent) 
    { 


     final ViewHolder holder; 

     if (convertView == null) 
     { 
      convertView = inflater.inflate(R.layout.selecteditemlistview, null); 
      holder = new ViewHolder(); 

      holder.textViewSelectedText = (TextView)convertView.findViewById(R.id.selectedtext); 
      holder.price=(TextView)convertView.findViewById(R.id.selectitemprice); 
      holder.image=(ImageView)convertView.findViewById(R.id.selectitemimage); 
      holder.qty=(EditText)convertView.findViewById(R.id.selectqty); 
      holder.total=(TextView)convertView.findViewById(R.id.price);    
      holder.delete = (Button) convertView.findViewById(R.id.delete); 

      holder.uncheck = (CheckBox)convertView.findViewById(R.id.bcheck); 

      convertView.setTag(holder); 
     } 
     else 
     { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     String amount=holder.qty.getText().toString(); 
     final Double price1=Double.parseDouble(itemprice.get(position)); 
     int qut=Integer.parseInt(holder.qty.getText().toString()); 
     Double total=(price1*qut); 
     holder.textViewSelectedText.setText(arr1.get(position)); 
     holder.price.setText(itemprice.get(position)); 
     holder.image.setImageBitmap(itemimage.get(position)); 
     holder.total.setText(String.valueOf(total)); 


     holder.delete.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       arr1.remove(position); 
       itemimage.remove(position); 
       itemprice.remove(position); 
       // holder.uncheck.setChecked(false); 
       View parent = (View)v.getParent(); 
       CheckBox chkBox = (CheckBox)parent.findViewById(R.id.bcheck); 
       if ((holder.arr1==true)&&(holder.uncheck.isChecked())) 
        {holder.uncheck.setChecked(false); 
        } 
       notifyDataSetChanged(); 


      } 

     }); 



     holder.qty.setOnFocusChangeListener(new OnFocusChangeListener() { 

     public void onFocusChange(View v, boolean hasFocus) { 
      // TODO Auto-generated method stub 
      if(!hasFocus) 
      { 

       int position = v.getId();  
       final EditText Caption = (EditText) v; 
       Caption.setFocusable(true); 
       holder.qty.setFocusable(true); 
       int q=Integer.parseInt(holder.qty.getText().toString()); 
       Double result=(price1 * q); 
       System.out.println(result);    
       holder.total.setText(String.valueOf(result)); 
           } 
     } 
    }); 


     return convertView;  
    } 

    class ViewHolder  
    { 
     protected boolean arr1; 
     public CheckBox uncheck; 
     public Button delete; 
     public TextView mTextView; 

     TextView textViewSelectedText = null; 
     TextView price=null; 
     ImageView image=null;  
     EditText qty=null; 
     TextView total=null; 
    } 
+0

向我們展示您的適配器代碼。 –

+0

我已將我的CustomAdapter代碼添加到問題中。 – Stuthi

+0

那麼你有兩個活動?第一個創建一個新的,你可以選擇項目? – user219882

回答

0

我知道解決的辦法是手動的。您必須將選定項目的列表傳遞給您的ListView。

您的主要活動

Intent intent = new Intent(this, YourListViewActivity.class); 
// parcelables or a List of indexes (your choice but I prefer Parcelables) 
// I assume that you have your selection stored in a List to work with it 
// make SomeKey as a constant 
intent.putParcelableArrayListExtra("SomeKey", listOfYourObjectsAsParcelables); 
startActivityForResult(intent, requestCode); 

評選活動 - 假設它擴展ListActivity

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // load selection from the intent 
    List<YourParcelableObject> parcelables = getIntent().getParcelableArrayListExtra("SomeKey"); 

    if (parcelables != null) { 
     // convert to YourObject 
     List<YourObject> selectedObjects = convert(parcelables); 

     // iterate over the list and check items   
     for (int i = 0; i < getListView().getAdapter().getCount(); i++) { 
      YourObject object = (YourObject) getListView().getAdapter().getItem(i); 
      if (selectedObjects.contains(object)) { 
       getListView().setItemChecked(i, true); 
      } 
     } 
    } 
} 

請注意,這只是一個片段,我沒有測試它,但它應該給你的想法。

+0

okai我會嘗試。 – Stuthi

相關問題