Android列表视图显示项目,但文本为空

时间:2017-02-09 06:37:54

标签: java android xml listview android-adapter

我遇到一个非常奇怪的问题,我无法查看任何文本/数据,但它清楚地显示了列表中的项目数。

我已经确认dataList有数据,并且我能够获得调试后设置的完全相同的字符串。

Problem

父片段:

ListView InboxListView = (ListView) view.findViewById(R.id.listViewInbox);
InboxAdapter adapter = new InboxAdapter(mActivity, R.layout.activity_inbox_item, dataList, NotificationInbox.this);

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/SwipeContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"

        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <EditText
            android:layout_width="match_parent"
            android:padding="15dp"
            android:id="@+id/searchBarInbox"
            android:hint="Search"
            android:layout_height="wrap_content" />


        <ListView
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:layout_marginTop="49dp"
            android:id="@+id/listViewInbox" />

    </RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>

适配器: http://pastebin.com/mSUrdLcW &lt;&lt; CLICK

public class InboxAdapter extends ArrayAdapter<InboxBO> {

    NotificationInbox fragment;
    Context context;
    List<InboxBO> inboxForSharedPref;
    List<InboxBO> inboxList;
    int layoutResID;
    SharedPreferences prefs;
    private ArrayList<InboxBO> inboxarraylist;
    private SparseBooleanArray mSelectedItemIds;

    public InboxAdapter(Context context, int layoutResourceID, List<InboxBO> objs, NotificationInbox fragment) {
        super(context, layoutResourceID, objs);
        mSelectedItemIds = new SparseBooleanArray();
        this.context = context;
        this.inboxList = objs;
        this.layoutResID = layoutResourceID;
        this.inboxarraylist = new ArrayList<InboxBO>();
        this.inboxarraylist.addAll(inboxList);
        this.fragment = fragment;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        inboxHolder inboxholder;
        View view = convertView;
        if (view == null) {
            view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(layoutResID, parent, false);
            //LayoutInflater inflater = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
            //LayoutInflater inflater = LayoutInflater.from(getContext());
            //inflater.inflate(R.layout.activity_inbox_item, null);
            inboxholder = new inboxHolder();
            //view = inflater.inflate(layoutResID, parent, false);
            prefs = context.getSharedPreferences(
                    Constants.PREF_NAME, 0);

            inboxholder.swipeLayout = (SwipeLayout)view.findViewById(R.id.swipe_layout);
            inboxholder.senderNameText = (TextView) view.findViewById(R.id.senderNameText);
            inboxholder.pushDateText = (TextView) view.findViewById(R.id.pushDateText);
            inboxholder.pushTimeText = (TextView) view.findViewById(R.id.pushTimeText);
            inboxholder.messageText = (TextView) view.findViewById(R.id.messageText);
            inboxholder.delete = (TextView)view.findViewById(R.id.delete);
            inboxholder.itemLayout = (RelativeLayout) view.findViewById(R.id.relativeViewInbox);
            inboxholder.swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
            inboxholder.delete.setOnClickListener(onDeleteListener(position, inboxholder));
            view.setTag(inboxholder);
        }
        else {
            inboxholder = (inboxHolder) view.getTag();
        }

        InboxBO mItem =     inboxList.get(position);
        if(mItem!=null){
            inboxholder.senderNameText.setText((String)mItem.getTitle());
            inboxholder.pushDateText.setText(new Date().toString());
            inboxholder.pushTimeText.setText(new Date().toString());
            inboxholder.messageText.setText((String)mItem.getMessage());
        }
        return view;
    }

    // For swipe action
    private View.OnClickListener onDeleteListener(final int position, final inboxHolder holder) {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alert = new AlertDialog.Builder(
                        context);
                alert.setTitle("Delete Message");
                alert.setMessage("Are you sure you wish to delete this message?");
                alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        inboxList.remove(position);
                        Gson gson = new Gson();
                        inboxForSharedPref = fragment.getStoredMessages();
                        inboxForSharedPref = inboxList;
                        String jsonSavePref = gson.toJson(inboxForSharedPref);
                        fragment.commitToStoredList(jsonSavePref);
                        Toast.makeText(context, "Message Deleted!",
                                Toast.LENGTH_SHORT).show();
                        holder.swipeLayout.close();
                        fragment.retrieveMessage();
                        dialog.dismiss();
                    }
                });

                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();
                    }
                });

                alert.show();
            }
        };
    }

    // Algorithm to filter out listview based on text changed in listview searchbox
    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        inboxList.clear();
        if (charText.length() == 0) {
            inboxList.addAll(inboxarraylist);
        }
        else
        {
            for (InboxBO ilist : inboxarraylist)
            {
                /*if (ilist.getDate().toLowerCase(Locale.getDefault()).contains(charText) || ilist.getMessage().toLowerCase(Locale.getDefault()).contains(charText) || ilist.getSendername().toLowerCase(Locale.getDefault()).contains(charText))
                {
                    inboxList.add(ilist);
                }*/
                if(ilist.getTitle().toLowerCase(Locale.getDefault()).contains(charText) || ilist.getMessage().toLowerCase(Locale.getDefault()).contains(charText) ){
                    inboxList.add(ilist);
                }
            }
        }
        notifyDataSetChanged();
    }


    public static class inboxHolder {
        TextView senderNameText, pushDateText, pushTimeText, messageText, delete, title;
        RelativeLayout itemLayout;
        private SwipeLayout swipeLayout;
    }

    // Methods below are for multi-deletion
    public void  toggleSelection(int position) {
        selectView(position, !mSelectedItemIds.get(position));
    }
    // Remove selection after unchecked
    public void  remove(InboxBO object) {
        inboxList.remove(object);
        Gson gson = new Gson();
        inboxForSharedPref = fragment.getStoredMessages();
        inboxForSharedPref = inboxList;
        String jsonSavePref = gson.toJson(inboxForSharedPref);
        fragment.commitToStoredList(jsonSavePref);
        notifyDataSetChanged();
    }

    // Item checked on selection
    public void selectView(int position, boolean value) {
        if (value)
            mSelectedItemIds.put(position,  value);
        else
            mSelectedItemIds.delete(position);
        notifyDataSetChanged();
    }

    // Get number of selected item
    public int  getSelectedCount() {
        return mSelectedItemIds.size();
    }

    public  SparseBooleanArray getSelectedIds() {
        return mSelectedItemIds;
    }

    public void removeSelection() {
        mSelectedItemIds = new SparseBooleanArray();
        notifyDataSetChanged();
    }



}

适配器XML:

<?xml version="1.0" encoding="utf-8"?>
<com.daimajia.swipe.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/bottom_wrapper"
        android:layout_width="100dp"
        android:layout_gravity="start"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#ff0000">
            <TextView
                android:id="@+id/delete"
                android:text="Delete"
                android:textSize="18sp"
                android:textColor="@color/black"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>
    </LinearLayout>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:padding="10dp"
        android:background="?android:attr/activatedBackgroundIndicator"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:id="@+id/relativeViewInbox"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical|center_horizontal"
            android:orientation="horizontal"
            android:id="@+id/linearLayout">
            <TextView
                android:layout_width="match_parent"
                android:text="SenderName"
                android:textColor="@color/black"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:gravity="left"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:id="@+id/senderNameText"
                android:textStyle="bold" />
            <TextView
                android:layout_width="match_parent"
                android:text="PushDate"
                android:gravity="right"
                android:textColor="@color/black"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:id="@+id/pushDateText" />
        </LinearLayout>

        <TextView
            android:layout_width="320dp"
            android:layout_height="match_parent"
            android:layout_marginTop="10dp"
            android:text="Message..."
            android:id="@+id/messageText"
            android:textColor="@color/black"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:ellipsize="end"
            android:maxLines="1"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/linearLayout" />
        <TextView
            android:layout_width="match_parent"
            android:text="PushTime"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:gravity="right"
            android:layout_weight="1"
            android:textColor="@color/black"
            android:layout_height="match_parent"
            android:id="@+id/pushTimeText"
            android:layout_alignTop="@+id/messageText" />
    </RelativeLayout>
</com.daimajia.swipe.SwipeLayout>

编辑:添加了截图

EDIT2:澄清调用适配器的父片段。

EDIT3:我做了一个临时修复。显然,AdapterXML中的RelativeLayout和Linear Layout是我痛苦的原因。不过,我不确定为什么。我认为它是RelativeLayout和LinearLayout以某种方式不显示文本。删除这两个标签将以线性方式显示文本。

1 个答案:

答案 0 :(得分:0)

试试这个 适配器XML:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/bottom_wrapper"
        android:layout_width="100dp"
        android:layout_gravity="start"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#ff0000">
            <TextView
                android:id="@+id/delete"
                android:text="Delete"
                android:textSize="18sp"
                android:textColor="@color/black"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>
    </LinearLayout>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:padding="10dp"
        android:background="?android:attr/activatedBackgroundIndicator"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:id="@+id/relativeViewInbox"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical|center_horizontal"
            android:orientation="horizontal"
            android:id="@+id/linearLayout">
            <TextView
                android:layout_width="match_parent"
                android:text="SenderName"
                android:textColor="@color/black"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:gravity="left"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:id="@+id/senderNameText"
                android:textStyle="bold" />
            <TextView
                android:layout_width="match_parent"
                android:text="PushDate"
                android:gravity="right"
                android:textColor="@color/black"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:id="@+id/pushDateText" />
        </LinearLayout>

        <TextView
            android:layout_width="320dp"
            android:layout_height="match_parent"
            android:layout_marginTop="10dp"
            android:text="Message..."
            android:id="@+id/messageText"
            android:textColor="@color/black"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:ellipsize="end"
            android:maxLines="1"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/linearLayout" />
        <TextView
            android:layout_width="match_parent"
            android:text="PushTime"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:gravity="right"
            android:layout_weight="1"
            android:textColor="@color/black"
            android:layout_height="match_parent"
            android:id="@+id/pushTimeText"
            android:layout_alignTop="@+id/messageText" />
    </RelativeLayout>