在我的应用程序中,我有一个listview,其中包含一个字符串“Add New String”。当用户选择此项时,“警报”对话框会提示一个编辑文本和按钮。用户必须在此框中写入新String的名称,并且当用户单击必须添加到原始列表的按钮时。直到我已经完成了。但我的问题是当用户关闭此应用程序并重新启动时,然后列表中没有新添加的字符串。我必须以共享的偏好来做这件事。怎么可以用呢?这是我的代码。
public class MainActivity extends Activity {
ListView lv;
String add[]=new String[]{"Add new List"};
final private static int DIALOG_LOGIN = 1;
ArrayList<String> arr1;
ArrayAdapter<String> adapter1;
ArrayList<String> arr2 = new ArrayList<String>();
ArrayAdapter<String> adapter2;
int count = 0;
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.listView1);
adapter1 = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, add);
lv.setAdapter(adapter1);
//onLoad();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String data = lv.getItemAtPosition(arg2).toString();
if(data == "Add new List")
{
count = count + 1;
showDialog(DIALOG_LOGIN);
}
}
});
}
@Override
protected Dialog onCreateDialog(int id)
{
AlertDialog dialogDetails = null;
switch (id)
{
case DIALOG_LOGIN:
LayoutInflater inflater = LayoutInflater.from(this);
View dialogview = inflater.inflate(R.layout.dialog, null);
AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);
dialogbuilder.setView(dialogview);
dialogDetails = dialogbuilder.create();
break;
}
return dialogDetails;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DIALOG_LOGIN:
final AlertDialog alertDialog = (AlertDialog) dialog;
Button btnOk = (Button) alertDialog.findViewById(R.id.btnOk);
final EditText etList = (EditText) alertDialog.findViewById(R.id.etList);
btnOk.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(etList.length() <= 0)
{
etList.requestFocus();
Toast.makeText(getBaseContext(), "Enter Name of List", 1000).show();
}
else
{
String name = etList.getText().toString();
String newName[] = new String[]{};
if(arr2.contains("Add new List"))
{
arr2.add(name);
Toast.makeText(getBaseContext(), "" + name , 1000).show();
adapter2 = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, arr2);
lv.setAdapter(adapter2);
adapter2.notifyDataSetChanged();
etList.setText("");
Toast.makeText(getBaseContext(), "" + count, 1000).show();
dismissDialog(DIALOG_LOGIN);
}
else
{
arr2.add("Add new List");
arr2.add(name);
adapter2 = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, arr2);
lv.setAdapter(adapter2);
adapter2.notifyDataSetChanged();
etList.setText("");
dismissDialog(DIALOG_LOGIN);
}
}
}
});
break;
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/etList"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:ems="10"
android:hint="@string/etHint" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnOk"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/etList"
android:text="@string/ok" />
</RelativeLayout>
答案 0 :(得分:0)
您可以使用btnOk
的{{1}}方法将数据保存到共享偏好设置。您可以在onClick()
方法的arr2.add(name);
行之前保存数据。
使用共享偏好设置。
要获取共享偏好,
onClick()
要编辑和保存首选项,
SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
要阅读偏好,
String myString = "TextHere";
prefs.edit().putString("com.example.app.myValue", myString).commit();