gpt4 book ai didi

java - RecyclerView 不显示任何内容

转载 作者:行者123 更新时间:2023-11-29 19:15:28 25 4
gpt4 key购买 nike

好的,我有两个 TextView “name”和“id”,我想从 recyclerview 和 json 中填充它们。我可以在 Android Monitor 中获取 json,但它在 recyclerview 中没有显示任何内容!

这是我的 MainActivity.java :

    txt = (TextView) findViewById(R.id.name);
idd = (TextView) findViewById(R.id.id);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new
LinearLayoutManager(getApplicationContext()));
listItems = new ArrayList<>();
loadRecyclerViewData();
}
public void loadRecyclerViewData()
{
final String URL_DATA = "my url";
final StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_DATA, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray array = jsonObject.getJSONArray("");

for(int i=0;i<array.length();i++)
{
JSONObject o = array.getJSONObject(i);
ListItem_Ostan item = new ListItem_Ostan(o.getString("name"),o.getString("id"));
listItems.add(item);
}
adapter = new MyAdapter_Ostan(listItems,getApplicationContext());
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {


}
})
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);
}

这是我的 ListItem_ostan.java :

public class ListItem_Ostan {
private String id,name;

public ListItem_Ostan(String id, String name)
{
this.id = id;
this.name = name;
}

public String getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

这是我的 MyAdapter_ostan.java :

public class MyAdapter_Ostan extends RecyclerView.Adapter<MyAdapter_Ostan.ViewHolder> {
private List<ListItem_Ostan> listItems;
Context context;

public MyAdapter_Ostan(List<ListItem_Ostan> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}


@Override
public MyAdapter_Ostan.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layouts_ostan, parent, false);
return new ViewHolder(v);
}


@Override
public void onBindViewHolder(final MyAdapter_Ostan.ViewHolder holder, int position) {
final ListItem_Ostan listItem = listItems.get(position);
LinearLayout linearLayout = holder.linearLayout;
holder.id.setText(listItem.getId());
holder.name.setText(listItem.getName());
final CardView cardView = holder.cardView;

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

public class ViewHolder extends RecyclerView.ViewHolder {
TextView name,id;
LinearLayout linearLayout;
ArrayList<ListItem_Ostan> listItem = new ArrayList<ListItem_Ostan>();
CardView cardView;
Context context;
public TextView getName() {
return name;
}



public ViewHolder(View itemView)
{
super(itemView);
cardView = (CardView)itemView.findViewById(R.id.card_view);
name = (TextView)itemView.findViewById(R.id.name);
id = (TextView)itemView.findViewById(R.id.id);
}

这是我的 json 响应:

Value [{"id":"1","name":"Sydney"},{"id":"2","name":"Shiraz"},{"id":"3","name":"Tokyo"},{"id":"4","name":"Tehran"}]

有什么问题吗?我该如何解决?请帮忙!

最佳答案

你的JSON

[{"id":"1","name":"Sydney"},{"id":"2","name":"Shiraz"},{"id":"3","name":"Tokyo"},{"id":"4","name":"Tehran"}]

你可以这样试试

         JSONArray jArray = new JSONArray(s);

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

JSONObject jObject = jArray.getJSONObject(i);
String name = jObject.getString("name");
String id= jObject.getString("id");
System.out.println("id: " + i + " IntelliJ Amiya " + jObject.getString("id"));
ListItem_Ostan item = new ListItem_Ostan(name,id);
listItems.add(item);

}

关于java - RecyclerView 不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43655613/

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