2016-12-17 14 views
0

私は、共有情報を使用してユーザー情報を格納できる健康およびフィットネスアプリケーションを開発しています。これらは後でユーザーが見ることができます。私はこれを行い、情報をリストビューの中に戻すことができます。ユーザーがリストビュー内の項目をクリックすると、情報を表示する別のアクティビティーがロードされます。私の問題は、情報が常にランダムな順序であることです。私はsharedprefrencesを使用しているためです。私の質問は、それは常に同じ順序を持つようにこの情報を並べ替えることが可能ですか?私のアプリの作業のJava:データの並べ替え

画像:

enter image description here

ここで画像は、ユーザーが運動の情報を入力し、それがその後、保存ボタンを使用して保存されている画面です。

コード:

public void saveLog (View view){ 

    EditText Date = (EditText)findViewById(R.id.editDate); 
    EditText Name = (EditText)findViewById(R.id.editName); 
    EditText Cal = (EditText)findViewById(R.id.editCal); 
    EditText STime = (EditText)findViewById(R.id.editSTime); 
    EditText ETime = (EditText)findViewById(R.id.editETime); 
    EditText Entry = (EditText)findViewById(R.id.editEntry); 

    try { 
     SharedPreferences userInfo = getSharedPreferences("userData", Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor = userInfo.edit(); 
     Set<String> LogSet = new HashSet<String>(); 

     LogSet.add("Date: "+Date.getText().toString()+"\n"); 
     LogSet.add("Exercise: "+Name.getText().toString()+"\n"); 
     LogSet.add("Start Time: "+STime.getText().toString()+"\n"); 
     LogSet.add("End Time: "+ETime.getText().toString()+"\n"); 
     LogSet.add("Calories Lost: "+Cal.getText().toString()+"\n"); 
     editor.putStringSet(Entry.getText().toString(), LogSet); 
     editor.commit(); 

     // show toast message for successful save 
     Context context = getApplicationContext(); 
     CharSequence text = "User data saved!"; 
     int duration = Toast.LENGTH_LONG; 
     Toast toast = Toast.makeText(context, text, duration); 
     toast.show(); 
     String Grab = userInfo.getString ("Date" , Date.getText().toString()); 


    } 
    catch(Exception ex) { 
     // insert your own error message here 
    } 


} 

次画像がリスト内に表示される情報です。

enter image description here

コード:

 @Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_log); 

    // Get ListView object from xml 
    listView = (ListView) findViewById(R.id.list); 
    listView.setFriction(ViewConfiguration.getScrollFriction() * 0); 
    // ListView Item Click Listener 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 


     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      int itemPosition  = position; 
      String itemValue = (String) listView.getItemAtPosition(position); 
      SharedPreferences userInfo = getSharedPreferences("userData", Context.MODE_PRIVATE); 
      SharedPreferences.Editor editor = userInfo.edit(); 
      String myString = userInfo.getAll().keySet().toString(); 
      String[] values = new String[] { myString }; 


      Intent i = new Intent(ActivityLog.this, Log_Data.class); 
      //Create the bundle 
      Bundle bundle = new Bundle(); 
      //Add your data from getFactualResults method to bundle 
      bundle.putString("TAG", itemValue); 
      //Add the bundle to the intent 
      i.putExtras(bundle); 
      startActivity(i); 
      // ListView Clicked item index 

      // ListView Clicked item value 

      // Show Alert 
      //Toast.makeText(getApplicationContext(), "Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG).show(); 

     } 

    }); 

} 


@Override 
protected void onStart() 
{ 
    SharedPreferences userInfo = getSharedPreferences("userData", Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = userInfo.edit(); 
    String myString = userInfo.getAll().keySet().toString(); 
    // String[] values = new String[] { myString }; 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item, android.R.id.text1); 


    Map<String, ?> allEntries = userInfo.getAll(); 
    for (Map.Entry<String, ?> entry : allEntries.entrySet()) { 


     Log.d("map values", entry.getKey() + ": " + entry.getValue().toString()); 
     adapter.add(entry.getKey()+ ": " + entry.getValue().toString()); 
     listView.setAdapter(adapter); 

    } 
    super.onStart(); 
} 


public void showLog (View view){ 

    Intent intent = new Intent(this, ActivityNewLog.class); 
    startActivity(intent); 

} 


public void deleteLog (View view){ 

    SharedPreferences settings = getSharedPreferences("userData", Context.MODE_PRIVATE); 
    settings.edit().clear().commit(); 
    getSharedPreferences("userData", 0).edit().clear().commit(); 

} 

public void dataShow (View view){ 

    Intent intent = new Intent(this, Log_Data.class); 
    startActivity(intent); 

} 

と最後の画像、ユーザがリスト内のアイテムをクリックしたときに活性です。そのキーからすべての情報を読み込みます。

enter image description here

コード:新しいアクティビティにデータを渡すために、バンドルを使用した

public class Log_Data extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_log__data); 
    TextView data = (TextView)findViewById(R.id.textView5); 

    Bundle bundle = getIntent().getExtras(); 
    //Extract the data… 
    String something = bundle.getString("TAG"); 
    //Create the text view 
    data.setText(something); 


    } 
} 

。それで、このデータをソートして、日付、運動の名前、開始時間、終了時間、および最後に失われたカロリーとして表示されると言うでしょうか?現時点では、データは常に新しいログエントリごとにランダムに表示されます。

+3

環境設定の代わりにローカルdb(sqlite)を使用します。 – androidnoobdev

+0

まあ、私はちょうどそれが現在の状態で情報を並べ替えることが可能であることを知りたいのプリファレンスの使用を変更したいですか? –

+2

セットは本質的に順不同です。代わりにリストを使用してください。 – wvdz

答えて

0

sqliteで日付ベースを使用することをお勧めします。

関連する問題