gpt4 book ai didi

android - JSON 对象的适配器

转载 作者:行者123 更新时间:2023-11-30 04:58:35 26 4
gpt4 key购买 nike

我从API获取JSON格式的数据,有id和url。现在,我需要为我显示的每个图像创建一个按钮“添加到收藏夹”。当我尝试设置 adapter.setListener(this); 时,出现错误,因为我无法使用字符串格式。

我该如何解决这个问题?我花了 5 个小时解决这个问题:(

主要 Activity :

protected void onCreate(Bundle savedInstanceState) {

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView = findViewById(R.id.listItem);
favorites = findViewById(R.id.buttonFav);
catDetailsArrayList = new ArrayList<>();
myAdapter = new MyAdapter(MainActivity.this ,catDetailsArrayList);
searchbtn = findViewById(R.id.buttonSearch);

searchbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
catDetailsArrayList.clear();
myAdapter.notifyDataSetChanged();
displayCats();
}
});


});


}

private void displayCats() {
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());


StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {

try{

JSONArray jsonArray = new JSONArray(response);

for(int i=0; i<jsonArray.length(); i++){
JSONObject jsonObject1 = jsonArray.getJSONObject(i);

String jsonCatUrl2 = jsonObject1.getString("url");
String jsonCatId2 = jsonObject1.getString("id");

CatDetails catDetails = new CatDetails();

catDetails.setUrl(jsonCatUrl2);
catDetails.setId(jsonCatId2);

catDetailsArrayList.add(catDetails);

}
listView.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();


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


}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),error.getMessage(),Toast.LENGTH_LONG).show();
}
});


requestQueue.add(stringRequest);
}

我的适配器:

public class MyAdapter extends BaseAdapter {


public Activity activity;
public ArrayList<CatDetails> catDetailsArrayList;
public LayoutInflater inflater;

Button btn;
TextView idnr;


public MyAdapter(Activity activity, ArrayList<CatDetails> catDetailsArrayList) {
this.activity = activity;
this.catDetailsArrayList = catDetailsArrayList;
}

@Override
public Object getItem(int position) {
return catDetailsArrayList.get(position);
}


@Override
public long getItemId(int position) {
return (long)position;
}

@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
if (inflater == null) {
inflater = this.activity.getLayoutInflater();
}


if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
}

ImageView imageView = convertView.findViewById(R.id.ImageView);
final CatDetails catDetails = this.catDetailsArrayList.get(position);
Picasso.get().load(catDetails.getUrl()).into(imageView);
idnr =convertView.findViewById(R.id.textView);
btn = convertView.findViewById(R.id.buttonFav);
final String id = catDetails.getId();
idnr.setText(catDetails.getId());
return convertView;
}

@Override
public int getCount() {
return this.catDetailsArrayList.size();
}

我显示我从服务器收到的每个项目的 ID,没关系,但我不知道如何设置“添加到收藏夹”按钮才能正常工作。它必须接收项目 ID(我从服务器接收到的)作为参数,但 ID 是字符串格式。

最佳答案

 final String id = catDetails.getId();

改成

final String id = Integer.toString(catDetails.getId());

关于android - JSON 对象的适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58685029/

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