无法在ListView中列出我数据库中的所有值

时间:2016-02-13 16:44:04

标签: java android json listview android-arrayadapter

我正在尝试将localhost database中的所有值列入ListView。基本上,REST进程和背景内容发生在我的AsyncTask类中,因此我无法真正使用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

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:4)

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

您可以通过将List传递给适配器而不是数组来执行以下操作,以便Activity和Adapter共享相同的集合。

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();
            }
        }
    }

}

希望这可以提供帮助。