0

嗨,我需要在同一布局上显示两个列表视图,但我在如何解决问题方面面临一些挑战。如果有人可以帮助我,我将不胜感激。这是我的代码

Activity_Main

<TextView
    android:id="@+id/test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Latest Events"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView 
    android:id="@+id/downloading"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="Downloading ... "
    android:layout_gravity="center_vertical|center_horizontal"
    android:gravity="center_vertical|center_horizontal"/>    


<ListView
    android:id="@+id/list_view"
    android:layout_width="fill_parent"
    android:layout_height="270dp" >
</ListView>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Recommendation"
    android:textAppearance="?android:attr/textAppearanceMedium" />

    <ListView
    android:id="@+id/list_view2"
    android:layout_width="fill_parent"
    android:layout_height="100dp" >
</ListView>

主要活动

在这个主要活动中,我需要添加另一个具有不同 url 的列表视图来获取不同的数据。所以基本上,listview是使用下面的代码获取数据,我声明listview2,即获取其他数据。有人知道我如何在里面实现这个方法吗?我尝试声明 ListView2,但是当我运行应用程序时。程序自动上传 listview 2 上的数据,将第一个 listview 留空。

package com.farid.eventauto;

  public class MainActivity extends Activity {

private static final String TAG = "MainActivity";
// Button button;
static String json = "";
static JSONObject jObj = null;
TextView downloading;



ListView listView;
ListView listView2;

static final String[] MOBILE_OS = new String[] { "Android", "iOS",
        "WindowsMobile", "Blackberry" };
ArrayList<SingaPoreEvents> singaPoreEvents = new ArrayList<SingaPoreEvents>();
JSONArray events = null;

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

    //ListView 1
    listView = (ListView) findViewById(R.id.list_view);

    //ListView 2
     listView2 = (ListView) findViewById(R.id.list_view2);


    downloading = (TextView) findViewById(R.id.downloading);
    // button = (Button) findViewById(R.id.get);
    /*
     * button.setOnClickListener(new OnClickListener() {
     * 
     * @Override public void onClick(View arg0) {} });
     */
    buttonClick();

}

private void buttonClick() {

    new Thread(new Runnable() {
        @Override
        public void run() {
            String imgUrl = "";
            String imgUrlSmall = "";
            String imgUrlMedium = "";
            String imgUrlThumb = "";
            Drawable eventDrawable;
            jObj = sendReq();
            events = jObj.names();
            Log.e(TAG, "size of events array -->" + events.length());
            /*
             * for (int i = 0; i < events.length(); i++) { try { Log.e(TAG,
             * "-->"+events.getString(i)); } catch (JSONException e) { //
             * TODO Auto-generated catch block e.printStackTrace(); } }
             */
            try {
                /*
                 * Log.e(TAG, "events arrray lenght-->" +
                 * jObj.getJSONObject("events").length());
                 */
                JSONArray eventsArray = jObj.getJSONObject("events")
                        .getJSONArray("event");
                for (int i = 0; i < eventsArray.length(); i++) {
                    JSONObject c = eventsArray.getJSONObject(i);
                    /*
                     * Log.e(TAG, "event title-->" + c.getString("title"));
                     */
                    if (!c.getString("image").equals("null")) {
                        JSONObject te = c.getJSONObject("image");
                        JSONArray ar = te.names();
                        for (int j = 0; j < ar.length(); j++) {
                            Log.e(TAG, "++++++>" + ar.getString(j));
                        }
                        if (!c.getString("image").equals("null")) {
                            imgUrl = c.getJSONObject("image").getString(
                                    "url");
                            if (!c.getJSONObject("image")
                                    .getString("small").equals("null")) {
                                imgUrlSmall = c.getJSONObject("image")
                                        .getJSONObject("small")
                                        .getString("url");
                            } else {
                                imgUrlSmall = "";
                            }
                            if (!c.getJSONObject("image")
                                    .getString("medium").equals("null")) {
                                imgUrlMedium = c.getJSONObject("image")
                                        .getJSONObject("medium")
                                        .getString("url");
                            } else {
                                imgUrlMedium = "";
                            }
                            if (!c.getJSONObject("image")
                                    .getString("thumb").equals("null")) {
                                imgUrlThumb = c.getJSONObject("image")
                                        .getJSONObject("thumb")
                                        .getString("url");
                            } else {
                                imgUrlThumb = "";
                            }

                            // eventDrawable =
                            // drawableFromUrl(imgUrl);
                            eventDrawable = null;
                        } else {
                            imgUrl = "";
                            imgUrlMedium = "";
                            imgUrlThumb = "";
                            eventDrawable = null;
                        }
                        singaPoreEvents.add(new SingaPoreEvents(c
                                .getString("title"), imgUrl, c
                                .getString("description"), imgUrlMedium,
                                imgUrlMedium, imgUrlThumb));
                    }
                }
                handler.sendEmptyMessage(UPDATE_LIST_VIEW);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }).start();

}

private JSONObject sendReq() {
    JSONObject jObj = null;
    try {

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet getRequest = new HttpGet(
                "http://api.eventful.com/json/events/search?app_key=42t54cX7RbrDFczc&location=singapore&page_size=50");
        getRequest.addHeader("accept", "application/json");

        HttpResponse response = httpClient.execute(getRequest);

        if (response.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + response.getStatusLine().getStatusCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(
                (response.getEntity().getContent())));
        StringBuilder sb = new StringBuilder();
        String line = null;
        Log.e(TAG, "Output from Server .... \n");
        while ((line = br.readLine()) != null) {
            sb.append(line + "\n");
        }
        json = sb.toString();
        // Log.e(TAG, "------->" + json);
        httpClient.getConnectionManager().shutdown();

        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return jObj;
}

private static final int UPDATE_LIST_VIEW = 1;
Handler handler = new Handler() {
    public void handleMessage(android.os.Message msg) {
        if (msg.what == UPDATE_LIST_VIEW) {
            Log.e(TAG,
                    "lenght of singapore events-->"
                            + singaPoreEvents.size());
            if (singaPoreEvents.size() > 0) {
                downloading.setVisibility(View.GONE);
                listView.setAdapter(new LazyAdapter(MainActivity.this,
                        singaPoreEvents));
                listView.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {
                        startActivity(new Intent(getApplicationContext(),
                                DetailActivity.class)
                                .putExtra(
                                        DetailActivity.event_title,
                                        singaPoreEvents.get(arg2)
                                                .getTitle())
                                .putExtra(
                                        DetailActivity.event_description,
                                        singaPoreEvents.get(arg2)
                                                .getDescritption())
                                .putExtra(
                                        DetailActivity.event_img_url,
                                        singaPoreEvents.get(arg2)
                                                .getEventDrawableMedium()));
                    }
                });
            }else{
                downloading.setVisibility(View.VISIBLE);

            }
        }
    };
};
4

1 回答 1

0

您应该为您的 ListView 控件创建适配器。喜欢这里

于 2013-10-23T06:26:43.750 回答