我是一名正在学习Android的初学者。
我想要开发的功能是“将在EditText中输入的数据保存为JSON,将其另存为SharedPreference,并将其输出到ListView”。
要保存它,因为SharedPreference没问题,但是,要将它输出到ListView现在不能正常工作。
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private ListView listView;
private Button save_btn;
private ArrayList<List> data = new ArrayList<List>();
ListAdapter adapter;
String title="";
String info="";
int img = R.drawable.man;
String jsondata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
save_btn = (Button) findViewById(R.id.button1);
loadArrayList(getApplicationContext());
adapter = new ListAdapter(this, R.layout.row, data);
listView.setAdapter(adapter);
registerForContextMenu(listView);
save_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
View dlgview = View.inflate(MainActivity.this, R.layout.adds, null);
//adds.xml
final EditText et_title = (EditText) dlgview.findViewById(R.id.editText1);
final EditText et_info = (EditText) dlgview.findViewById(R.id.editText2);
ImageView img1 = (ImageView) dlgview.findViewById(R.id.imageView2);
AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
dlg.setTitle("ADD");
dlg.setView(dlgview);
dlg.setNegativeButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
try{
jsonObject.put("title", et_title.getText().toString());
jsonObject.put("info", et_info.getText().toString());
jsonObject.put("image",img);
jsonArray.put(jsonObject);
} catch (JSONException e){
e.printStackTrace();
}
jsondata = jsonArray.toString();
saveArrayList();
adapter.notifyDataSetChanged();
}
});
dlg.setPositiveButton("Cancel",null);
dlg.show();
}
});
}//End of onCreate
private void saveArrayList(){
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString("jsonData", jsondata);
Log.i("moi","get Data test : " + "jsonData");
editor.apply();
}
private void loadArrayList(Context context){
SharedPreferences sharedPrefs2 = PreferenceManager.getDefaultSharedPreferences(context);
int size = sharedPrefs2.getInt("appreciation_size",0);
String strJson = sharedPrefs2.getString("jsonData", "fail");
Log.i("moi","get SharedPreferences test : " + strJson);
if (strJson != "fail")
{
try {
JSONArray response = new JSONArray(strJson);
for (int i=0; i<size; i++)
{
JSONObject jsonobject = response.getJSONObject(i);
title = jsonobject.getString("title");
Log.i("moi","title test : " + "title");
info = jsonobject.getString("info");
Log.i("moi","info test : " + "info");
data.add(new List(title, info, img));
}
adapter = new ListAdapter(getApplicationContext(), R.layout.row, data);
listView.setAdapter(adapter);
} catch (JSONException e){
e.printStackTrace();
}
}
}
}//End of class
答案 0 :(得分:0)
尝试删除您的代码:
adapter = new ListAdapter(this, R.layout.row, data);
listView.setAdapter(adapter);
CreateView
中的。
我认为您将适配器listview
设置为double。
我希望能帮到你..谢谢
答案 1 :(得分:0)
因为您刚刚在loadArrayList()
中致电onCreate()
一次。这意味着适配器中的数据只会更改一次。
你调用adapter.notifyDataSetChanged()
,但这个方法没有t call
loadArrayList()`。
因此,请尝试将新json
保存到onClick()