如何在recyclelerview适配器中使用getItematposition()作为ListView

时间:2015-10-13 09:39:30

标签: java android android-listview android-recyclerview

我有一个RecyclerViewAdapter用StoreName,image等填充我的List,我有listitems.ok的模型,我从ListView迁移到recylerview。我想点击任何项目并获取项目数据(如商店名称)和意图到另一个活动,现在问题是,当我点击项目时,它给出了错误的商店名称。但是当我使用提供正确输入的onItemclickListener时。我会告诉你..

这是我的 RecyclerViewAdapter

 public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.DataObjectHolder> {
    private static String LOG_TAG = "MyRecyclerViewAdapter";
    private List<StoresItem> mDataset;
    private static MyClickListener myClickListener;
    Context context;

    Bitmap xx;

    public MyRecyclerViewAdapter(Context context, List<StoresItem> myDataset)
    {
        mDataset = myDataset;
    }

    public static class DataObjectHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView txtStoreName,txtDeliveryTime;
        ImageView storeImg;
        RatingBar ratingBar;



        public DataObjectHolder(View itemView)
        {


            super(itemView);
            txtStoreName = (TextView) itemView.findViewById(R.id.txtStoreName);
            txtDeliveryTime = (TextView) itemView.findViewById(R.id.txtDeliveryTime);
            storeImg = (ImageView) itemView.findViewById(R.id.storeImg);
            ratingBar=(RatingBar)itemView.findViewById(R.id.ratingBar);
            Log.i(LOG_TAG, "Adding Listener");
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v)
        {

            myClickListener.onItemClick(getLayoutPosition(), v);
        }
    }



    public void setOnItemClickListener(MyClickListener myClickListener)
    {
        this.myClickListener = myClickListener;
    }



    @Override
    public DataObjectHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.storelist, parent, false);

        DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
        return dataObjectHolder;
    }

    @Override
    public void onBindViewHolder(DataObjectHolder holder, int position)
    {

        ImageView storeImg;

        holder.txtStoreName.setText(mDataset.get(position).gettxtStoreName().toString());
        holder.txtDeliveryTime.setText(mDataset.get(position).gettxtDeliveryTime().toString());
        holder.ratingBar.setFocusable(false);


        int loader = R.drawable.image;

        String url = mDataset.get(position).getstoreImg().toString();

        ImageLoader imgLoader = new ImageLoader(context);

        imgLoader.DisplayImage(url,loader,holder.storeImg);


    }



    public void addItem(StoresItem dataObj, int index) {
        mDataset.add(dataObj);
        notifyItemInserted(index);
    }

    public void deleteItem(int index) {
        mDataset.remove(index);
        notifyItemRemoved(index);
    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }

    public interface MyClickListener
    {
        public void onItemClick(int position, View v);
    }

    public Object getItem(int position)
    {
        return mDataset.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return mDataset.indexOf(getItem(position));
    }


    @Override
    public int getItemViewType(int position)
    {
        return super.getItemViewType(position);
    }
}

这是我的 StoreModel

public class StoresItem
{
        private String txtStoreName, txtDeliveryTime, storeImg;

        public StoresItem(String txtStoreName, String txtDeliveryTime,String storeImg)
        {

            this.txtStoreName = txtStoreName;
            this.txtDeliveryTime = txtDeliveryTime;
            this.storeImg = storeImg;

        }

    public String gettxtStoreName()
    {
        return txtStoreName;
    }

    public void settxtStoreName(String txtStoreName)
    {
        this.txtStoreName = txtStoreName;
    }

    public String gettxtDeliveryTime()
    {
        return txtDeliveryTime;
    }

    public void settxtDeliveryTime(String txtDeliveryTime)
    {
        this.txtDeliveryTime = txtDeliveryTime;
    }

    public String getstoreImg()
    {
        return storeImg;
    }

    public void setstoreImg(String storeImg)
    {
        this.storeImg = storeImg;
    }


}

这是我的片段,我使用 onclickListner

 public class RecylerFragment extends Fragment
{

    ListView storeListView;
    List<StoresItem> storesItem;
    StoresListAdaptor adapter;
    String selectedStore;

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;



    public static final String[] storeNameGrocery = new String[]
            {
                    "Value Store", "Grocery Mart", "Wild Water-Sadar Bazar", "Store99","Coco-Buzz", "Wild Water - Sector 46"
            };
    public static final String[] storeNameFresh = new String[]
            {
                    "Mother's Love", "The Flower Shop", "Flower N Emotions"
            };
    public static final String[] storeNameElectronics = new String[]
            {
                    "Molife-Sheetla Mata Road", "Apple Store- Shona Road", "Vandana Communication", "Samsung Store","JMD Mobile - Sector 14", "Sterling - Gurgaon"
            };
    public static final String[] storeNameVegetable = new String[]
            {
                    "Fortune Store", "Best Food - Mega Mall", "Sadar Bazar", "MTR Store","Coco-Buzz"
            };

    public static final String[] deliveryTime = new String[]
            {
                    "20min", "10min", "9min", "15min","30min", "1hr","20min","20min", "10min", "9min", "15min","30min", "1hr","20min" };


    private static final String KEY_POSITION="position";



    public static final String[] iconsUrl = new String[]
            {
                    "http://icons.iconarchive.com/icons/femfoyou/angry-birds/96/angry-bird-icon.png",
                    "http://icons.iconarchive.com/icons/rokey/the-blacy/96/grimace-icon.png",
                    "http://wcdn1.dataknet.com/static/resources/icons/set106/fec007de.png",
                    "http://cdn4.aptoide.com/imgs/7/6/e/76e0376928b8393227a150fbed5d6b4a_icon_96x96.png",
                    "http://vaivocabulary.com/vai/images/android.png",
                    "http://cdn4.aptoide.com/imgs/7/6/e/76e0376928b8393227a150fbed5d6b4a_icon_96x96.png",
                    "http://vaivocabulary.com/vai/images/android.png",
                    "http://wcdn1.dataknet.com/static/resources/icons/set106/fec007de.png",
            };


    public RecylerFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

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


        Bundle bundle = this.getArguments();
        String myString = bundle.getString("page_position");

        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);


        RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL);
        mRecyclerView.addItemDecoration(itemDecoration);

        // Code to Add an item with default animation
        //((MyRecyclerViewAdapter) mAdapter).addItem(obj, index);

        // Code to remove an item with default animation
        //((MyRecyclerViewAdapter) mAdapter).deleteItem(index);


        storesItem = new ArrayList<StoresItem>();

        if(myString.equals("999")) {
            for (int i = 0; i < storeNameGrocery.length; i++) {
                StoresItem item = new StoresItem(storeNameGrocery[i], "Delivery Time " + deliveryTime[i],iconsUrl[i]);
                storesItem.add(item);
            }
        }

        else if(myString.equals("888")) {
            for (int i = 0; i < storeNameFresh.length; i++) {
                StoresItem item = new StoresItem(storeNameFresh[i], "Delivery Time " + deliveryTime[i], iconsUrl[i]);
                storesItem.add(item);
            }
        }

        else if(myString.equals("777")) {
            for (int i = 0; i < storeNameElectronics.length; i++) {
                StoresItem item = new StoresItem(storeNameElectronics[i], "Delivery Time " + deliveryTime[i], iconsUrl[i]);
                storesItem.add(item);
            }
        }

        else if(myString.equals("666")) {
            for (int i = 0; i < storeNameVegetable.length; i++) {
                StoresItem item = new StoresItem(storeNameVegetable[i], "Delivery Time " + deliveryTime[i], iconsUrl[i]);
                storesItem.add(item);
            }
        }

        mAdapter = new MyRecyclerViewAdapter(getActivity(), storesItem);
        mRecyclerView.setAdapter(mAdapter);




        return rootView;
    }


    **@Override
    public void onResume() {
        super.onResume();
        ((MyRecyclerViewAdapter) mAdapter).setOnItemClickListener(new MyRecyclerViewAdapter.MyClickListener()
        {
        @Override
         public void onItemClick(int position, View v)
        {
            System.out.println(" Clicked on Item " + position);
            StoresItem storesItems = (StoresItem) ((MyRecyclerViewAdapter) mAdapter).getItem(position);
            Bundle args = new Bundle();
            Intent in = new Intent(getActivity(), ProductActivity.class);
            args.putString("SelectedStoreName", storesItems.gettxtStoreName().toString());
            in.putExtras(args);
            startActivity(in);
        }
       });
    }**


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}

okk ... recyclerviews存在问题 - onItemclicklister因为列表视图 - onItemCLicklistner()   提供Adapterview,我可以像这样使用parent.getItematposition(position)来获取项目。

 storeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            StoresItem storesItems = (StoresItem) parent.getItemAtPosition(position);
            Bundle args = new Bundle();
            Intent in = new Intent(getActivity(), ProductActivity.class);
            args.putString("SelectedStoreName", storesItems.gettxtStoreName().toString());
            in.putExtras(args);
            startActivity(in);

        }
    });

可以任何人告诉我什么是getWrong StoreName的原因。??

1 个答案:

答案 0 :(得分:0)

您正在使用myClickListener.onItemClick(getLayoutPosition(), v);,您必须使用myClickListener.onItemClick(getAdapterPosition(), v);