0

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

在此处输入图像描述

当我离开活动并回来时,我希望仍然拥有包含我所有数据的列表视图。到目前为止,我已经尝试使用 onSavedInstance 和 onRestoredInstance 保存我的数组列表的内容,然后在 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;
}
4

3 回答 3

0

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

在: onResume(){ 打开 sharedpreferences,恢复您的列表,setdata 已更改}

于 2012-07-08T15:01:26.203 回答
0

您应该将数据保存在 中onPause并重新填充适配器onResume。即时消息之类的内容应保存到持久数据存储(例如,SQLite 数据库),然后使用CursorAdapter. 在整个生命周期中维护数据负载的许多细节Activity都可以通过使用LoaderManagera 和用 a 加载数据来抽象出来CursorLoader

听起来您还想Service在后台运行,即使Activity不在焦点上也可以接收消息并将即时消息保存到数据存储中。

于 2012-07-08T15:04:47.217 回答
0

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

您应该使用SharedPreferences

于 2012-07-08T15:52:24.483 回答