nick
nick

Reputation: 61

How to check whether the ListView is empty or not using custom list adapter

I have used two adpaters in my app, one for offline mode which fetches the data from a local sqlite DB.The second adapter fetches data from server and displays the items in the listview which is common for both the adapters. In there is no data in the list I have used the empty view to show the list is empty. But even if there is data in the sqlite still it shows me the list empty. I would like to know is there any way to check whether the adapter is null or not. If it is not null it means that it should display the values in the list.

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;
    }
}

List:

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));
    }

Upvotes: 2

Views: 5655

Answers (6)

Syed Danish Haider
Syed Danish Haider

Reputation: 1384

 if(mAdapter.getItemCount()==0)
            {
                Toast.makeText(UpdatePorder.this, "list is filled", Toast.LENGTH_SHORT).show();
            }
            else
            {
               Toast.makeText(UpdatePorder.this, "List is empty", Toast.LENGTH_SHORT).show();
            }

Upvotes: 1

krunal patel
krunal patel

Reputation: 25

The following code worked for me:

list_view.setAdapter(adapter);
if (adapter.getCount() > 0) {

} else {
  Toast.makeText(getApplicationContext(), "NO Data Available..", Toast.LENGTH_SHORT).show();
}

Upvotes: 1

abdul khan
abdul khan

Reputation: 863

if (adapter!=null) {
    if (adapter.getCount() > 0) {
        // listView not empty
    } else {
        // listView  empty
    }
}

Upvotes: 3

Monish Kamble
Monish Kamble

Reputation: 1488

Just set the emptyView before setting the adapter on listview. No need to check whether the adapter is empty, listview will take care of it.

You code should look like this :

TextView empty = (TextView) findViewById(android.R.id.empty);
listView.setEmptyView(empty);
listView.setAdapter(adapter);

Upvotes: 1

Pramod Waghmare
Pramod Waghmare

Reputation: 1283

Try this, may help you

    ImageListAdapter imageListAdapter= new ImageListAdapter();
    listviewObject.setAdapter(imageListAdapter);

    if (imageListAdapter!= null)
        if (imageListAdapter.getCount() > 0){
            // implement your work
        }else {
            // do whatever you want on empty list adapter 
        }

Upvotes: 0

Jagjit Singh
Jagjit Singh

Reputation: 1969

Use getcount() to know the number of items

if(adapter.getCount()>0)
{
//some rows of listview
}
else
{
//listview is empty
}

Hope it helps thanks

Upvotes: 1

Related Questions