在我的 ListView 中列出我数据库中的所有值时遇到问题

Trouble listing all the values from my database in my ListView

我正在尝试将 localhost database 中的所有值列出到 ListView 中。基本上,REST 进程和后台内容发生在我的 AsyncTask class 中,所以我不能真正使用 onCreate 方法。

无论如何,这是我的代码:

public class ViewNames extends Activity {
    ListView listView;
    ArrayAdapter<String> viewAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewnames);
        listView = (ListView) findViewById(R.id.listView);
        // If I add the adapter here then I can't view the results. I want to view all the values I have as soon
        as I open up the form. But this process only happens in my AsyncTask class so I don't know how to model this
        list
        //adapter = new ArrayAdapter<String>(ViewNames.this, android.R.layout.simple_list_item_1,result);
        //listView.setAdapter(adapter);
    }
}

public class ViewNamesJSON extends AsyncTask<String, String, String> {  

    ... //Other methods go here. They work fine.

    @Override
    // This is where it doesn't work. If I display the adapter in onCreate then I have an empty list.
    // But I want to display the results of all the values after the user has clicked the button.
    protected void onPostExecute(String s) {
        List<String> namesList = new List<String>();
        try {
            JSONObject jsonObject = new JSONObject(s);
            JSONArray jsonArray = jsonObject.optJSONArray("names");

            for(int i=0; i < jsonArray.length(); i++){
                JSONObject ones = jsonArray.getJSONObject(i);
                String names = ones.optString("names");
                namesList.add(names);
            }
            String[] result = namesList.toArray(new String[namesList.size()]);
            adapter = new ArrayAdapter<String>(ViewNames.this, android.R.layout.simple_list_item_1,result);
            listView.setAdapter(adapter);
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

我的 XML 文件:

<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".ViewNames"
    android:orientation="vertical"
    >

    <Button
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:text="Add Names"
        android:id="@+id/addNamesButton"
        android:layout_marginTop="10dp"
        android:layout_gravity="center_horizontal" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_gravity="center_horizontal" />

</LinearLayout>

我遇到了一些困难,因为我想立即将数据库中的所有内容显示到 ListView 中,然后在用户单击按钮时更新 ListView

如有任何帮助,我们将不胜感激。

您可以在onCreate中定义和添加适配器。这只是在任务完成时调用 notifyDataSetChanged 的问题。

您可以按如下方式进行,将 List 传递给适配器而不是数组,这样 Activity 和适配器共享同一个集合。

public class ViewNames extends Activity {

    ListView listView;
    List<String> result;
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewnames);
        listView = (ListView) findViewById(R.id.listView);
        result = new ArrayList<String>();
        adapter = new ArrayAdapter<String>(ViewNames.this, android.R.layout.simple_list_item_1,result);
        listView.setAdapter(adapter);

        (new ViewNamesJSON()).execute();
    }

    public class ViewNamesJSON extends AsyncTask<String, String, String> {

        // Other methods go here. They work fine.

        @Override
        protected void onPostExecute(String s) {
            List<String> namesList = new ArrayList<String>();
            try {
                JSONObject jsonObject = new JSONObject(s);
                JSONArray jsonArray = jsonObject.optJSONArray("names");

                for(int i=0; i < jsonArray.length(); i++){
                    JSONObject ones = jsonArray.getJSONObject(i);
                    String names = ones.optString("names");
                    namesList.add(names);
                }
                result.clear();
                result.addAll(namesList);
                adapter.notifyDataSetChanged();
            }
            catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

}

希望这对您有所帮助。