我遇到了两个问题。
1)如何让我的list.xml显示在listview.xml中?
在我的java类中,我扩展了ListActivity并使用代码:
ListView listView = (ListView)findViewById(R.id.list);
ArrayAdapter<?> arrayAdapter = new ArrayAdapter<?>(this, R.layout.row
我不知道如何完成这个。
2)如何将我保存在sharedPReference上的数据加载到我的行中,这是两个字符串值?
保存两个字符串值的代码是:
String product = "Health Vitamins";
String category = "Nutrition";
savePreference("NAME", product);
savePreference("NUTRITION", category);
然后我在savePreference方法中使用putString(key,value)和commit()。当我想加载数据时,我只需使用loadPreference(),它将使用getString(“NAME”,“”)和getString(“NUTRITION”,“”)并检索数据。
问题是如何将我检索到的数据实际放入listview row.xml?例如,一旦调用了loadPreference()方法,就可以检索两个字符串值并将其放在行中?任何想法?
答案 0 :(得分:0)
String[] data = //load the array here.
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, data);
无论你想把什么放进你的阵列。它可能是您的共享偏好值。但是,为什么你会使用共享偏好,因为它使用键值对。并且你不能在键上加倍意味着你的列表总是有1个或最多2个条目。
答案 1 :(得分:0)
您可以使用Intent
从SharedPreferences将数据传递到ListView公共类MainActivity扩展AppCompatActivity实现了View.OnClickListener {
private EditText nameEditText, rollEditText, deptEditText, semesterEditText;
private Button submittButton;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEditText = findViewById(R.id.nameEditTextID);
rollEditText = findViewById(R.id.rollEditTextID);
deptEditText = findViewById(R.id.deptEditTextID);
semesterEditText = findViewById(R.id.semesterEditTextID);
submittButton = findViewById(R.id.submitButtonID);
textView = findViewById(R.id.textViewID);
submittButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.submitButtonID) {
String userName = nameEditText.getText().toString();
String userRoll = rollEditText.getText().toString();
String userDept = deptEditText.getText().toString();
String userSemester = semesterEditText.getText().toString();
if (userName.equals("") && userRoll.equals("") && userDept.equals("") && userSemester.equals("")) {
Toast.makeText(getApplicationContext(), "Please enter data", Toast.LENGTH_SHORT).show();
} else {
SharedPreferences sharedPreferences = getSharedPreferences("Details", 0);
//SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("userNameKey", userName);
editor.putString("userRollKey", userRoll);
editor.putString("userDeptKey", userDept);
editor.putString("userSemesterKey", userSemester);
editor.apply();
nameEditText.setText("");
rollEditText.setText("");
deptEditText.setText("");
semesterEditText.setText("");
Toast.makeText(getApplicationContext(), "Saved successfully", Toast.LENGTH_SHORT).show();
if (sharedPreferences.contains("userNameKey") && sharedPreferences.contains("userRollKey") && sharedPreferences.contains("userDeptKey")
&& sharedPreferences.contains("userSemesterKey")) {
Intent intent = new Intent(MainActivity.this, ListDataActivity.class);
startActivity(intent);
}
/*if (sharedPreferences.contains("userNameKey") && sharedPreferences.contains("userRollKey") && sharedPreferences.contains("userDeptKey")
&& sharedPreferences.contains("userSemesterKey")) {
String name = sharedPreferences.getString("userNameKey", "Data not found");
String roll = sharedPreferences.getString("userRollKey", "Data not found");
String department = sharedPreferences.getString("userDeptKey", "Data not found");
String semester = sharedPreferences.getString("userSemesterKey", "Data not found");
Toast.makeText(getApplicationContext(), "Loaded successfully", Toast.LENGTH_SHORT).show();
textView.setText(name + "\n" + roll + "\n" + department + "\n" + semester);
}*/
}
}
}
公共类ListDataActivity扩展了AppCompatActivity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_data);
listView = findViewById(R.id.listViewID);
SharedPreferences sharedPreferences = getSharedPreferences("Details", 0);
String name = sharedPreferences.getString("userNameKey", "Data not found");
String roll = sharedPreferences.getString("userRollKey", "Data not found");
String department = sharedPreferences.getString("userDeptKey", "Data not found");
String semester = sharedPreferences.getString("userSemesterKey", "Data not found");
Toast.makeText(this, "Roll is:" + roll, Toast.LENGTH_SHORT).show();
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add(name);
arrayList.add(roll);
arrayList.add(department);
arrayList.add(semester);
ArrayAdapter<String>arrayAdapter = new ArrayAdapter<String>(this,R.layout.list_item,R.id.sampleTextViewID,arrayList);
listView.setAdapter(arrayAdapter);
}
}