我將動態添加到兩列自定義ListView的問題。 AsyncTask從Web服務正確獲取數據,但行不會添加到ListView。 add_to_list()過程是bing調用的,它正在顯示參數,但ListView沒有得到更新。請指導我的代碼有什麼問題。Android中的多列自定義ListView
1)mail_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/cell_borders"
android:orientation="horizontal"
android:paddingBottom="1dip"
android:paddingTop="1dip"
android:weightSum="4"
tools:ignore="HardcodedText" >
<TextView
android:id="@+id/msgTime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1.5"
android:text="Time" />
<TextView
android:id="@+id/msgText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="2.5"
android:text="Message" />
</LinearLayout>
2)mail_box.xml
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:orientation="vertical"
android:padding="5dp"
tools:ignore="HardcodedText" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:ignore="UselessParent" >
<Button
android:id="@+id/bBack_Mail"
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Back"
tools:ignore="HardcodedText" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/darker_gray" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/cell_borders"
android:orientation="horizontal"
android:paddingBottom="1dip"
android:paddingTop="1dip"
android:weightSum="4"
tools:ignore="HardcodedText" >
<TextView
android:id="@+id/tvTime_Mail"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:gravity="center"
android:text="Time" />
<TextView
android:id="@+id/tvText_Mail"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2.5"
android:gravity="center"
android:text="Message" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/darker_gray" />
<ListView
android:id="@+id/lstMail_Mail"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
3)Mail_Box.java
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class Mail_Box extends Activity implements View.OnClickListener {
private ListView list;
private ArrayList<HashMap<String, String>> mylist;
private HashMap<String, String> map;
private String url = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //Here is a valid URL
private String urlSearch = null;
private Button bBack;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mail_box);
this.initializer();
SimpleAdapter mSchedule = new SimpleAdapter(this, mylist,
R.layout.mail_row,
new String[] { "mail_time", "mail_message" }, new int[] {
R.id.tvTime_Mail, R.id.tvText_Mail });
list.setAdapter(mSchedule);
this.urlSearch = this.url + "rashid";
MyTask runner = new MyTask();
runner.execute(this.url);
}
private void initializer() {
bBack = (Button) findViewById(R.id.bBack_Mail);
bBack.setOnClickListener(this);
list = (ListView) findViewById(R.id.lstMail_Mail);
mylist = new ArrayList<HashMap<String, String>>();
}
@Override
public void onClick(View v) {
finish();
}
private void add_to_list(String tm, String msg) {
//This message is shown properly
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
try {
map = null;
map = new HashMap<String, String>();
map.put("mail_time", tm);
map.put("mail_message", msg);
mylist.add(map);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
}
}
// --------------AsyncTask Class--------------------------------
private class MyTask extends AsyncTask<String, Void, String> {
private String msg = "";
private JSONParser jParser;
private JSONObject json;
private static final String TAG_DATA = "data";
private static final String TAG_TIME = "MAIL_TIME";
private static final String TAG_TYPE = "TYPE";
private static final String TAG_TEXT = "MSG_TEXT";
private JSONArray data = null;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog = ProgressDialog.show(Mail_Box.this,
"Loading Mails", "Please wait for a while.", true);
}
@Override
protected String doInBackground(String... urls) {
try {
this.jParser = new JSONParser();
this.json = jParser.getJSONFromUrl(urls[0]);
if (this.json != null)
msg = "DONE";
} catch (Exception e) {
e.printStackTrace();
}
Log.d("Response: ", this.msg);
return msg;
}
@Override
protected void onPostExecute(String msg) {
progressDialog.dismiss();
if (msg.equals("DONE")) {
try {
data = json.getJSONArray(TAG_DATA);
int i = data.length();
if (i == 0) {
Toast.makeText(getApplicationContext(),
"No record found.", Toast.LENGTH_SHORT).show();
} else {
for (int j = 0; j < i; j++) {
JSONObject c = data.getJSONObject(j);
String tm = c.getString(TAG_TIME);
String type = c.getString(TAG_TYPE);
String txt = c.getString(TAG_TEXT);
add_to_list(tm, txt);
}
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Error",
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(),
"Unable to search record.", Toast.LENGTH_SHORT).show();
}
}
}
}
沒有自定義適配器? – hasan83
我發現這種方式在大多數情況下更方便。 –