Ich habe zwei Adpater in meiner App verwendet, einen für Offline-Modus, der die Daten von einem lokalen SQLite DB holt. Der zweite Adapter holt Daten vom Server und zeigt die Elemente in der Listenansicht, die beiden Adaptern gemeinsam ist. Es gibt keine Daten in der Liste Ich habe die leere Ansicht verwendet, um anzuzeigen, dass die Liste leer ist. Aber selbst wenn es Daten in der SQLite gibt, zeigt es mir die Liste leer. Ich würde gerne wissen, gibt es eine Möglichkeit zu überprüfen, ob der Adapter null ist oder nicht. Wenn es nicht null ist, bedeutet dies, dass es die Werte in der Liste anzeigen sollte.Wie überprüft man, ob die ListView leer ist oder nicht benutzerdefinierten Listenadapter verwendet
Offline-Adapter:
public class ImageListAdapter extends BaseAdapter {
Context context;
ArrayList<ImagelistItems> imageList;
public ImageListAdapter(Context context, ArrayList<ImagelistItems> list) {
this.context = context;
imageList = list;
}
@Override
public int getCount() {
return imageList.size();
}
@Override
public Object getItem(int position) {
return imageList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
ImagelistItems imagelistItems = imageList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.imagelist_items, null);
}
byte[] outImage = imagelistItems.getImage();
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
final Bitmap theImage = BitmapFactory.decodeStream(imageStream);
ImageView img = (ImageView) convertView.findViewById(R.id.imgview);
img.setImageBitmap(theImage);
final TextView merchname = (TextView) convertView.findViewById(R.id.lsmerchantname);
merchname.setText(imagelistItems.getMerchantname());
final TextView paid = (TextView) convertView.findViewById(R.id.lspaiddate);
paid.setText(imagelistItems.getPaidon());
TextView status = (TextView) convertView.findViewById(R.id.lsstatus);
status.setText(imagelistItems.getStatus());
final TextView amt = (TextView) convertView.findViewById(R.id.lsamount);
amt.setText(imagelistItems.getAmount());
ImageButton oofbutton = (ImageButton) convertView.findViewById(R.id.btnofflinebutton);
oofbutton.setVisibility(View.INVISIBLE);
final TextView category = (TextView) convertView.findViewById(R.id.lscategory);
category.setText(imagelistItems.getCategory());
final TextView paymode = (TextView) convertView.findViewById(R.id.lspaidwith);
paymode.setText(imagelistItems.getPaymmode());
final TextView comment = (TextView) convertView.findViewById(R.id.lscomment);
comment.setText(imagelistItems.getComment());
final TextView moneydet = (TextView) convertView.findViewById(R.id.moneydetails);
final String moen;
CurrenctSession currenctSession = new CurrenctSession(context);
if (currenctSession.isLoggedIn()) {
HashMap<String, String> cur = currenctSession.getUserDetails();
moen = cur.get(currenctSession.KEY_CURRENCY);
moneydet.setText(moen);
} else {
moneydet.setVisibility(View.INVISIBLE);
}
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String am = amt.getText().toString();
if (am.contains("km") | am.contains("m")) {
Intent i = new Intent(context, MapsDetailPage.class);
i.putExtra("distance", amt.getText().toString());
i.putExtra("origin", merchname.getText().toString());
i.putExtra("dest", paymode.getText().toString());
i.putExtra("catg", category.getText().toString());
i.putExtra("pdate", paid.getText().toString());
i.putExtra("comm", comment.getText().toString());
context.startActivity(i);
} else {
Intent i = new Intent(context, DetailsPage.class);
i.putExtra("bitmap", theImage);
i.putExtra("amount", amt.getText().toString());
i.putExtra("mername", merchname.getText().toString());
i.putExtra("paidon", paid.getText().toString());
i.putExtra("catg", category.getText().toString());
i.putExtra("paym", paymode.getText().toString());
i.putExtra("comm", comment.getText().toString());
context.startActivity(i);
}
}
});
return convertView;
}
}
Online Adapter:
public class CustomlistJsonadapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<ReceiptMov> movieItems;
public CustomlistJsonadapter(Activity activity, List<ReceiptMov> 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.onlineimagelist_items, null);
final TextView merchantname = (TextView) convertView.findViewById(R.id.olsmerchantname);
final TextView paiddate = (TextView) convertView.findViewById(R.id.olspaiddate);
final TextView amounts = (TextView) convertView.findViewById(R.id.olsamount);
final TextView paidwith = (TextView) convertView.findViewById(R.id.olspaidwith);
final TextView categ = (TextView) convertView.findViewById(R.id.olscategory);
final TextView comment = (TextView) convertView.findViewById(R.id.olscomment);
final TextView stast = (TextView) convertView.findViewById(R.id.olsstatus);
ImageView imgview = (ImageView) convertView.findViewById(R.id.oimgview);
ReceiptMov m = movieItems.get(position);
// Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
byte[] s = m.getMimage();
ByteArrayInputStream imageStream = new ByteArrayInputStream(s);
final Bitmap theImage = BitmapFactory.decodeStream(imageStream);
// Bitmap bm = BitmapFactory.decodeByteArray(s, 0 ,s.length);
imgview.setImageBitmap(theImage);
merchantname.setText(m.getMerchantname());
paiddate.setText("Paid on:" + m.getMerdate());
amounts.setText(m.getMeramount());
paidwith.setText(m.getMerpaid());
categ.setText(m.getMercategory());
comment.setText(m.getMecomment());
stast.setText("Status: " + "Uploaded");
final TextView moneydet = (TextView) convertView.findViewById(R.id.omoneydetails);
final String moen;
CurrenctSession currenctSession = new CurrenctSession(activity);
if (currenctSession.isLoggedIn()) {
HashMap<String, String> cur = currenctSession.getUserDetails();
moen = cur.get(currenctSession.KEY_CURRENCY);
moneydet.setText(moen);
} else {
moneydet.setVisibility(View.INVISIBLE);
}
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String am = amounts.getText().toString();
if (am.contains("km") | am.contains("m")) {
Intent i = new Intent(activity, MapsDetailPage.class);
i.putExtra("distance", amounts.getText().toString());
i.putExtra("origin", merchantname.getText().toString());
i.putExtra("dest", paidwith.getText().toString());
i.putExtra("catg", categ.getText().toString());
i.putExtra("pdate", paiddate.getText().toString());
i.putExtra("comm", comment.getText().toString());
activity.startActivity(i);
} else {
Intent i = new Intent(activity, DetailsPage.class);
i.putExtra("bitmap", theImage);
i.putExtra("amount", amounts.getText().toString());
i.putExtra("mername", merchantname.getText().toString());
i.putExtra("paidon", paiddate.getText().toString());
i.putExtra("catg", categ.getText().toString());
i.putExtra("paym", paidwith.getText().toString());
i.putExtra("comm", comment.getText().toString());
activity.startActivity(i);
}
}
});
return convertView;
}
}
Liste:
if(imageListAdapter!=null){
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
// Spinner Drop down elements
// List<String> lables = db.getAllLabels();
ArrayList<ImagelistItems> list = new ArrayList<ImagelistItems>();
list = db.getAllLabels();
imageListAdapter = new ImageListAdapter(
ListMode.this, list);
if (loginSession.isLoggedIn()) {
loginSession.checkLogin();
listView.setAdapter(imageListAdapter);
// listView.setEmptyView(findViewById(R.id.empty));
Toast.makeText(getApplicationContext(), db.getSyncStatus(), Toast.LENGTH_LONG).show();
} else {
Intent i = new Intent(ListMode.this, LoginPAge.class);
startActivity(i);
ListMode.this.finish();
}
}else{
listView.setEmptyView(findViewById(R.id.empty));
}
hinzufügen imageListAdapter.notifydatasetChanged() nach dieser Zeile listView.setAdapter (imageListAdapter); –
Ich habe hinzugefügt, aber immer noch zeigt es mir die Liste leere Ansicht – nick
Sind Sie sicher, dass Ihr ImageListAdapter nicht null ist? –