名称在列表视图项目中出现两次

Names appearing twice in list view items

我正在尝试设置一个 2 行列表视图,但每个字符串似乎都在项目中复制自身而不是显示一次。我怎样才能防止这种情况发生?即我需要数据显示如下:对于项目 1 - America,America Description 而不是 America,America;对于项目 2 - Europe, Europe Description 而不是 Europe, Europe。请参阅屏幕截图以了解不良结果的证据。

ListData.java

public class ListData {

    public static final int[][] items = {
            {R.string.america,R.string.america_description},
            {R.string.europe, R.string.europe_description}
    };

}

ListViewAdapter

public class ItemListAdapter extends BaseAdapter implements Filterable {

    private List<String> mData;
    private List<String> mFilteredData;
    private LayoutInflater mInflater;
    private ItemFilter mFilter;

    public ItemListAdapter (List<String> data, Context context) {
        mData = data;
        mFilteredData = data;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public String getItem(int position) {
        return mFilteredData.get(position);
    }

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

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

        String strItem = mFilteredData.get(position);
        ViewHolder holder;
        if (convertView == null) {
           convertView = mInflater.inflate(R.layout.item_row, parent, false);

            holder = new ViewHolder();
            holder.mTitle = (TextView) convertView.findViewById(R.id.item_title);
            holder.mDescription = (TextView) convertView.findViewById(R.id.item_description);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.mTitle.setText(strItem);
        holder.mDescription.setText(strItem);

        return convertView;
    }

    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new ItemFilter();
        }
        return mFilter;
    }

    /**
     * View holder
     */
    static class ViewHolder {
        private TextView mTitle;
        private TextView mDescription;
    } 
}

item_row.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingRight="?android:attr/scrollbarSize"
    android:baselineAligned="false">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <TextView android:id="@+id/item_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:ellipsize="marquee"
            android:textColor="?android:attr/textColorPrimary" />

        <TextView android:id="@+id/item_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/item_title"
            android:layout_alignLeft="@id/item_title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary" />

    </RelativeLayout>

</LinearLayout>

ItemListadapter.java

public class ItemListAdapter extends BaseAdapter implements Filterable {

    private List<String> mData;
    private List<String> mFilteredData;
    private LayoutInflater mInflater;
    private ItemFilter mFilter;

    public ItemListAdapter (List<String> data, Context context) {
        mData = data;
        mFilteredData = data;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public String getItem(int position) {
        return mFilteredData.get(position);
    }

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

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

        String strItem = mFilteredData.get(position);
        ViewHolder holder;
        if (convertView == null) {
           convertView = mInflater.inflate(R.layout.item_row, parent, false);
            holder = new ViewHolder();

            holder.mTitle = (TextView) convertView.findViewById(R.id.item_title);
            holder.mDescription = (TextView) convertView.findViewById(R.id.item_description);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.mTitle.setText(strItem);
        holder.mDescription.setText(strItem);

        return convertView;
    }

    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new ItemFilter();
        }
        return mFilter;
    }

    /**
     * View holder
     */
    static class ViewHolder {
        private TextView mTitle;
        private TextView mDescription;
    }

    /**
     * Filter for filtering list items
     */
    private class ItemFilter extends Filter {

        /**
         * Invoked on a background thread.  This is where all the filter logic should go
         * @param constraint the constraint to filter on
         * @return the resulting list after applying the constraint
         */
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            FilterResults results = new FilterResults();

            if (TextUtils.isEmpty(constraint)) {
                results.count = mData.size();
                results.values = mData;
            } else {
                //Create a new list to filter on
                List<String> resultList = new ArrayList<>();
                for (String str : mData) {
                    if (str.toLowerCase().contains(constraint.toString().toLowerCase())) {
                        resultList.add(str);
                    }
                }
                results.count = resultList.size();
                results.values = resultList;
            }
            return results;
        }

        /**
         * Runs on ui thread
         * @param constraint the constraint used for the result
         * @param results the results to display
         */
        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {

            if (results.count == 0) {
                notifyDataSetInvalidated();
            } else {
                mFilteredData = (ArrayList<String>)results.values;
                notifyDataSetChanged();
            }
        }
    }
}

该名称在 ListView 中显示了两次,因为您同时传递了两个 TextView 相同的项目:

String strItem = mFilteredData.get(position);

为避免这种情况并使用两个单独的字段,您需要将具有两个字段的模型传递给您的适配器:

public class CustomListItem {
    private String title;
    private String description;

    //get method

    public CustomListItem(String title, String description){
        this.title = title;
        this.description = description;
    }
}

然后您必须添加它们以在您的 activity/fragment 中创建一个使用适配器的 new List<CustomListItem> 并将此列表传递给适配器:

public class ItemListAdapter extends ArrayAdapter<CustomListItem>{
    private Context context;
    private List<CustomListItem> data;

    public ItemListAdapter(Context context, List<CustomListItem> data){
        this.context = context;
        this.data = data;
    }
}