本文介绍了调用notifyDataSetChanged()后,Android ListView无法更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个自定义列表适配器,并通过Asynctask获取数据以从Web服务器获取数据.
I have a custom list adapter and get data to fill it via a Asynctask that gets data from a webserver.
这一切似乎都很好,但是当我调用notifyDataSetChanged()
时,列表视图确实显示以下信息是我的代码:
This all seems to work fine however when I call notifyDataSetChanged()
the listview does display the information below is my code:
public class my_Activity extends Activity {
...
ListView accounts;
static MyListAdapter m_adapter;
private ArrayList<Account> accountslist;
LoadAllAccounts l = new LoadAllAccounts();
private ProgressDialog pDialog;
int userID;
JSONParser jParser = new JSONParser();
JSONArray products = null;
ArrayList<HashMap<String, String>> productsList;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.account);
SharedPreferences perfs = getSharedPreferences(CFG.PREFS_NAME, 0);
userID = perfs.getInt("USERID", 0);
accounts = (ListView) findViewById(R.id.accounts_list);
productsList = new ArrayList<HashMap<String, String>>();
l.execute();
accountslist = new ArrayList<Account>();
m_adapter = new MyListAdapter(this, R.layout.list_row_layout,
accountslist);
accounts.setAdapter(m_adapter);
accounts.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Account Item = (Account) (accounts.getItemAtPosition(arg2));
Intent i = new Intent(Account_Activity.this,
Balance_Activity.class);
i.putExtra("Account", Item.getID());
startActivityForResult(i, 1000);
}
});
...
}
class MyListAdapter extends ArrayAdapter<Account> {
private ArrayList<Account> items;
public MyListAdapter(Context context, int textViewResourceId,
ArrayList<Account> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_row_layout, null);
}
Account o = items.get(position);
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.ptitle);
TextView bb = (TextView) v.findViewById(R.id.balance);
if (tt != null) {
tt.setText(Integer.toString(o.getID()));
}
if (bb != null) {
bb.setText("Balance: " + o.getBalance());
}
}
return v;
}
}
class LoadAllAccounts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Account_Activity.this);
pDialog.setMessage("Loading Accounts. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("USERID", Integer
.toString(userID)));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(CFG.url_all_accounts,
"GET", params);
try {
// Checking for SUCCESS TAG
int success = json.getInt(CFG.TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray("lists");
productsList.clear();
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString("ACCOUNTID");
String name = c.getString("BALANCE");
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("ACCOUNTID", id);
map.put("BALANCE", name);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
}
} catch (JSONException e) {
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
for (int i = 0; i < productsList.size(); i++) {
Account list = new Account(
Integer.parseInt(productsList.get(i).get(
"ACCOUNTID")),
Integer.parseInt(productsList.get(i).get(
"BALANCE")));
accountslist.add(list);
}
m_adapter.clear();
for (int i = 0; i < accountslist.size(); i++) {
m_adapter.add(accountslist.get(i));
}
m_adapter.notifyDataSetChanged();
}
});
}
}
}
我的帐户类别:
public class Account {
int accountID;
int Balance;
Account(int id, int b){
accountID = id;
Balance = b;
}
int getID(){
return accountID;
}
int getBalance(){
return Balance;
}
public String toString(){
return "Account object with ID: "+accountID+" and balance: "+Balance;
}
}
和列表行布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/ptitle"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<TextView
android:id="@+id/ptitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".70"
android:paddingBottom="15dp"
android:paddingLeft="18dp"
android:paddingTop="15dp"
android:text="test"
android:textSize="20sp" />
<TextView
android:id="@+id/balance"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".70"
android:text="TextView" />
</LinearLayout>
推荐答案
尝试一下..
删除OnCreate()
accountslist = new ArrayList<Account>();
m_adapter = new MyListAdapter(this, R.layout.list_row_layout,
accountslist);
accounts.setAdapter(m_adapter);
将其添加到onPostExecute
内,如下所示
Add that inside onPostExecute
like below
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
accountslist = new ArrayList<Account>();
for (int i = 0; i < productsList.size(); i++) {
Account list = new Account(
Integer.parseInt(productsList.get(i).get(
"ACCOUNTID")),
Integer.parseInt(productsList.get(i).get(
"BALANCE")));
accountslist.add(list);
}
m_adapter = new MyListAdapter(this, R.layout.list_row_layout, accountslist);
accounts.setAdapter(m_adapter);
}
});
}
这篇关于调用notifyDataSetChanged()后,Android ListView无法更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!