[英]Java: Sorting Data
我正在開發一個健康和健身應用程序,它可以使用共享偏好來存儲用戶信息。 這些可以稍后由用戶查看。 我可以這樣做並將信息顯示在列表視圖中。 當用戶單擊列表視圖中的項目時,它將加載顯示信息的單獨活動。 我的問題是,由於我使用的是sharedprefrences,因此我認為信息始終是隨機的。 我的問題是可以對這些信息進行排序,使其始終具有相同的順序嗎?
我的應用工作的圖片:
此處的圖像是用戶輸入運動信息然后使用“保存”按鈕存儲的屏幕。
碼:
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
}
}
下一個圖像是列表中顯示的信息。
碼:
@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);
}
最后一個圖像是用戶單擊列表中的項目時的活動。 它從該密鑰加載所有信息。
碼:
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);
}
}
使用捆綁包將數據傳遞到新活動。 那么是否有可能對這些數據進行排序,使其顯示為日期,運動名稱,開始時間,結束時間和最后丟失的卡路里? 截至目前,數據始終總是在每個新日志條目中隨機顯示。
我建議通過sqlite使用日期數據庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.