如何更改RecyclerView中每个项目的值

时间:2016-06-28 14:28:35

标签: java android android-recyclerview

我有一个RecyclerView,有2个项目。我希望能够分别更改每个项目中TextView的值。

例如:

Tomato Soup    Chicken    Lamb

Chicken Soup   Fruit      Carrot

目前,每个项目都具有相同的值。

(我使用OkHttp和一些JSON解析来从this link的网页获取值

MainActivity.java

package me.anshsehgal.foodplanner;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ArrayList<Menu> menus;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView menuRecyclerView = (RecyclerView) findViewById(R.id.menuRecyclerView);

        // Initialize contacts
        menus = Menu.createContactsList(2);
        // Create adapter passing in the sample user data
        DaysAdapter adapter = new DaysAdapter(this, menus);
        // Attach the adapter to the recyclerview to populate items
        menuRecyclerView.setAdapter(adapter);
        // Set layout manager to position the items
        menuRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        // That's all!
    }
}

DaysAdapter.java(我的recyclerview适配器)

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;

public class DaysAdapter extends
        RecyclerView.Adapter<DaysAdapter.ViewHolder> {

    public static class ViewHolder extends RecyclerView.ViewHolder{

        public TextView daysMenu;
        public TextView soupMenu;
        public TextView nonVegMainMenu;

        public ViewHolder(View itemView){

            super(itemView);

            daysMenu = (TextView)itemView.findViewById(R.id.dayMenu);
            soupMenu = (TextView)itemView.findViewById(R.id.soupMenu);
            nonVegMainMenu = (TextView)itemView.findViewById(R.id.nonVegMainMenu);
        }
    }

    // Store a member variable for the contacts
    private List<Menu> mMenuItems;

    // Store the context for easy access
    private Context mContext;

    // Pass in the contact array into the constructor
    public DaysAdapter(Context context, List<Menu> menus) {
        mMenuItems = menus;
        mContext = context;
    }

    // Easy access to the context object in the recyclerview
    private Context getContext() {
        return mContext;
    }


    @Override
    public DaysAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        // Inflate the custom layout
        View menutItemView = inflater.inflate(R.layout.days, parent, false);

        // Return a new holder instance
        ViewHolder viewHolder = new ViewHolder(menutItemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(DaysAdapter.ViewHolder viewHolder, int position) {
        Menu menuItem = mMenuItems.get(position);
        Menu.OkHttpHandler handler = new Menu.OkHttpHandler();
        TextView dayText = viewHolder.daysMenu;

        try{
            //changing value here
            String result = handler.execute("http://asehgal17.netau.net/json_get_data.php").get();
            dayText.setText(handler.getJsonDay());

            TextView soupText = viewHolder.soupMenu;
            soupText.setText(handler.getJsonSoup());

            TextView nonVegMainText = viewHolder.nonVegMainMenu;
            nonVegMainText.setText(handler.getJsonNonVegMain());
        }catch (ExecutionException e){
            e.printStackTrace();
        }catch (InterruptedException e){
            e.printStackTrace();
        }



    }

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

Menu.java(存储数据的类)

import android.os.AsyncTask;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Menu {
    private int mId;
    private String mDay;
    private String mSoup;
    private String mNonVegMain;
    private String mNonVegSide;
    private String mVegMain;
    private String mVegSide;

    public Menu(int id, String day, String soup, String nonVegMain, String nonVegSide, String vegMain, String vegSide) {
         mId = id;
         mDay = day;
         mSoup = soup;
         mNonVegMain = nonVegMain;
         mNonVegSide = nonVegSide;
         mVegMain = vegMain;
         mVegSide = vegSide;
    }

    public String getmDay() {
        return mDay;
    }

    public int getmId() {
        return mId;
    }

    public String getmNonVegMain() {
        return mNonVegMain;
    }

    public String getmNonVegSide() {
        return mNonVegSide;
    }

    public String getmSoup() {
        return mSoup;
    }

    public String getmVegMain() {
        return mVegMain;
    }

    public String getmVegSide() {
        return mVegSide;
    }


    //getting JSON response in this class
    public static class OkHttpHandler extends AsyncTask<String, Void, String> {
        String jsonDay;
        String jsonSoup;
        String jsonNonVegMain;
        String jsonNonVegSide;
        String jsonVegMain;
        String jsonVegSide;
        OkHttpClient client = new OkHttpClient();



        @Override
        protected String doInBackground(String... params) {

            Request.Builder builder = new Request.Builder();
            builder.url(params[0]);

            Request request = builder.build();

            try {

                Response response = client.newCall(request).execute();
                String jsonString = response.body().string() ;
                //parsing JSON
                JSONObject json = new JSONObject(jsonString);
                JSONArray parentArray = json.getJSONArray("server_response");
                JSONObject mondayMenu = parentArray.getJSONObject(1);
                String day = mondayMenu.getString("day");
                String soup = mondayMenu.getString("soup");
                String nonVegMain = mondayMenu.getString("non_veg_main");
                String nonVegSide = mondayMenu.getString("non_veg_side");
                String vegMain = mondayMenu.getString("veg_main");
                String vegSide = mondayMenu.getString("veg_side");
                jsonDay = day;
                jsonSoup = soup;
                jsonNonVegMain = nonVegMain;
                jsonNonVegSide = nonVegSide;
                jsonVegMain = vegMain;
                jsonVegSide = vegSide;




            } catch (IOException e) {
                e.printStackTrace();
            }catch (JSONException e){
                e.printStackTrace();
            }

            return null;
        }
        public String getJsonDay() {
            return jsonDay;
        }

        public String getJsonNonVegMain() {
            return jsonNonVegMain;
        }

        public String getJsonNonVegSide() {
            return jsonNonVegSide;
        }

        public String getJsonSoup() {
            return jsonSoup;
        }

        public String getJsonVegMain() {
            return jsonVegMain;
        }

        public String getJsonVegSide() {
            return jsonVegSide;
        }
    }
    public static ArrayList<Menu> createContactsList(int numContacts) {
        OkHttpHandler handler = new OkHttpHandler();
        ArrayList<Menu> menuItems = new ArrayList<Menu>();
        menuItems.add(new Menu(numContacts,handler.jsonDay,handler.jsonSoup,handler.jsonNonVegMain,handler.jsonNonVegSide,handler.jsonVegMain,handler.jsonVegSide));
        menuItems.add(new Menu(2,"fff","fff","e","e","3r","3rrr"));


        return menuItems;
    }
}

1 个答案:

答案 0 :(得分:2)

我想你的arrayList mMenuItems是你想要显示的不同菜单项的arraylist。您已使用

填充了ArrayList菜单
menus = Menu.createContactsList(2);

在这种情况下,你可以简单地,

public void onBindViewHolder(DaysAdapter.ViewHolder viewHolder, int position) {
    Menu menuItem = mMenuItems.get(position);
    viewHolder.daysMenu.setText(menuItem.mDay);
    viewHolder.soupMenu.setText(menuItem.mSoup);
    viewHolder.nonVegMainMenu.setText(menuItem.mNonVegMain);
    }