我要動態地添加列表視圖項。我做了這個,但是當我重新啓動應用程序列表視圖項目消失。我如何將這個項目永久保存到列表視圖。請建議Android動態添加Listview項目嗎?
public class ChatWithAttorney extends Activity {
ImageView btnBack;
ListView chatList;
EditText etMsg;
Button btnSend;
String msg_to_send;
ArrayAdapter<String> adapter;
ArrayList<String> listMsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chatwithattorny);
btnBack = (ImageView) findViewById(R.id.btnback);
chatList = (ListView) findViewById(R.id.chatList);
etMsg = (EditText) findViewById(R.id.etMsg);
btnSend = (Button) findViewById(R.id.btnSend);
listMsg = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, R.layout.chat_list_text, R.id.tvSentMsg,listMsg);
chatList.setAdapter(adapter);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
try {
msg_to_send = URLEncoder.encode(etMsg.getText().toString(),
"utf-8");
listMsg.add(msg_to_send);
adapter.notifyDataSetChanged();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
});
}
使用本地數據庫 – mdDroid
你的問題是關於持久性存儲,不添加動態元素......話雖這麼說,有一個名爲[存儲選項]寫得很好頁面(https://developer.android.com /guide/topics/data/data-storage.html) –
你好,請檢查此鏈接,你會得到你的答案[點擊這裏](http://stackoverflow.com/questions/4540754/dynamically-add-elements-to-a -listview-機器人)的 –