Java 从SharedReferences加载数据并填充listview
我已经为此工作了很长时间,但还是没能做好。我正在尝试创建一个简单的应用程序,允许用户手动将项目添加到列表中,并使用SharedReferences永久存储项目。但我一直没能把它做好(顺便说一句,我真的是个编程高手)。有人能给点建议吗?多谢各位Java 从SharedReferences加载数据并填充listview,java,android,listview,android-studio,Java,Android,Listview,Android Studio,我已经为此工作了很长时间,但还是没能做好。我正在尝试创建一个简单的应用程序,允许用户手动将项目添加到列表中,并使用SharedReferences永久存储项目。但我一直没能把它做好(顺便说一句,我真的是个编程高手)。有人能给点建议吗?多谢各位 public class Mainmemo extends Activity { public ArrayList<User> arrayOfUsers = User.getUsers(); public ListView listView;
public class Mainmemo extends Activity {
public ArrayList<User> arrayOfUsers = User.getUsers();
public ListView listView;
public CustomUsersAdapter adapter;
public SharedPreferences sharedpref;
public Bundle extra;
public static final String PREFERENCES_TODO = "TODO_List_Shared_Preferences";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_list);
populateUsersList();
}
private void populateUsersList() {
adapter = new CustomUsersAdapter(this, arrayOfUsers);
listView = (ListView) findViewById(R.id.lvUsers);
final TextView textview = (TextView) findViewById(R.id.empty);
final Button manualaddmemo = (Button) findViewById(R.id.addmemo);
listView.setEmptyView(textview);
listView.setAdapter(adapter);
listView.setItemsCanFocus(false);
listView.setClickable(true);
extra = getIntent().getExtras();
if (extra != null) {
String text = extra.getString("text");
String date = extra.getString("dateoftext");
extra = null;
savestringtopreferences(text, date);
}
manualaddmemo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent newIntent = new Intent(Mainmemo.this, add_memo_to_list.class);
startActivityForResult(newIntent, 0);
}
});
}
private void savestringtopreferences(String text, String date) {
sharedpref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedpref.edit();
JSONObject obj = new JSONObject();
try {
obj.put("Name", text);
obj.put("Category", date);
} catch (JSONException e) {
e.printStackTrace();
}
editor.putString("JSON", obj.toString());
editor.apply();
}
}
public类mainmoom扩展活动{
public ArrayList arrayOfUsers=User.getUsers();
公共列表视图列表视图;
公共CustomUsersAdapter适配器;
公共共享引用共享引用;
公共捆绑额外费用;
公共静态最终字符串首选项\u TODO=“TODO\u List\u Shared\u PREFERENCES”;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u custom\u列表);
populateUsersList();
}
私有无效的populateUserList(){
适配器=新的CustomUsersAdapter(这是ArrayFuser);
listView=(listView)findViewById(R.id.lvUsers);
最终文本视图文本视图=(文本视图)findViewById(R.id.empty);
最终按钮manualaddmemo=(按钮)findViewById(R.id.addmemo);
setEmptyView(文本视图);
setAdapter(适配器);
setItemsCanFocus(false);
listView.setClickable(真);
extra=getIntent().getExtras();
如果(额外!=null){
String text=extra.getString(“text”);
String date=extra.getString(“dateoftext”);
额外=零;
savestringtopreferences(文本、日期);
}
manualaddmemo.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Intent newIntent=newIntent(mainmoom.this,将\u memo\u添加到\u list.class);
startActivityForResult(newIntent,0);
}
});
}
私有void savestringtopreferences(字符串文本、字符串日期){
sharedpref=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor=sharedpref.edit();
JSONObject obj=新的JSONObject();
试一试{
obj.put(“名称”,文本);
实物交付(“类别”,日期);
}捕获(JSONException e){
e、 printStackTrace();
}
putString(“JSON”,obj.toString());
editor.apply();
}
}
因此,在这里,我试图创建一个带有自定义数组适配器的listview,按下ManualAddDemo按钮后,它将导航到另一个页面,允许用户键入任何他们想要的内容,并且他们键入的内容将解析回主活动(连同日期作为另一个字符串)。这样做之后,我尝试将这两个字符串放入JSONObject,然后将其保存在共享首选项中。然后在customarrayadapter中检索数据,以便每次加载listview时都会检索数据
public class CustomUsersAdapter extends ArrayAdapter<User> {
public CustomUsersAdapter(Context context, ArrayList<User> users) {
super(context, 0, users);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
User user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.memolist, parent, false);
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvdate = (TextView) convertView.findViewById(R.id.date);
TextView indexview = (TextView) convertView.findViewById(R.id.indexnumber);
// Populate the data into the template view using the data object
int sequence = position +1;
user.index = String.valueOf(sequence);
SharedPreferences sharedpref = PreferenceManager.getDefaultSharedPreferences(getContext());
String name = sharedpref.getString("JSON", "");
try {
JSONObject object = new JSONObject(name);
user.name = object.getString("Name");
user.date= object.getString("Category");
indexview.setText(user.index);
tvName.setText(user.name);
tvdate.setText(user.date);
} catch (JSONException e) {
e.printStackTrace();
}
// Return the completed view to render on screen
return convertView;
}
公共类CustomUsersAdapter扩展了ArrayAdapter{
公共CustomUsersAdapter(上下文,ArrayList用户){
超级(上下文,0,用户);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//获取此职位的数据项
用户=获取项目(位置);
//检查是否正在重用现有视图,否则会膨胀视图
if(convertView==null){
convertView=LayoutInflater.from(getContext()).flate(R.layout.memolist,parent,false);
}
//数据填充的查找视图
TextView-tvName=(TextView)convertView.findViewById(R.id.tvName);
TextView-tvdate=(TextView)convertView.findViewById(R.id.date);
TextView indexview=(TextView)convertView.findViewById(R.id.indexnumber);
//使用数据对象将数据填充到模板视图中
int序列=位置+1;
user.index=String.valueOf(序列);
SharedPreferences sharedpref=PreferenceManager.getDefaultSharedPreferences(getContext());
String name=sharedpref.getString(“JSON”和“”);
试一试{
JSONObject对象=新的JSONObject(名称);
user.name=object.getString(“name”);
user.date=object.getString(“类别”);
index.setText(用户索引);
tvName.setText(user.name);
tvdate.setText(用户日期);
}捕获(JSONException e){
e、 printStackTrace();
}
//返回要在屏幕上渲染的已完成视图
返回视图;
}
}
我的用户类:
public class User {
public String name;
public String date;
public String index;
public User( String index, String name, String date) {
this.index = index;
this.name = name;
this.date = date;
}
public static ArrayList<User> getUsers() {
ArrayList<User> users = new ArrayList<>();
return users;
}
公共类用户{
公共字符串名称;
公共字符串日期;
公共字符串索引;
公共用户(字符串索引、字符串名称、字符串日期){
这个指数=指数;
this.name=名称;
this.date=日期;
}
公共静态ArrayList getUsers(){
ArrayList用户=新建ArrayList();
返回用户;
}
}
有什么建议吗?Tqvm 如评论中所述: 首先,在自定义适配器类中声明构造函数
public customAdapter(Context context, List<String> info){
// do whatever you want to do
}
请说明您面临的问题是什么?您是否无法检索存储的项或问题到底是什么?我可以保存我想要的数据,但无法在customarrayadapter中检索它…因此,每当我的listview运行我添加的项时,将无法检索,请使用JSON对象上的调试集进行检查,并检查它是否确实获得了您需要的值。你可以在你写东西的地方和你试着读东西的地方进行检查。我检查过了,似乎我可以正确地保存JSON对象并获得我在主要活动中需要的值,但是我在将它加载到我的自定义数组适配器中时遇到了困难。我尝试过,但我仍然面临同样的问题。我的customarrayadapter似乎无法检索到我在SharedReference中保存的数据,这意味着每次我访问时都没有发生任何事情
CustomAdapter adapter = new CustomAdapter(this, infoList);