2016-03-29 7 views
-1

私はかなりの間この作業をしていますが、それでも正しい結果を得られませんでした。私はユーザーが手動でリストに項目を追加することを可能にするシンプルなアプリケーションを作成しようとしていて、項目は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!コメントで述べたように

+0

あなたが直面している問題は何ですか?格納されたアイテムを取得できないのですか、まったく問題がありますか? –

+0

私が望むデータを保存できますが、customarrayadapterで取得できませんでした。リストビューを実行すると、追加したアイテムは取得されません。 – Percy

+0

JSONオブジェクトでデバッグセットをチェックして確認してくださいあなたが必要とする価値を実際に得るならば。これは、あなたが書いた場所と読んでみる場所の両方で確認できます。 –

答えて

0

まず、カスタムアダプタクラスの宣言コンストラクタのように、

public customAdapter(Context context, List<String> info){ 

// do whatever you want to do 

} 

としてMainActivityでコンストラクタを呼び出し中:私は信じている

CustomAdapter adapter = new CustomAdapter(this, infoList); 
+0

私は試しましたが、私はまだ同じ問題に直面しています。私のcustomarrayadapterは私のsharedpreferenceで保存したデータを取得することができないようです。つまり、リストビューがロードされるたびに何も起こらないということです。 – Percy

+0

データが環境設定に挿入されているかどうかチェックしましたか? – Chordin4tion

+0

チェックしたところ、データを問題なく環境設定に保存できるようです。私は主なアクティビティでデータを取得しようとしてチェックしましたが、正常に動作しました。しかし、今私の問題は私がカスタムアレイアダプタでそれを取り出すことができなかったことです。カスタムアレイアダプタで環境設定データを取得しようとすると、何も起きません。 – Percy

0
  1. 共有設定でオブジェクトを上書きしています。おそらく、JSON配列を格納することができます。これを追加するときは、最後に と書かれた配列を取り出し、最新のオブジェクトを追加する必要があります。
  2. sqlliteを使用してデータを保存する必要があります。共有設定はそのような用途には使用できません。
関連する問題