0

我遇到了两个问题。

1) 如何让我的 row.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() 方法,假设检索两个字符串值并将其放在行中?有任何想法吗?

4

2 回答 2

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 个条目。

于 2012-07-17T19:31:32.007 回答
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);




}

}

于 2018-05-23T09:38:38.970 回答