开发者

Android context menu's info.position changing with orientation change

My activity is a ListActivity populated by an ArrayList<HashMap<String,String>> THREADS;

I've also defined an ArrayList<ArrayList<HashMap<String,String>>> PAGES; that is populated in the getView of my activity.

For each THREADS item, I add corresponding data to PAGES

public View getView(int position, View convertView, ViewGroup parent){
...
                      String lp = THREADS.get(position).get("lastPage");
                      String base = THREADS.get(position).get("base").replaceAll("^.*(?:td\\-p\\/)", "").replaceAll("/jump-to/first-unread-message", "?page_size=25&page=PAGE_NUMBER_HERE");
                      int p = Integer.parseInt(lp);
                      ArrayList<HashMap<String, String>> lnks = new ArrayList<HashMap&l开发者_如何学Pythont;String, String>>();

                          for(int u=0;u<p;u++){
                              HashMap<String,String> temp = new HashMap<String,String>();
                              temp.put("page", Integer.toString(u+1));
                              temp.put("base", base);
                              temp.put("title", THREADS.get(position).get("title"));
                              lnks.add(temp);
                              PAGES.add(position, lnks);

                          }
...
}

This PAGES ArrayList is accessed through a context menu

           case R.id.goPage:
                final Dialog dlg = new Dialog(ThreadListing.this);
                dlg.setContentView(R.layout.link_selector);
                ListView sel = (ListView) dlg.findViewById(R.id.linksel);

                adaptr = new SimpleAdapter(this, PAGES.get(info.position), R.layout.link_sel_row, new String[]{"page"}, new int[]{R.id.link_title});
                pgs = PAGES.get(info.position);
                sel.setAdapter(adaptr);
                dlg.setTitle(PAGES.get(info.position).get(0).get("title"));

                dlg.show();
                sel.setOnItemClickListener(new OnItemClickListener(){
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                        Intent i = new Intent(getApplicationContext(), ThreadViewer.class);
                        String pg = pgs.get(position).get("base").replaceAll("PAGE_NUMBER_HERE", pgs.get(position).get("page"));
                        i.setData(Uri.parse(pg));
                        startActivity(i);
                        dlg.dismiss();
                    }
                });
                return true;

This context menu also accesses the THREADS ArrayList

On first load of the activity, everything is matched up correctly. The dialog shows the correct title, and the adapter contains the correct data.

However, when I either start a new activity then return via the back button, or change the orientation, the dialog gets thrown out of order. It starts showing the title and adapter for a list item earlier than the one I am selecting as if info.position referred to a different position, however the context menu item that accesses THREADS remains in the correct order. So THREADS.get(info.position) and PAGES.get(info.position) aren't synced anymore somehow. Changing the orientation again sometimes re-syncs it correctly, but I don't understand how this happens in the first place.


Why exactly are you adding to the PAGES ArrayList in GetView?

 for(int u=0;u<p;u++){
     ....
     PAGES.add(position, lnks);
 }

getView doesn't belong to the Activity it is (likely, but not certain without seeing your code) inherited from Adapter That method is intended to return a view corresponding to a row, not to modify the dataset as you are doing. When returning from another Activity or changing orientation the system is refreshing the views (by calling getView on the adapter) which is modifying the underlying dataset, which is obviously not what you intended.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜