gpt4 book ai didi

android - Android中的多列自定义ListView

转载 作者:行者123 更新时间:2023-11-30 03:16:07 24 4
gpt4 key购买 nike

我在将行动态添加到两列自定义 ListView 时遇到问题。 AsyncTask 从 Web 服务正确获取数据,但行未添加到 ListView。 add_to_list() 过程被 bing 调用,它正在显示参数,但 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) 邮箱.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();
}
}
}

}

最佳答案

动态加载数据时必须调用 adapter.notifyDataSetChanged()只需将适配器设为全局变量并将 add_to_list() 方法更改为

   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);
adapter.notifyDataSetChanged()
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
}
}

关于android - Android中的多列自定义ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20101658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com