Android GridLayoutManager和CustomAdapter在使用“this”为自己调用时出错

时间:2017-03-19 06:23:49

标签: java android android-fragments android-recyclerview android-cardview

我想解析JSON并在Android中使用RecyclerView和CardView进行显示。我有一个tabHost菜单,它有三个片段活动。我将CardView的所有代码放入其中一个片段中,但当我使用“this”调用GridLayoutManager和CustomAdapter时,代码有错误。

这是片段活动中的代码,错误在onViewCreated:

  

final TextView tv =(TextView)view.findViewById(R.id.tv);   adapter = new CustomAdapter(this,data_list);

错误消息是:

  

错误:(75,51)错误:不兼容的类型:Tab2Fragment无法转换为Context,GridLayoutManager中的GridLayoutManager(android.content.Context,int)无法应用于(com.xxx.xxx.xxx.Tab2Fragment)

public class Tab2Fragment extends Fragment {

private RecyclerView recyclerView;
private GridLayoutManager gridLayoutManager;
private CustomAdapter adapter;
private List<MyData> data_list;

public Tab2Fragment() {
}

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

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

}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    SharedPreferences settings = this.getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
    String username = settings.getString("username","default"); // get back the stored share preferences
    final TextView tv = (TextView) view.findViewById(R.id.tv);
    recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    data_list  = new ArrayList<>();
    load_data_from_server(username);

    gridLayoutManager = new GridLayoutManager(this,2);
    recyclerView.setLayoutManager(gridLayoutManager);

    adapter = new CustomAdapter(this,data_list);
    recyclerView.setAdapter(adapter);


}

private void load_data_from_server(final String username) {

    AsyncTask<String,Void,Void> task = new AsyncTask<String, Void, Void>() {
        @Override
        protected Void doInBackground(String... strings) {

            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url("http://www.tootcards.com/instaidea/cardapi.php?username="+strings[0])
                    .build();
            try {
                Response response = client.newCall(request).execute();

                JSONArray array = new JSONArray(response.body().string());

                for (int i=0; i<array.length(); i++){

                    JSONObject object = array.getJSONObject(i);

                    MyData data = new MyData(object.getInt("shop_id"),object.getString("shop_name"),
                            object.getString("cardimg"));

                    data_list.add(data);
                }



            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                System.out.println("End of content");
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            adapter.notifyDataSetChanged();
        }
    };

    task.execute(username);
}


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

}

CustomAdapter.java中的代码:

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

private Context context;
private List<MyData> my_data;

public CustomAdapter(Context context, List<MyData> my_data) {
    this.context = context;
    this.my_data = my_data;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card,parent,false);

    return new ViewHolder(itemView);
}

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

    holder.description.setText(my_data.get(position).getDescription());
    Glide.with(context).load(my_data.get(position).getImage_link()).into(holder.imageView);

}

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

public  class ViewHolder extends  RecyclerView.ViewHolder{

    public TextView description;
    public ImageView imageView;

    public ViewHolder(View itemView) {
        super(itemView);
        description = (TextView) itemView.findViewById(R.id.description);
        imageView = (ImageView) itemView.findViewById(R.id.image);
    }
}
}

myData.java中的代码:

class MyData {
private int id;
private String description, image_link;

public MyData(int id, String description, String image_link) {
    this.id = id;
    this.description = description;
    this.image_link = image_link;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getImage_link() {
    return image_link;
}

public void setImage_link(String image_link) {
    this.image_link = image_link;
}
}

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

更改

  

  

getActivity()

试试这个

 final TextView tv = (TextView) view.findViewById(R.id.tv); adapter = new CustomAdapter(getActivity(), data_list);

final TextView tv = (TextView) view.findViewById(R.id.tv); adapter = new CustomAdapter(getContext(), data_list);