方向更改后Android ListView未更新

时间:2013-04-18 20:43:31

标签: android

我在google搜索过去几个小时,我已经看过这个问题20次,但是没有一个解决方案似乎适合我。我有一个ListView和一个CustomList类,它包含一个列表适配器和一个简单的对话框,用于编辑/删除/重命名项目。一切正常,直到第一次屏幕旋转。之后,尽管适配器包含所有正确的数据,但列表仍会停止更新。 我会发布这两个文件,而不会跳过一行。我此时不需要保存任何数据,我稍后会处理。这真的很奇怪,因为据说应用程序在屏幕旋转后完全重启。是什么让它变得更加奇怪,即使在屏幕旋转之后,虚拟项目也被添加到了创建状态,但在此之后,列表中没有其他响应。

请原谅我的英语和我的无知,并试着指出我正确的方向。

  package com.sl.mylandmarks;

import android.app.Activity; import android.content.ContentResolver; import android.content.Context; import android.location.LocationManager; import android.os.Bundle; import android.provider.Settings; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView;

public class MainActivity extends Activity {

    Button addButton;   ListView landmarksList;     EditText inputName, inputSearch;    CustomList customList;  Context globalContext;  TextView gpsState;  private LocationManager locationManager;    private GPSTracker gpsTracker;  private double longitude;   private double latitude;

    @Override   protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);

        gpsTracker = new GPSTracker(MainActivity.this);

        globalContext = this;

        addButton = (Button) findViewById(R.id.addBtn);

        customList = new CustomList(this);

                landmarksList = (ListView) findViewById(R.id.listView1);        landmarksList.setAdapter(customList.getAdapter());      landmarksList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);       landmarksList.setTextFilterEnabled(true);       landmarksList.setLongClickable(true);       customList.theList = landmarksList;


        inputName = (EditText) findViewById(R.id.EditItemName);

        gpsState = (TextView) findViewById(R.id.gpsState);

        ContentResolver contentResolver = getBaseContext().getContentResolver();        boolean gpsStatus = Settings.Secure.isLocationProviderEnabled(
                contentResolver, LocationManager.GPS_PROVIDER);         if (gpsStatus) {            gpsState.setText("GPS Enabled");        } else {            gpsState.setText("GPS Disabled");       }

        // // SEARCH BOX

        inputSearch = (EditText) findViewById(R.id.editText3);      inputSearch.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {

            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {            }

            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                customList.getAdapter().getFilter().filter(s);          }       });         // // SEARCH BOX



        // / DUMMY ITEMS        customList.addItem("Home", "43.1565 / 15.8645");        customList.addItem("Work", "43.1565 / 15.8645");        customList.addItem("Denis` apartment", "43.1565 / 15.8645");        customList.addItem("Natasa", "43.1565 / 15.8645");      customList.addItem("Bruce Wayne", "43.1565 / 15.8645");         customList.addItem("Walker shop", "43.1565 / 15.8645");         customList.addItem("Chuck Norris Residence", "43.1565 / 15.8645");      customList.addItem("Some landmark", "43.1565 / 15.8645");       // customList.removeItem(3);


        OnItemLongClickListener listLongClick = new OnItemLongClickListener() {

            @Override           public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                customList.showOptionsDialog(arg2);
                return true;// event consumed, not dispatched forward           }       };

        landmarksList.setOnItemLongClickListener(listLongClick);

        OnClickListener ButtonClick = new View.OnClickListener() {

            @Override           public void onClick(View v) {
                switch (v.getId()) {
                case R.id.addBtn:

                    customList.addItem(inputName.getText().toString(),
                            "45.5644 / 23.6541");

                    inputName.setText("");
                    break;

                }

            }

        };

        addButton.setOnClickListener(ButtonClick);

    }

    @Override   public void onPause() {         super.onPause();


    }

    @Override   public void onResume() {        super.onResume();

    }

    @Override   public boolean onCreateOptionsMenu(Menu menu) {         // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);       return true;

    }

}

和CustomList.java

package com.sl.mylandmarks;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class CustomList {

    ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(
            2);
    HashMap<String, String> maplist;
    SimpleAdapter listAdapter;
    public Context context;
    public ListView theList;
    public CustomList(Context con) {

        context = con;
        String[] from = { "line1", "line2" };

        int[] to = { android.R.id.text1, android.R.id.text2 };

        listAdapter = new SimpleAdapter(context, list,
                android.R.layout.simple_list_item_2, from, to);

    }

    public void addItem(String name, String coords) {
        if ((name != null) && (name.length() != 0)) {
            maplist = new HashMap<String, String>();
            maplist.put("line1", name);
            maplist.put("line2", coords);
            list.add(maplist);
            listAdapter.notifyDataSetChanged();
        }
    }

    public void removeItem(int id) {



        Log.d("removing",list.remove(id).toString());

        listAdapter.notifyDataSetChanged();
    }

    public void renameItem(int id, String newName) {
        Log.d("SL","Rename Selected");
        maplist = new HashMap<String, String>();
        maplist.put("line1", newName);
        maplist.put("line2", list.get(id).get("line2"));
        list.set(id, maplist);
        listAdapter.notifyDataSetChanged();

    }

    public SimpleAdapter getAdapter() {

        return listAdapter;
    }

    public void showOptionsDialog(final int position) {

        //Log.d("SL", String.valueOf(position));
        final Dialog optionsDialog = new Dialog(context);
        optionsDialog.setContentView(R.layout.list_dialog);
        optionsDialog.setTitle("Options");
        optionsDialog.show();
        final EditText itemNameEdit = (EditText) optionsDialog
                .findViewById(R.id.EditItemName);
        final Button removeBtn = (Button) optionsDialog
                .findViewById(R.id.removeBtn);
        final Button renameBtn = (Button) optionsDialog
                .findViewById(R.id.renameBtn);
        final Button cancelBtn = (Button) optionsDialog
                .findViewById(R.id.cancelBtn);

        itemNameEdit.setText(list.get(position).get("line1"));
        itemNameEdit.setSelection(itemNameEdit.length());

        OnClickListener ButtonClick = new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                switch (arg0.getId()) {
                case R.id.removeBtn:
                    removeItem(position);
                    optionsDialog.dismiss();
                    break;
                case R.id.renameBtn:
                    renameItem(position, itemNameEdit.getText().toString());
                    optionsDialog.dismiss();
                    break;
                case R.id.cancelBtn:
                    optionsDialog.dismiss();
                    break;
                }
            }

        };

        removeBtn.setOnClickListener(ButtonClick);
        renameBtn.setOnClickListener(ButtonClick);
        cancelBtn.setOnClickListener(ButtonClick);

    }

}

1 个答案:

答案 0 :(得分:1)

我不确定你的问题究竟是什么。而且我没有看你的代码,因为我不认为SO是一个很好的地方只是转储类并说“修复这个”。但我可能会给你答案。

Android正在旋转时重新创建活动(如roky所说)。如果你来自iPhone开发背景(这是我的情况),它看起来有点奇怪。

但是,如果您的活动不是太复杂,您可以对Android说处理它。这样做,将其添加到AndroidManifest.xml的标签

<activity android:configChanges="orientation|screenSize" ... >
</activity>

这是一个比我更好的解释:android:configChanges="orientation" does not work with fragments

如果我的答案很好,那就接受它会很好!