删除项目后,ListView中的剩余项目位置不正确

时间:2011-09-28 10:50:52

标签: android listview view position

删除ListView中的项目后,我无法获取正确的项目。 该位置应该适合ArrayList中项目的索引,但在删除ListView中的项目后,位置不正确(甚至可能会给indexoutofbounds异常。

我敢打赌它与Views位置有关,但我找不到错误。

我有一个带有内部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中的位置有关是最终的,但我不确定?如果这是问题,我该如何纠正?

1 个答案:

答案 0 :(得分:1)

嗯,我没有看过你代码的每一个细节,但我认为在更改你的数组之后,你错过了致电myAdapernotifyDataSetChanged()

修改

因为你发布了大量的代码,所以很难看出来。所以这就是我得到的。我无法看到这里发生了什么(库存删除),但我认为一定有问题:

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

请不要发布整个代码,尝试将其减少到实际问题。我不会再检查整个代码......