用数据填充列表视图

Populating the Listview with data

好的,我在使用列表视图时遇到了问题。我有一个用 sharedPreferences 保存的字符串数组,我想访问它们并通过它们获取我需要的信息。保存的字符串是来自 parse.com 的对象 ID,通过它们我可以访问任何用户数据。所以我想要的是从 "fullname" 列中检索每个用户的姓名并制作一个列表,稍后我可以单击它并访问他们的个人资料。那么有人明白我的意思吗?这就是我所拥有的,我卡住了。

SharedPreferences settings = getSharedPreferences("NotificationIDs", Context.MODE_PRIVATE);
            Set<String> myStrings = settings.getStringSet("myStrings", new HashSet<String>());

            for (String s : myStrings) {


                ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
                query.getInBackground(s, new GetCallback<ParseObject>() {
                    @Override
                    public void done(ParseObject parseObject, ParseException e) {

                        String name = parseObject.getString("fullname");
                        Log.d("These are the names: ", parseObject.getString("fullname"));

                    }

                });

如您所见,我使用 "for" 来遍历所有已保存的字符串 (objectIDS)。它有效,在 logcat 中显示我想要的名称。但我需要以某种方式列出这些名称并使它们可点击。单击它会打开一个新的 activity,其中将放置用户 objectID 并检索所有数据。但这并不重要,重要的是列表。

//编辑

我这样做了,但没有用。该列表最终为空白。

public class UserListForHistory 扩展了 AppCompatActivity {

private ArrayAdapter<String> mAdapter = null;



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


    mAdapter = new ArrayAdapter<String>(getApplicationContext(),
            android.R.layout.simple_list_item_1, android.R.id.text1);

    final ListView list = (ListView) findViewById(R.id.idList);
    list.setAdapter(mAdapter);

    SharedPreferences settings = getSharedPreferences("NotificationIDs", Context.MODE_PRIVATE);
    Set<String> myStrings = settings.getStringSet("myStrings", new HashSet<String>());

    for (String s : myStrings) {


        ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
        query.getInBackground(s, new GetCallback<ParseObject>() {
            @Override
            public void done(ParseObject parseObject, ParseException e) {

                mAdapter.add(parseObject.getString("fullname"));
                Log.d("These are the names: ", parseObject.getString("fullname"));
            }
        });



    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_user_list_for_history, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

xml 文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.parseapp.eseen.eseen.UserListForHistory">



<TextView
    android:id="@android:id/text1"
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />


<ListView

    android:id="@+id/idList"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

我们知道字符串存在,如果 logcat 显示它们。您唯一需要做的就是创建一个 adapter (or any of the subclases if needed) and pass to it the list of strings that are retrieved from the preferences. You can create the list on an asynctask 来检索所有名称的列表(代码中的 for 循环),并在流程完成时通知更改