如果在android中列表中没有项目,如何在listview中找到NO项目

时间:2014-09-10 04:33:12

标签: android

这是list-view的代码

listView = (ListView) layout.findViewById(R.id.listView1);
        Notifcationadapter adapter = new Notifcationadapter(this, notifies);
        listView.setAdapter(adapter);
        TextView emptyText = (TextView)findViewById(android.R.id.empty);
        listView.setEmptyView(emptyText);
        listView.setOnItemClickListener(this);

这是我的list.xmlfile:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup"
    android:layout_width="fill_parent"
    android:layout_height="350dip" >

    <RelativeLayout
        android:id="@+id/rl_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="#00000000" >

        <ImageView
            android:id="@+id/image_tringle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="50dp"
            android:src="@drawable/notficationarrow_icon" />
    </RelativeLayout>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/textView1"
        android:background="@anim/notifcationitembroder" >
    </ListView>



    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/rl_top"
        android:background="@drawable/bgnotifcationlayoutback"
        android:gravity="center"
        android:text="Notification"
        android:textColor="#666666"
        android:textSize="25dip"
        android:textStyle="bold" />

</RelativeLayout>

这是我的列表视图adepter类:

public class Notifcationadapter extends BaseAdapter {
    private static ArrayList<Notify> values;
    Context context;
    String senderid;
    String imagepath;

    // List<RowItem> rowItems;
    ViewHolder holder = null;

    LinearLayout linear2;

    DataBaseManager dbManager = new DataBaseManager(context);
    ArrayList<Notify> notifies;

    public Notifcationadapter(Context context, ArrayList<Notify> notifies) {
        super();
        this.context = context;
        this.notifies = notifies;

    }

    /* private view holder class */
    private class ViewHolder {

        TextView txtTitle;
        TextView txtDesc;
        ImageView yesimage;

        ImageView noimage;
        TextView revresetime;

        TextView rejected;
        ImageView notificationuserimage;
        LinearLayout linear2;

    }

    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.notifcationlistitem,
                    parent, false);
            holder = new ViewHolder();
            // holder.linear2 = (LinearLayout) convertView
            // .findViewById(R.id.linear2);
            holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);

            holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
            holder.yesimage = (ImageView) convertView
                    .findViewById(R.id.imageView1);
            holder.noimage = (ImageView) convertView
                    .findViewById(R.id.imageView2);
            holder.revresetime = (TextView) convertView
                    .findViewById(R.id.revresetime);
            holder.rejected = (TextView) convertView
                    .findViewById(R.id.rejectedrequest);
            holder.notificationuserimage = (ImageView) convertView
                    .findViewById(R.id.notificationuserimage);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        // holder.txtTitle.setText("Title");
        String statustype = notifies.get(position).getNotificationType();

        if (statustype.equals("1")) {

            holder.txtDesc.setText(notifies.get(position)
                    .getNotificationDescrpiton());

            holder.revresetime.setText(Functions
                    .setLastSeenNotifcationTime(notifies.get(position)
                            .getNotificationDatetime()));
            holder.txtTitle.setText("Request For travelling");

            senderid = notifies.get(position).getNotificationSenderID();
            imagepath = "http://api.lociiapp.com/TransientStorage/"

            + senderid + ".jpg";
            AQuery aq = new AQuery(context);

            aq.id(holder.notificationuserimage).image(imagepath);

            holder.yesimage.setVisibility(View.VISIBLE);
            holder.noimage.setVisibility(View.VISIBLE);

            holder.yesimage.setTag(convertView);
            holder.yesimage.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    View parentView = (View) arg0.getTag();
                    View view = parentView.findViewById(R.id.imageView1);
                    view.setVisibility(View.GONE);
                    view = parentView.findViewById(R.id.imageView2);
                    view.setVisibility(View.GONE);
                    // view = parentView.findViewById(R.id.linear2);
                    view.setVisibility(View.INVISIBLE);
                    view = parentView.findViewById(R.id.runnimage);
                    view.setVisibility(View.INVISIBLE);
                    Toast.makeText(context, "Yes", 10000).show();

                }
            });
            holder.noimage.setTag(convertView);
            holder.noimage.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    View parentView = (View) v.getTag();
                    View view = parentView.findViewById(R.id.imageView1);
                    view.setVisibility(View.GONE);
                    view = parentView.findViewById(R.id.imageView2);
                    view.setVisibility(View.GONE);
                    view = parentView.findViewById(R.id.rejectedrequest);
                    view.setVisibility(View.VISIBLE);
                    Toast.makeText(context, "NO", 10000).show();
                    // holder.linear2.setVisibility(View.INVISIBLE);
                    holder.yesimage.setVisibility(View.GONE);
                    holder.noimage.setVisibility(View.GONE);
                    holder.rejected.setVisibility(View.VISIBLE);
                }
            });

        }

        else {

            holder.txtDesc.setText(notifies.get(position)
                    .getNotificationDescrpiton());
            holder.txtTitle.setText("Post");
            holder.revresetime.setText(Functions
                    .setLastSeenNotifcationTime(notifies.get(position)
                            .getNotificationDatetime()));

            senderid = notifies.get(position).getNotificationSenderID();
            imagepath = "http://api.lociiapp.com/TransientStorage/"

            + senderid + ".jpg";
            AQuery aq = new AQuery(context);
            aq.id(holder.notificationuserimage).image(imagepath);

            holder.yesimage.setVisibility(View.GONE);
            holder.noimage.setVisibility(View.GONE);
        }

        String notisread = notifies.get(position).getNotificationStatus();
        if (notisread.equals("false")) {

            convertView.setBackgroundColor(Color.parseColor("#ffffff"));
        } else {
            convertView.setBackgroundColor(Color.parseColor("#f5f6f1"));
        }

        return convertView;
    }

    @Override
    public int getCount() {
        return notifies.size();
    }

    @Override
    public Object getItem(int position) {
        return notifies.get(position).getNotificationType();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

}

我想在列表中没有Listview项目时显示No item Found,或者我们必须在listview中显示空文本但我已经尝试但是当添加项目时Noitem发现文本正在显示,而我必须隐藏它。请帮忙

4 个答案:

答案 0 :(得分:0)

在收集通知arraylist的值和设置适配器后,在onCreate方法中放入一个条件。

if(notifies.size<=0){
   emptyText.setVisibility(View.VISIBLE);
   listView.setAdapter(null);
   listView.setVisibility(View.GONE);
}

答案 1 :(得分:0)

你可以使用setEmptyView()来检查ListView的空虚。

Here您可以找到setEmptyView()的好例子。

答案 2 :(得分:0)

if (notifies ==0){
// write your code  or textview visible and listview gone 

listView .setEmptyView(findViewById(R.id. empty));

Toast.makeText(getApplicationContext(),"No items found",3000).show();


}

答案 3 :(得分:0)

这样做

if(notifyList.size()==0){
    //tview.setText("Empty");
    urLst.setEmptyView(tview);
    }
    else{
    urAdapter=new Adapter();//create here
    urList.setAdapter(urAdapter);
    usAdapter.notifyDataSetChanged();
    }

希望这会有所帮助。我这样做了

更新

如果您使用AsyncTask获取listItems,则在onPostExecute()中执行此操作。 同时在那里创建Listview引用。