列表适配器接收对象,但它不会在列表视图中显示它们

时间:2016-07-26 09:27:46

标签: arrays listview hashmap listadapter

所以,在这种情况下,在onPostExecute方法中我使用了ListView,并且我在其中调整了一个列表适配器,以便在列表视图中显示对象(事件)。问题是适配器识别对象但它不会在ListView中显示它们。我已经看过如何使用List适配器填充一个Hashmaps数组,我确信我已经在我的代码中正确完成了它。我认为问题可能出在xml文件中。任何可以指导我正确方向的想法?

这是我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</ListView>

<TextView
    android:id="@+id/id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:paddingTop="6dip"
    android:textSize="16sp"/>


<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip" />

<TextView
    android:id="@+id/date"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="start"/>

<TextView
    android:id="@+id/time"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="start" />

<TextView
    android:id="@+id/time_end"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="start" />

</LinearLayout>

.java文件:

public class EventsJSONActivity extends ListActivity {

    private static String url = "http://mob.students.acg.edu/json3.php";

    public static final String TAG_EVENT_INFO = "eventsinfo";
    public static final String TAG_ID = "id";
    public static final String TAG_TITLE = "title";
    public static final String TAG_DATE = "date";
    public static final String TAG_TIME = "time";
    public static final String TAG_TIME_END = "time_end";


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

        new GetEvents().execute();
    }

    class GetEvents extends AsyncTask<Void,Void,Void> {
        ArrayList<HashMap<String,String>> eventList;
        ProgressDialog proDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            proDialog = new ProgressDialog(EventsJSONActivity.this);
            Log.d("prodialog: ","Setting Message.");
            proDialog.setMessage("Please wait...");
            Log.d("prodialog: ","Setting cancellable.");
            proDialog.setCancelable(false);
            Log.d("prodialog: ","Showing...");
            proDialog.show();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            WebRequest webreq = new WebRequest();
            String url = "http://mob.students.acg.edu/json3.php";
            String jsonStr = webreq.makeWebServiceCall(url,WebRequest.POSTRequest);

            Log.d("Response: ", "> " + jsonStr);

            eventList = ParseJSON(jsonStr);
            Log.d("eventList info: " , eventList.toString());

            return null;
        }
        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        @Override
        protected void onPostExecute(Void requestresult) {
            super.onPostExecute(requestresult);

            if (proDialog.isShowing()) {
                proDialog.dismiss();
                Log.d("prodialog: ","dismissing dialog.");
            }

            Log.d("Listadapter: ", "eventList: " + eventList.toString());
            ListAdapter adapter = new SimpleAdapter(
                    EventsJSONActivity.this,eventList,
                    android.R.layout.simple_list_item_1,
                    new String[] {TAG_ID, TAG_TITLE, TAG_DATE, TAG_TIME, TAG_TIME_END},
                    new int[] {R.id.id,R.id.title,R.id.date,R.id.time,R.id.time_end});
            Log.d("Listadapter: ","data fed");
            ListView lv = (ListView)findViewById(android.R.id.list);
            lv.setAdapter(adapter);
            Log.d("Listadapter: ",adapter.getCount() + " items set");

        }//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


        private ArrayList<HashMap<String,String>> ParseJSON(String json) {

            if (json != null) {
                try {
                    ArrayList<HashMap<String,String>> eventList = new ArrayList<>();
                    JSONObject jsonObj = new JSONObject(json);

                    JSONArray events = jsonObj.getJSONArray(TAG_EVENT_INFO);

                    for (int i = 0; i < events.length(); i++) {
                        JSONObject j = events.getJSONObject(i);

                        String id = j.getString(TAG_ID);
                        String title = j.getString(TAG_TITLE);
                        String date = j.getString(TAG_DATE);
                        String time = j.getString(TAG_TIME);
                        String time_end = j.getString(TAG_TIME_END);

                        HashMap<String,String> event = new HashMap<String,String>();

                        event.put(TAG_ID,id);
                        event.put(TAG_TITLE,title);
                        event.put(TAG_DATE,date);
                        event.put(TAG_TIME,time);
                        event.put(TAG_TIME_END,time_end);

                        /*event.put(TAG_ID,"id");
                        event.put(TAG_TITLE,"title");
                        event.put(TAG_DATE,"date");
                        event.put(TAG_TIME,"time");
                        event.put(TAG_TIME_END,"time_end");*/

                        eventList.add(event);
                    }
                    Log.d("EventList > ", eventList.toString());
                    return eventList;

                } catch (JSONException e) {
                    e.printStackTrace();
                    return null;
                }
            } else {
                Log.e("ServiceHandler", "No data received from HTTP Request");
                return null;
            }
        }
    }


}

1 个答案:

答案 0 :(得分:0)

尝试检查此处发布的解决方案... updating ListView from AsyncTaskrunOnUiThread runnable可能就是你所缺少的,因为你在这里的代码看起来是正确的。如果这不起作用,您可以尝试为listview和/或适配器创建类级别变量,并在AsyncTask的onPostExecute中修改它们。

希望这有帮助!