所以我设法将数组中的数据保存在json文件中。您可以通过8 item.add(...)
手动创建此数组,每个项目都有4个值。 isSelected
,userName
,userAttack
,userStatus
和isFavorite
。只有isSelected
和isFavorite
会通过onItemClickListeners
进行更改,并且类型均为布尔值。我想存储这些值的更改,只要单击该项目即可。日志会告诉我json是一个数组,其中包含所有信息。
我的问题是在创建应用程序时检索此信息,我不知道将其放置在何处。不仅如此,我也不知道如何在onCreate中使用json文件,因为仅当我单击或长按项目时才创建json。有人可以给我提示如何成功让应用程序记住该项目具有的价值吗?会非常感激的。
//package and imports
public class MainActivity extends Activity {
ListView listView;
private String msg = "Android : ";
private boolean isChecked;
ArrayList<UserModel> item = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<UserModel> item = new ArrayList<>();
listView = (ListView) findViewById(R.id.list_view);
item.add(new UserModel(false, R.string.name0, R.string.atk0, R.string.stat0,false));
item.add(new UserModel(false, R.string.name1, R.string.atk1, R.string.stat1,false));
item.add(new UserModel(false, R.string.name2, R.string.atk2, R.string.stat2,false));
item.add(new UserModel(false, R.string.name3, R.string.atk3, R.string.stat3,false));
item.add(new UserModel(false, R.string.name4, R.string.atk4, R.string.stat4,false));
item.add(new UserModel(false, R.string.name5, R.string.atk5, R.string.stat5,false));
item.add(new UserModel(false, R.string.name6, R.string.atk6, R.string.stat6,false));
item.add(new UserModel(false, R.string.name7, R.string.atk7, R.string.stat7,false));
Log.d(msg, "Made UserModel List with size: " + item.size());
final CustomAdapter adapter = new CustomAdapter(this, item);
listView.setAdapter(adapter);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.d(msg,"Item " + i + " was long clicked!");
UserModel model = item.get(i);
if(model.isFavorite()) {
model.setFavorite(false);
Log.d(msg, "Item " + i + " was removed as Favorite!");
Toast.makeText(view.getContext(), model.getUserName() + i + " was removed from Favorites!", Toast.LENGTH_SHORT).show();
} else {
model.setFavorite(true);
Log.d(msg, "Item " + i + " was added as Favorite!");
Toast.makeText(view.getContext(),model.getUserName() + i + " was added to Favorites!",Toast.LENGTH_SHORT).show();
}
item.set(i, model);
adapter.updateRecords(item);
Gson gson = new Gson();
String json = gson.toJson(item);
Log.d(msg, "Here is the json file: " + json);
return model.isFavorite();
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.d(msg, "Item " + (i) + " was clicked!");
UserModel model = item.get(i);
if (model.isSelected())
model.setSelected(false);
else
model.setSelected(true);
Log.d(msg, "Item " + (i) + " " + model.isSelected());
item.set(i, model);
//now update adapter
adapter.updateRecords(item);
Gson gson = new Gson();
String json = gson.toJson(item);
Log.d(msg, "Here is the json file: " + json);
}
});
}
}
答案 0 :(得分:0)
假设我理解了您的问题,这就是如何加载json数据onCreate
。
ArrayList<UserModel> items = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Load json to string from the file you saved it in
...
String rawJson = "...";
//Parse data to your list
...
items = gson.fromJson(br, new TypeToken<List<UserModel>>(){}.getType());
}
但是,我建议您考虑将数据存储在本地数据库中,因为它更易于更新和管理。将json存储在文件中非常难看。
您可以将库签出为Room。