简体   繁体   English

如何使用自定义列表适配器检查ListView是否为空

[英]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. 我在应用程序中使用了两个adpater,一个是脱机模式,用于从本地sqlite DB中获取数据。第二个适配器是从服务器中获取数据,并在两个适配器都通用的listview中显示项目。 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. 但是即使sqlite中有数据,它仍然显示列表为空。 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. 如果不为null,则表示应在列表中显示值。

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));
    }
if (adapter!=null) {
    if (adapter.getCount() > 0) {
        // listView not empty
    } else {
        // listView  empty
    }
}

Use getcount() to know the number of items 使用getcount()知道项目数

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

Hope it helps thanks 希望对你有帮助

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 
        }

Just set the emptyView before setting the adapter on listview. 在listview上设置adapter之前,只需设置emptyView No need to check whether the adapter is empty, listview will take care of it. 无需检查适配器是否为空,listview会处理它。

You code should look like this : 您的代码应如下所示:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有自定义光标适配器的ListView为空 - ListView with Custom Cursor Adapter is Empty 如何按字母顺序对 listView 的文件进行排序并集成列表适配器?我正在使用自定义适配器 - How do i sort alphabetically the files of my listView and integrate a list adapter?I'm using a custom adapter 无法获取自定义列表适配器以使用asynctask填充listview - Can't get custom list adapter to populate listview using asynctask 尝试使用 notifyDataSetChanged 更新我的 listView 自定义列表适配器但不工作 - Trying to update my custom list adapter of listView using notifyDataSetChanged but not working 空片段列表与自定义适配器 - Empty Fragment list with custom adapter 如何在休眠响应中检查列表中的列表是否为空 - How to check whether list inside list is empty in hibernate response 使用pasrequery的ListView的自定义适配器 - Custom adapter for listview using pasrequery 在Android中使用自定义适配器的Listview - Listview using custom adapter in android 如何使用简单的适配器和列表视图创建自己的自定义行布局 - How to create own custom row layout using simple adapter and listview 如何在Android中使用简单适配器将图像添加到自定义列表视图 - How to add images to custom listview using Simple Adapter in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM