こんにちは、同じレイアウトで 2 つのリスト ビューを表示する必要がありますが、問題のトラブルシューティング方法でいくつかの課題に直面しています。誰かが私を助けてくれれば本当にありがたいです。これは私のコードです
アクティビティ_メイン
<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 を持つ別のリストビューを追加する必要があります。基本的に、リストビューは以下のコードを使用してデータを取得することです。私は 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);
}
}
};
};