自定义数组适配器不显示listview中的数据

时间:2017-02-02 15:14:18

标签: java android listview android-fragments android-arrayadapter

这是我的wordAdapter。当我打开Fragment时,它没有显示任何结果。请帮帮我,因为我是新手Android开发人员。 Fragment页面为空,运行时也没有显示任何错误。所以,请告诉我缺少什么,以及此代码中的问题。

public class wordAdapter extends ArrayAdapter<word> {
    private Context context;
    private List<word> wrd;
    private SharedPreference sharedPreference;

    public wordAdapter(Context context, List<word> wrd) {
        super(context, R.layout.item, wrd);
        this.context = context;
        this.wrd = wrd;
        sharedPreference = new SharedPreference();
    }

    private class ViewHolder {
        TextView productNameTxt;
        TextView productTypeTxt;
        ImageView favoriteImg;
    }

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

    @Override
    public word getItem(int position) {
        return wrd.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.item, null);
            holder = new ViewHolder();
            holder.productNameTxt = (TextView) convertView
                    .findViewById(R.id.carname);
            holder.productTypeTxt = (TextView) convertView
                    .findViewById(R.id.cartype);
            holder.favoriteImg = (ImageView) convertView
                    .findViewById(R.id.favu);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        word wrdn = (word) getItem(position);
        holder.productNameTxt.setText(wrdn.getName());
        holder.productTypeTxt.setText(wrdn.getType());

        /*If a product exists in shared preferences then set heart_red drawable
         * and set a tag*/
        if (checkFavoriteItem(wrdn)) {
            holder.favoriteImg.setImageResource(R.drawable.fav);
            holder.favoriteImg.setTag("red");
        } else {
            holder.favoriteImg.setImageResource(R.drawable.unfav);
            holder.favoriteImg.setTag("grey");
        }

        return convertView;
    }

    /*Checks whether a particular product exists in SharedPreferences*/
    private boolean checkFavoriteItem(word checkwrdn) {
        boolean check = false;
        List<word> favorites = sharedPreference.getFavorites(context);
        if (favorites != null) {
            for (word wrdn : favorites) {
                if (wrdn.equals(checkwrdn)) {
                    check = true;
                    break;
                }
            }
        }
        return check;
    }

    @Override
    public void add(word wrdn) {
        super.add(wrdn);
        wrd.add(wrdn);
        notifyDataSetChanged();
    }

    @Override
    public void remove(word wrdn) {
        super.remove(wrdn);
        wrd.remove(wrdn);
        notifyDataSetChanged();
    }
}

这是我的word文件。

public class word {
    private String name;
    private String type;

    public word(String name, String type) {
        super();
        this.name = name;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public String getType() {
        return type;
    }
}

这是我的Fragment文件及其数据。

public class Cars extends android.support.v4.app.Fragment implements
        AdapterView.OnItemClickListener,                             
    AdapterView.OnItemLongClickListener {

    public static final String ARG_PAGE = "ARG_PAGE";

    public static Favourites newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        Favourites fragment = new Favourites();
        fragment.setArguments(args);
        return fragment;
    }

    Activity activity;
    ListView productListView;
    List<word> wrds;
    wordAdapter wrdAdapter;

    SharedPreference sharedPreference;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = getActivity();
        sharedPreference = new SharedPreference();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.list, container,
                false);
        findViewsById(view);

        setProducts();

        wrdAdapter = new wordAdapter(activity, wrds);
        productListView.setAdapter(wrdAdapter);
        productListView.setOnItemClickListener(this);
        productListView.setOnItemLongClickListener(this);
        return view;
    }

    private void setProducts() {

        word product1 = new word("Lamborghini Huracan", "Sport");
        word product2 = new word("Lamborghini Aventador", "Sport");
        word product3 = new word("Jaguar XF", "Luxury Sedan");
        word product4 = new word("Audi A4", "Luxury Sedan");
        word product5 = new word("Ferrari 488", "Sport");
        word product6 = new word("BMW i8", "Hybrid Sport");
        word product7 = new word("Audi TT", "Sport");

        wrds = new ArrayList<word>();
        wrds.add(product1);
        wrds.add(product2);
        wrds.add(product3);
        wrds.add(product4);
        wrds.add(product5);
        wrds.add(product6);
        wrds.add(product7);
    }

    private void findViewsById(View view) {
        productListView = (ListView) view.findViewById(R.id.list_product);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        word product = (word) parent.getItemAtPosition(position);
        Toast.makeText(activity, product.toString(), Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View view,
                                   int position, long arg3) {
        ImageView button = (ImageView) view.findViewById(R.id.favu);

        String tag = button.getTag().toString();
        if (tag.equalsIgnoreCase("grey")) {
            sharedPreference.addFavorite(activity, wrds.get(position));

            button.setTag("red");
            button.setImageResource(R.drawable.fav);
        } else {
            sharedPreference.removeFavorite(activity, wrds.get(position));
            button.setTag("grey");
            button.setImageResource(R.drawable.unfav);
        }

        return true;
    }

    @Override
    public void onResume() {
        getActivity().setTitle(R.string.app_name);
        getActivity().getActionBar().setTitle(R.string.app_name);
        super.onResume();
    }
}

这是我的列表视图。

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EDEDED" >

<ListView
    android:id="@+id/list_product"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:dividerHeight="10dp"
    android:drawSelectorOnTop="true"
    android:footerDividersEnabled="false"
    android:padding="10dp"
    android:scrollbarStyle="outsideOverlay" >

</ListView>
</RelativeLayout>

这是项目视图。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants" >

    <RelativeLayout
        android:id="@+id/pdt_layout_item"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/carname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="6dp" />

        <TextView
            android:id="@+id/cartype"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/carname"
            android:padding="6dp" />

        <ImageView
            android:id="@+id/favu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="3dp"
            android:background="@null" />
    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@id/pdt_layout_item"/>

    </RelativeLayout>

0 个答案:

没有答案