[英]ListView won't refresh using notifyDataSetChanged
I'm trying to refresh the listview from the adapter class ( non activity class) I tried notifyDataSetChanged()
but it wont work , I also tried to call the displayListView()
in the adapter class but it crashes as well 我正在尝试从适配器类(非活动类)刷新listview,我尝试了notifyDataSetChanged()
但它不起作用,我也试图在适配器类中调用displayListView()
,但它也崩溃了
this is my displayListView()
Method in The ActivityClass 这是我在ActivityClass中的 displayListView()
方法
Cursor cursor = dbHelper.fetchAllCountries();
// The desired columns to be bound
String[] columns = new String[]{
DBAdapter.qty,
DBAdapter.price,
DBAdapter.PRODUCT_NAME
};
// the XML defined views which the data will be bound to
int[] to = new int[]{
R.id.qtyL,
R.id.priceL,
R.id.PRODUCT_NAMEL
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.country_inf,
cursor,
columns,
to,
0);
HorizontalListView listView = (HorizontalListView) findViewById(R.id.cartCeckOutList);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
dataAdapter.notifyDataSetChanged();
And this is the Adapter Class I use 这是我使用的适配器类
package com.abdullahadhaim.finegrillresturant.adater;
public class CustomListAdapter extends BaseAdapter {
Context c;
// DBHelper myDataBaseHelper ;
private Activity activity;
private LayoutInflater inflater;
private List<Movie> movieItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
DBAdapter myADapterDB;
WaiterActivity waiterAct;
public CustomListAdapter(Activity activity, List<Movie> movieItems) {
this.activity = activity;
this.movieItems = movieItems;
}
@Override
public int getCount() {
return movieItems.size();
}
@Override
public Object getItem(int location) {
return movieItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView
.findViewById(R.id.thumbnail);
final TextView title = (TextView) convertView.findViewById(R.id.title);
TextView price = (TextView) convertView.findViewById(R.id.rating);
// myDataBaseHelper=new DBHelper(activity, "CDB", null, 1);
myADapterDB = new DBAdapter(activity);
myADapterDB.open();
waiterAct = new WaiterActivity();
// getting movie data for the row
final Movie m = movieItems.get(position);
// thumbnail image
thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
// title
title.setText(m.getTitle());
// price
price.setText("Price: " + "SR " + String.valueOf(m.getPrice()));
Button add = (Button) convertView.findViewById(R.id.button1);
Button plus = (Button) convertView.findViewById(R.id.plus_butt);
Button minus = (Button) convertView.findViewById(R.id.minus_butt);
final EditText qty = (EditText) convertView
.findViewById(R.id.editText1);
plus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String the_qty = qty.getText().toString();
int my_qty = Integer.parseInt(the_qty) + 1;
qty.setText(my_qty + "");
}
});
minus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String the_qty = qty.getText().toString();
int my_qty = Integer.parseInt(the_qty) - 1;
qty.setText(my_qty + "");
}
});
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int myQTY = Integer.parseInt(qty.getText().toString());
String myTitle = m.getTitle().toString();
String myCategory = m.getCategory().toString();
int myCategoryN = m.getOrderNum();
double myPrice = m.getPrice() * myQTY;
if (myQTY == 0)
Toast.makeText(activity, "Orders Must be 1 Atleast",
Toast.LENGTH_SHORT).show();
else {
myADapterDB.checkIfExcist(activity, "productName", myTitle,
myQTY, myPrice, m.getPrice(), myTitle, myCategory,
myCategoryN + "", myQTY);
myADapterDB.deleteZeroOrLessValues();
}
}
});
return convertView;
}
public void notifyDataSetChanged() {
// TODO Auto-generated method stub
super.notifyDataSetChanged();
}
}
take a look at Loaders. 看一下装载机。 I think this is the prefered way to load data from datebase into views. 我认为这是将数据从数据库加载到视图中的首选方法。 With Loaders you get callbacks if the data has changed and the view is updated for you. 使用装载程序,如果数据已更改并且视图已为您更新,则将获得回调。
http://developer.android.com/guide/components/loaders.html http://developer.android.com/guide/components/loaders.html
Here is a good tutorial I think: http://code.tutsplus.com/tutorials/android-fundamentals-properly-loading-data--mobile-5673 我认为这是一个很好的教程: http : //code.tutsplus.com/tutorials/android-fundamentals-properly-loading-data--mobile-5673
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.