在调用onDestroy()之后恢复我的自定义列表视图和数据,然后在android中恢复onCreate()

时间:2012-07-08 14:54:53

标签: android android-listview android-activity android-adapter custom-adapter

我有一个带有IM样式页面的应用程序,使用自定义列表视图显示文本

enter image description here

当我离开活动并回来时,我仍然希望列表视图中包含我的所有数据。到目前为止,我已尝试使用onSavedInstance和onRestoredInstance保存我的arraylist的内容,然后再次在onRestoredInstance中设置适配器,但这不起作用。

以下是我的活动代码:

private ArrayList<String> nameList = new ArrayList<String>();
    private ArrayList<String> messageList = new ArrayList<String>();
    private ArrayList<String> timeList = new ArrayList<String>();

    Button send = (Button) findViewById(R.id.chat_send);
    EditText send = (EditText) findViewById(R.id.edit_text);
    ListView chat_list = (ListView) findViewById(R.id.chat_list);
    MyCustomArrayAdapter myArrayAdapter = new MyCustomArrayAdapter(this, messageList, nameList,
            timeList, receiverName, senderName, language_chosen);

    chat_list.setAdapter(myArrayAdapter);       

    send.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            String new_text = edit_text.getText().toString();
            String sys_time = new Time(System.currentTimeMillis())
                    .toString();
            timeList.add(sys_time);
            messageList.add(new_text);
            nameList.add(username);
            myArrayAdapter.notifyDataSetChanged();
            chat_list.setSelection(chat_list.getChildCount()-1);
        }
    });

这是我的适配器的代码

public MyCustomArrayAdapter(Context context, ArrayList<String> planetList,
        ArrayList<String> NameList, ArrayList<String> TimeList,
        String receiver, String sender, String language) {
    super(context, R.layout.custom_chat_layout, planetList);
    this.context = context;
    messageList = planetList;
    nameList = NameList;
    timeList = TimeList;
    receiverName = receiver;
    senderName = sender;
    language_chosen = language;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    holder = new ViewHolder();

    rowView = inflater.inflate(R.layout.custom_chat_layout, parent, false);
    holder.chat_name = (TextView) rowView
            .findViewById(R.id.custom_chat_name);
    holder.chat_time = (TextView) rowView
            .findViewById(R.id.custom_chat_timestamp);
    holder.chat_text = (TextView) rowView
            .findViewById(R.id.custom_chat_text);


    holder.chat_name.setText(nameList.get(position));
    holder.chat_time.setText(timeList.get(position));
    holder.chat_text.setText(messageList.get(position));

    return rowView;
}

static class ViewHolder {
    TextView chat_name;
    TextView chat_time;
    TextView chat_text;
    RelativeLayout relative;
    RelativeLayout relative1;
}

3 个答案:

答案 0 :(得分:0)

在: 的onPause(){ 打开共享首选项,保存列表。 }

在: 的onResume(){ 打开共享首选项,恢复列表,更改setdata }

答案 1 :(得分:0)

您应该将数据保存在onPause中,然后在onResume中重新填充适配器。像即时消息这样的东西应该保存到持久数据存储(例如,SQLite数据库),然后加载CursorAdapter。通过使用Activity并使用LoaderManager加载数据,可以抽象出在CursorLoader生命周期内维护数据负载的许多细节。

听起来您还希望在后台运行Service,即使Activity无法清晰对焦,也可以接收消息并将即时消息保存到数据存储中。

答案 2 :(得分:0)

onSaveInstanceState()仅在您的应用运行时使用。应用程序停止后,捆绑包中的所有数据都将被释放。

您应该使用SharedPreferences