gpt4 book ai didi

java - RecycleView 调用 onBindViewHolder() 和 onCreateViewHolder()

转载 作者:行者123 更新时间:2023-11-29 22:53:12 26 4
gpt4 key购买 nike

这是我的代码,我不知道为什么它现在不起作用 songs 是一个包含 getter setter 的简单 java 类

在从 JSON 获取数据之前,我尝试在 recycleView 中静态添加数据,并且效果非常好。

Here is code,

   public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_track, container, false);

myOnClickListener = new track.MyOnClickListener(getActivity());
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview1);
//recyclerView.setHasFixedSize(true);

layoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());

loadData();

return view;
}

code of JSON parsing using volley

  private void loadData()
{
String url = "http://homejobs.live/mydata.php";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>(){

@Override
public void onResponse(String response) {
try{

JSONArray jsonArray = new JSONArray(response);
Log.d("test", String.valueOf(jsonArray));
Log.d("test", String.valueOf(jsonArray.length()));
for(int i =0 ; i < jsonArray.length() ; i++)
{
JSONObject jsonObject = jsonArray.getJSONObject(i);
songs item = new songs(
jsonObject.getString("id"),
jsonObject.getString("name"),
jsonObject.getString("uri")

);

Log.d("key", String.valueOf(item));

data.add(item);
}

adapter = new recycler_view_adapter(data);
recyclerView.setAdapter(adapter);

} catch (JSONException e) {
e.printStackTrace();
}
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("error"+error);
}
}

);

mRequestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
mRequestQueue.add(stringRequest);

}

recycler_view_adapter class

public class recycler_view_adapter extends RecyclerView.Adapter<recycler_view_adapter.MyViewHolder> {

private ArrayList<songs> dataSet;

public static class MyViewHolder extends RecyclerView.ViewHolder {

public static TextView textViewName;
public static ImageView imageViewIcon;

public MyViewHolder(View itemView) {
super(itemView);
this.textViewName = (TextView) itemView.findViewById(R.id.textView);
this.imageViewIcon = (ImageView) itemView.findViewById(R.id.imageView3);
}
}

public recycler_view_adapter(ArrayList<songs> data) {
this.dataSet = data;
}

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

view.setOnClickListener(track.myOnClickListener);

MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}


@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
Log.d("hy","test");
songs songsData = dataSet.get(position);


MyViewHolder.textViewName.setText(songsData.getName());
System.out.println("hello"+position);

Picasso.get()
.load(songsData.getImage())
.into(MyViewHolder.imageViewIcon);
}

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

}

最佳答案

在您的 onBindViewHolder 中,更改以下代码

MyViewHolder.textViewName.setText(songsData.getName());
System.out.println("hello"+position);

Picasso.get()
.load(songsData.getImage())
.into(MyViewHolder.imageViewIcon);

以下内容

holder.textViewName.setText(songsData.getName());
System.out.println("hello"+position);

Picasso.get()
.load(songsData.getImage())
.into(holder.imageViewIcon);

在您的代码中,您尝试使用类名访问 TextView 。相反,您应该使用 holder 变量。

另请检查您是否正确解析了 json。检查是否

data.add(item);

在 api 响应中被执行。另一个建议是在适配器构造函数中检查数据集数组的大小。

关于java - RecycleView 调用 onBindViewHolder() 和 onCreateViewHolder(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57708707/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com