我创建了一个listView活动,可以在项目点击时打开对话框。在对话框中,用户可以在editText中输入不同的值,这些值保存在textView中,位于同一列表视图项中。这是完美的,问题是,如果我关闭应用程序,当我再次打开它时,保存的值不再存在。关闭申请后如何保持价值?
我尝试使用SharedPrefences,但问题是listView中的每一行的值都不同,所以只是共享TextView不起作用。然后我尝试了其他的东西,但是有一个星期我遇到了很多NullPointerExceptins,到目前为止我还没有做任何事情。
这是我的:
适配器 - NoteAdapater.java:
package com.cngcnasaud.orar;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class NoteAdapter extends BaseAdapter {
String[] result;
Context context;
private static LayoutInflater inflater = null;
private Dialog dialog;
public NoteAdapter(Note note, String[] prgmNameList, String[] saved) {
// TODO Auto-generated constructor stub
result = prgmNameList;
context = note;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return result.length;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder {
TextView tv;
public TextView text;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.note_items, null);
holder.tv = (TextView) rowView.findViewById(R.id.textView1);
holder.text = (TextView) rowView.findViewById(R.id.textView2);
holder.tv.setText(result[position]);
rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Materie:" + result[position]);
final EditText txtMode = (EditText) dialog
.findViewById(R.id.dialog);
Button btnSave = (Button) dialog.findViewById(R.id.bsave);
btnSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String data = txtMode.getText().toString();
holder.text.setText(data);
dialog.dismiss();
Log.d("data", data);
}
});
dialog.show();
}
});
return rowView;
}
}
构造函数 - Note.java :
package com.cngcnasaud.orar;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListAdapter;
import android.widget.ListView;
public class Note extends Activity {
public static final ListAdapter NoteAdapter = null;
ListView lv;
Context context;
ArrayList<?> prgmName;
public static String[] prgmNameList = { "Romana - ", "Matematica - ",
"Lb. Engleza - ", "Lb. Germana/Franceza - ", "Istorie - ",
"Geografie - ", "Biologie - ", "Fizica - ", "Ed. Fizica - " };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.note_listview);
context = this;
lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(new NoteAdapter(this, prgmNameList, null));
}
@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;
}
}
答案 0 :(得分:0)
您丢失了数据,因为您的数据是静态的,您已在
中定义了数据prgmNameList = { "Romana - ", "Matematica - ",
"Lb. Engleza - ", "Lb. Germana/Franceza - ", "Istorie - ",
"Geografie - ", "Biologie - ", "Fizica - ", "Ed. Fizica - " };
每次关闭应用程序时,它都会选择该数据。为了保存更改,您需要实现一种存储形式。查看Android培训页面:Saving Data
我认为最好的选择是建立一个数据库Saving Data in SQL Databases
答案 1 :(得分:0)