2011-09-28 66 views
0

刪除ListView中的項目後,我無法獲取正確的項目。 該位置應該適合ArrayList中項目的索引,但是在刪除ListView中的項目後,位置不正確(甚至可能會導致indexoutofbounds異常。)刪除項目後,ListView中的剩餘項目位置不正確

我敢打賭,它與視圖位置,但我就是無法找到錯誤

我有一個內部BaseAdapter類此ListActivity類:

public class MSMobilMyStocksActivity extends ListActivity { 
    static MyStocksCtr myStocksCtr; 
    static ListView listview; 



    static EfficientAdapter adap; 
    boolean downloadSuccess; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.mystockslist); 
     mContext = this; 
     listview = getListView(); 
     myStocksCtr = MyStocksCtr.getInstance(mContext); 

     adap = new EfficientAdapter(this); 

     setListAdapter(adap); 
     if (isOnline()) 
      new InsertDataTask().execute(); 
     else { 
      startTimer(); 
      Toast.makeText(mContext, "Du har ingen internetforbindelse", 1000) 
        .show(); 
     } 

    } 

    public static boolean updateData() { 
     return myStocksCtr.downloadJson(); 
    } 

    private class InsertDataTask extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected void onPreExecute() { 
      try { 
       listview.setEnabled(false); 
      } catch (Exception e) { 
      } 
     } 

     // can use UI thread here 
     @Override 
     protected void onPostExecute(final Void unused) { 
      if (isOnline()) { 
       if (downloadSuccess) { 
        try { 
         if (myStocksCtr.getArray().size() == 0) { 
          Toast.makeText(mContext, "Ingen data hentet", 2000) 
            .show(); 
         } else if (myStocksCtr.getArray().size() == 1 
           && myStocksCtr.getArray().get(0) 
             .getString("FH_FULLNAME").equals("-")) { 
          Toast.makeText(mContext, "Ingen data hentet", 2000) 
            .show(); 
         } else { 
          adap.notifyDataSetChanged(); 
         } 
        } catch (JSONException e) { 
         Toast.makeText(mContext, "JSONException", 2000).show(); 
        } 
       } 
      } else 
       Toast.makeText(mContext, "Du har ingen internetforbindelse", 
         1000).show(); 
      startTimer(); 
      try { 
       listview.setEnabled(true); 
      } catch (Exception e) { 
      } 
     } 

     @Override 
     protected Void doInBackground(Void... arg0) { 
      downloadSuccess = false; 
      if (isOnline()) { 
       try { 
        if (updateData()) 
         downloadSuccess = true; 
       } catch (Exception e) { 
        Toast.makeText(mContext, "Fejl under download af data", 
          2000).show(); 
        downloadSuccess = false; 
       } 
      } else 
       Toast.makeText(mContext, "Du har ingen internetforbindelse", 
         1000).show(); 

      return null; 

     } 
    } 

    } 

    public static class EfficientAdapter extends BaseAdapter implements 
      Filterable { 
     private LayoutInflater mInflater; 
     private Context context; 

     public EfficientAdapter(Context context) { 
      mInflater = LayoutInflater.from(context); 
      this.context = context; 
     } 


     @Override 
     public View getView(final int position, View convertView, 
       ViewGroup parent) { 
      final ViewHolder holder; 

      if (convertView == null) { 
       convertView = mInflater.inflate(R.layout.mylistitem, null); 

       holder = new ViewHolder(); 
       holder.stockName = (TextView) convertView 
         .findViewById(R.id.stockName1); 
       holder.stockPrice = (TextView) convertView 
         .findViewById(R.id.stockValue1); 
       holder.stockChange = (TextView) convertView 
         .findViewById(R.id.stockChange1); 
       holder.stockPctChange = (TextView) convertView 
         .findViewById(R.id.stockPctChange1); 

       convertView.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 

         try { 
          Intent i = new Intent(mContext, MSStockDetailActivity.class); 
          Bundle b = new Bundle(); 
          b.putInt("position", position); //Your id 
          b.putBoolean("isMyStocks", true); 
          i.putExtras(b); //Put your id to your next Intent 
          mContext.startActivity(i); 
         } catch (Exception e) { 
          Toast.makeText(mContext, "OnClick error ", 2000) 
            .show(); 
         } 
        } 
       }); 

       convertView.setOnLongClickListener(new OnLongClickListener() { 

        @Override 
        public boolean onLongClick(View arg0) { 

         final CharSequence[] items = { 
           "See stock details", 
           "Delete the stock \"" + holder.stockName.getText() 
             + "\" from the list", "Cancel" }; 

         AlertDialog.Builder builder = new AlertDialog.Builder(
           mContext); 
         builder.setTitle("Configure list"); 
         builder.setItems(items, 
           new DialogInterface.OnClickListener() { 
            @Override 
            public void onClick(DialogInterface dialog, 
              int item) { 
             if (item == 1) { 
              myStocksCtr.removeStock(holder.ric); 
              adap.notifyDataSetChanged(); 
             } else if (item == 2) { 
             } 
             Toast.makeText(mContext, items[item], 
               Toast.LENGTH_SHORT).show(); 
            } 
           }); 
         AlertDialog alert = builder.create(); 
         alert.show(); 
         return true; 
        } 
       }); 

       convertView.setTag(holder); 
      } else { 

       holder = (ViewHolder) convertView.getTag(); 
      } 
      try { 
       String result = myStocksCtr.getArray().get(position) 
         .getString("FH_FULLNAME"); 
       holder.stockName.setText(result); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      try { 
       String result = myStocksCtr.getArray().get(position) 
         .getString("FH_RIC"); 
       holder.ric = result; 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      // + String.valueOf(position)); 
      try { 
       String result = myStocksCtr.getArray().get(position) 
         .getString("FH_PRC"); 
       if (String.valueOf(result.charAt(result.length() - 2)).equals(
         ".")) { 
        result += "0"; 
       } 
       ; 
       holder.stockPrice.setText(result); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      holder.position = position; 

      return convertView; 
     } 

     static class ViewHolder { 
      String ric; 
      TextView stockName; 
      TextView stockPrice; 
      TextView stockPctChange; 
      TextView stockChange; 
      int position; 

     } 

     @Override 
     public Filter getFilter() { 
      return null; 
     } 

     @Override 
     public long getItemId(int position) { 
      return 0; 
     } 

     @Override 
     public int getCount() { 
      return myStocksCtr.getArray().size(); 
     } 

     @Override 
     public JSONObject getItem(int position) { 
      return myStocksCtr.getArray().get(position); 
     } 

    } 

這是一些在我的庫存控制的事情(其中數組是):

public class MyStocksCtr { 

private static MyStocksCtr INSTANCE = null; 

ArrayList<String> myStocksArray; 
JSONCtr jsonCtr; 
static Context mContext; 
boolean isRunning = false; 
ArrayList<JSONObject> myArray; 

MyStocksCtr(Context mContext) { 
    this.mContext = mContext; 
    jsonCtr = new JSONCtr(mContext); 
    myStocksArray = new ArrayList<String>(); 
    myArray = new ArrayList<JSONObject>(); 
    } 
} 

public static MyStocksCtr getInstance(Context context){ 
    mContext = context; 
    if(INSTANCE == null) { 
      INSTANCE = new MyStocksCtr(mContext); 
     } 
     return INSTANCE; 
} 

public ArrayList<JSONObject> getArray() { 
    return myArray; 
} 

public boolean downloadJson() { 
    if (!myStocksArray.isEmpty()) { 
     String myStocksString = ""; 
     for (String s : myStocksArray) 
      myStocksString += (s + "%2C"); 
     myStocksString = myStocksString.substring(0, 
       myStocksString.length() - 3); 
     jsonCtr.getJSON(preStocksUrl + myStocksString + postStocksUrl); 
     myArray.clear(); 
     myArray.addAll(jsonCtr.getArray()); 
     return true; 
    } else { 
     return false; 
    } 
} 


public void removeStock(String newStock) { 
    JSONObject toRemove = null; 

    for (JSONObject obj : myArray) { 
     try { 
      if (obj.getString("FH_RIC").equals(newStock)) { 
       toRemove = obj; 
      } 
     } catch (JSONException e) { 
      Toast.makeText(mContext, "Kunne ikke fjerne objekt", 1000) 
        .show(); 
     } 
    } 
    if (toRemove != null){ 
     myStocksArray.remove(newStock); 
    jsonCtr.removeRic(toRemove); 
    myArray = jsonCtr.getArray(); 
    saveMyStocks(); 
} 
    } 

    } 

在我jsonCtr我刪除和獲取數組:

public ArrayList<JSONObject> getArray() { 
    return rics1; 
} 

public void removeRic(Object o){ 
    rics1.remove(o); 
} 

當我已刪除從列表中選擇對象,更新列表,所以該項目就不再出現。但是這些觀點的立場全都搞砸了,其中有些是過時的。

我猜它與GetView中的位置有關係是最終的,但我不確定嗎?如果這是問題,我該如何糾正?

+0

仍然不完整的代碼爲listview請提供listview實現代碼在這裏 – Pratik

+0

代碼現在編輯..我已經添加了整個ListActivity .. – JReneM

+0

我完全忘記了:請發佈您的logcat輸出(異常堆棧)。這肯定會有很大的幫助;-) – Knickedi

回答

1

嗯,我沒有看過你的代碼的每一個細節,但我認爲你錯過了打電話給myAdaper。更改你的數組後更改爲notifyDataSetChanged()

編輯

這* S真的很難看低谷,因爲這是一個很大的代碼,你在這裏發表。所以這就是我得到的。我看不出這是怎麼回事在(股票刪除),但必須有我覺得不對勁:

jsonCtr.removeRic(toRemove); 
myArray = jsonCtr.getArray(); 

請不要張貼整個代碼也一樣,儘量將其降低到實際問題。我不會再檢查整個代碼...

+0

代碼已更新..在數組中更改時使用notifyDataSetChanged() - 但它是正確的方式嗎? – JReneM

+0

對不起 - 我對此很陌生.. 我已經添加了您所指的代碼.. – JReneM