私はかなりの間この作業をしていますが、それでも正しい結果を得られませんでした。私はユーザーが手動でリストに項目を追加することを可能にするシンプルなアプリケーションを作成しようとしていて、項目はSharedPreferencesを使用して永久に保存されます。しかし、私はそれを正しく得ることができませんでした(私は実際にプログラミングのノブです)。誰もが何か提案を与えることができますか?どうもありがとうございました!SharedPreferencesからデータを読み込んでリストビューを作成します
だからここ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();
}
}
、私はカスタムアレイアダプタとリストビューを作成しようとしている、とボタンを押すと、それはユーザーが彼らが望むものを入力することが可能である別のページに移動しますManualAddDemo、と何でも彼らは意志を入力しましたメインアクティビティ(別の文字列としての日付とともに)を解析します。その後、私は両方の文字列をJSONObjectに入れて、共有設定の中に保存しようとしました。リストビューが読み込まれるたびにデータが取得されるように、私はcustomarrayadapter内のデータを取得します。
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;
}
}
私のユーザークラス:
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;
}
}
任意の提案ですか? Tqvm!コメントで述べたように
あなたが直面している問題は何ですか?格納されたアイテムを取得できないのですか、まったく問題がありますか? –
私が望むデータを保存できますが、customarrayadapterで取得できませんでした。リストビューを実行すると、追加したアイテムは取得されません。 – Percy
JSONオブジェクトでデバッグセットをチェックして確認してくださいあなたが必要とする価値を実際に得るならば。これは、あなたが書いた場所と読んでみる場所の両方で確認できます。 –