2016-12-17 41 views
0

我正在开发一个健康和健身应用程序,可以使用sharedprefrences存储用户信息。这些可以稍后由用户查看。我可以做到这一点,并在列表视图中显示信息。当用户点击列表视图中的项目时,它将加载显示信息的单独活动。我的问题是信息始终是随机排列的,我假设因为我正在使用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

使用本地数据库(sqlite)而不是首选项。 – androidnoobdev

+0

以及我不想改变偏好的使用,我只是想知道是否有可能根据当前状态排序信息? –

+2

一组本身是无序的。改用列表。 – wvdz

回答

0

我会推荐使用数据库通过sqlite。