刷新不在Listview中工作;已经在类似主题上查找了许多其他回复,但无法解决问题。
如何让它自动刷新?我尝试了.notifyDataSetChanged()
和.invalidate()
但没有成功。
这是代码: XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:contentDescription="@string/image"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<CheckBox
android:id="@+id/chkImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chkImage" />
<TextView
android:id="@+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/icon"
android:paddingBottom="2dp"
android:textColor="#CC0033"
android:textSize="28dp" />
<TextView
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/quantity"
android:paddingBottom="2dp"
android:textColor="#CC0033"
android:textSize="16dp" />
</RelativeLayout>
这是主要活动
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
currentOrderNumber = getCurrentOrderNumber();
od_listview = (ListView) findViewById(R.id.list);
txtViewOrderCount=(TextView)findViewById(R.id.orderCount);
txtViewCatalogCount=(TextView)findViewById(R.id.catalogCount);
buildUpCurrentOrderList();
addListenerOnButton();
}
public void addListenerOnButton() {
button4 = (ImageButton) findViewById(R.id.button4);
button4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
buildUpCurrentOrderList();
}
});
private void buildUpCurrentOrderList () {
ListView listview = (ListView) findViewById(R.id.list);
S_DAO db = new S_DAO(this) ;
db.open();
db.set_orderdetailChangedToN(this);
list.clear();
List<OrderDetail> orderdetails = new ArrayList<OrderDetail>();
orderdetails = db.getOrderDetailList();
orderCount=orderdetails.size();
final ArrayList<OrderDetail> orderdetailsReverse = new ArrayList<OrderDetail>(orderdetails);
Collections.reverse(orderdetailsReverse);
final CustomOrderViewAdapter adapter = new CustomOrderViewAdapter(this,
R.layout.scans_list, orderdetailsReverse);
listview.setAdapter(adapter);
txtViewOrderCount.setText(Integer.toString(orderCount));
txtViewOrderCount.bringToFront();
}
以下是适配器代码:
public class CustomOrderViewAdapter extends ArrayAdapter<OrderDetail> {
Context context;
public CustomOrderViewAdapter(Context context, int resourceId,
List<OrderDetail> items) {
super(context, resourceId, items);
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtBarcode;
TextView txtDesc;
TextView txtBrand;
TextView txtMeasure;
TextView txtWeightVol;
TextView txtPackItems;
TextView txtQuantity;
CheckBox chkImage;
ImageView icon;
ImageButton buttonMore;
}
public void updateOrderdetailList() {
this.notifyDataSetChanged();
}
public View getView(int position, View convertView, final ViewGroup parent) {
ViewHolder viewholder = null;
final OrderDetail orderdetail = getItem(position);
final int pos = position;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.scans_list, null);
viewholder = new ViewHolder();
viewholder.txtBarcode = (TextView) convertView.findViewById(R.id.barcode);
viewholder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
viewholder.txtBrand = (TextView) convertView.findViewById(R.id.brand);
viewholder.imageView = (ImageView) convertView.findViewById(R.id.icon);
viewholder.txtMeasure = (TextView) convertView.findViewById(R.id.measure);
viewholder.txtWeightVol = (TextView) convertView.findViewById(R.id.weightvol);
viewholder.txtPackItems = (TextView) convertView.findViewById(R.id.packitems);
viewholder.txtQuantity = (TextView) convertView.findViewById(R.id.quantity);
viewholder.chkImage = (CheckBox) convertView.findViewById(R.id.chkImage);
viewholder.icon = (ImageView) convertView.findViewById(R.id.icon);
viewholder.buttonMore = (ImageButton) convertView.findViewById(R.id.buttonMore);
convertView.setTag(viewholder);
} else
viewholder = (ViewHolder) convertView.getTag();
viewholder.txtBarcode.setText(orderdetail.get_od_p_Barcode());
S_DAO db = new S_DAO(context) ;
db.open();
final String tmpBarcode=orderdetail.get_od_p_Barcode();
final String tmpDesc = db.readProduct(context, tmpBarcode).get_p_DescLong();
final int tmpPackItems =db.readProduct(context, tmpBarcode).get_p_PackItems();
final int tmpWeightVol = db.readProduct(context, tmpBarcode).get_p_WeightVol();
final int tmpQuantity=orderdetail.get_od_Quantity();
viewholder.txtBrand.setText(db.readProduct(context, tmpBarcode).get_p_Brand());
viewholder.txtDesc.setText(tmpDesc);
viewholder.txtMeasure.setText(db.readProduct(context, tmpBarcode).get_p_Measure());
viewholder.txtWeightVol.setText(Integer.toString(tmpWeightVol));
viewholder.txtPackItems.setText(Integer.toString(tmpPackItems));
viewholder.txtQuantity.setText(Integer.toString(tmpQuantity));
int imageResource = context.getResources().getIdentifier(db.readProduct(context, tmpBarcode).get_p_ImageName(), "drawable", context.getPackageName());
db.close();
viewholder.imageView.setImageResource(imageResource);
viewholder.buttonMore.setOnClickListener(new AdapterView.OnClickListener() {
public void onClick(View v) {
orderdetailDialog(orderdetail, tmpPackItems, tmpDesc, tmpQuantity, pos, v, parent);
}
});
viewholder.icon.setOnClickListener(new AdapterView.OnClickListener() {
public void onClick(View v) {
setCyan(orderdetail, parent, pos, v);
}
});
viewholder.chkImage.setOnClickListener(new AdapterView.OnClickListener() {
public void onClick(View v) {
setCyan(orderdetail, parent, pos, v);
}
});
return convertView;
}
在同一个适配器类中:打开对话框的类
public void orderdetailDialog (final OrderDetail orderdetail, int itemsPerPack, String p_desc, int p_quantity, int pos, View v, ViewGroup parent) {
LayoutInflater factory = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
final View textEntryView = factory.inflate(R.layout.scans_more, null);
//text_entry is an Layout XML file containing two text field to display in alert dialog
final EditText inputQuantity = (EditText ) textEntryView.findViewById(R.id.quantity);
final TextView inputItemsPerPack = (TextView ) textEntryView.findViewById(R.id.itemsPerPack);
inputQuantity.setText(Integer.toString(p_quantity), TextView.BufferType.EDITABLE);
inputItemsPerPack.setText(Integer.toString(itemsPerPack), TextView.BufferType.EDITABLE);
final AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setIcon(R.drawable.more2)
.setTitle(p_desc)
.setView(textEntryView)
.setPositiveButton("Save",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
updateOrderDetail(orderdetail, Integer.parseInt(inputQuantity.getText().toString()));
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
});
alert.show();
}
然后是更新数据库的类:
private void updateOrderDetail (OrderDetail orderdetail, int quantity) {
S_DAO db = new S_DAO(context) ;
db.open();
db.updateOrderDetailWithQuantity(context, orderdetail, quantity);
db.close();
}
答案 0 :(得分:1)
您可以尝试拨打notifyDataSetChanged()
中的UIThread
吗?像那样
runOnUiThread(new Runnable() {
@Override
public void run() {
notifyDataSetChanged();
}
}
并在数据库更新的位置调用它。
private void updateOrderDetail (OrderDetail orderdetail, int quantity) {
S_DAO db = new S_DAO(context) ;
db.open();
List<OrderDetail> orderdetails = new ArrayList<OrderDetail>();
db.updateOrderDetailWithQuantity(context, orderdetail, quantity);
orderdetails = db.getOrderDetailList();
final ArrayList<OrderDetail> orderdetailsReverse = new ArrayList<OrderDetail>(orderdetails);
Collections.reverse(orderdetailsReverse);
clear();
addAll(orderdetailsReverse);
runOnUiThread(new Runnable() {
@Override
public void run() {
notifyDataSetChanged();
}
}
db.close();
}
答案 1 :(得分:0)
你在任何地方调用invalidate()或postInvalidate()方法吗?您需要在刷新表时调用其中一个。