gpt4 book ai didi

java - 根据RecyclerView中的json文件Id传递Json数据

转载 作者:行者123 更新时间:2023-12-01 19:44:52 25 4
gpt4 key购买 nike

我遇到了一个问题,我尝试了很多次但没有成功,请您帮助我!我有一个 RecyclerView,可以从 json 文件获取数据。我有两个 json 文件,我的 json 文件如下所示:

{ "ussd_code":[
{"cmd_id": 1,"cmd_name": "Solde de crédit","cmd_instruction": "*100#","cmd_commentaire": null,"opetor_id": 2,"mnu_id": 12,"test":2},
{"cmd_id": 2,"cmd_name": "Solde de crédit","cmd_instruction": "*565#","cmd_commentaire": null,"opetor_id": 1,"mnu_id": 12,"test":1},
{"cmd_id": 3,"cmd_name": "Solde de crédit","cmd_instruction": "*124#","cmd_commentaire": null,"opetor_id": 3,"mnu_id": 12,"test":3},
{"cmd_id": 4,"cmd_name": "Airtel Money","cmd_instruction": "*501#","cmd_commentaire": null,"opetor_id": 1,"mnu_id": 12,"test":1},
{"cmd_id": 5,"cmd_name": "Mon numéro","cmd_instruction": "*502#","cmd_commentaire": null,"opetor_id": 1,"mnu_id": 12,"test":1},
{"cmd_id": 6,"cmd_name": "8 minutes (Vers airtel)","cmd_instruction": "*171*50#", "cmd_commentaire": "50 Unités pour 1 jour","opetor_id": 1,"mnu_id": 11,"test":1},
{"cmd_id": 7,"cmd_name": "10 min (Tous réseau)","cmd_instruction": "*171*0100#","cmd_commentaire": "100 Unités pour 1 jour","opetor_id": 1,"mnu_id": 11,"test":1}]}

还有另一个:

{"operator":[
{"opetor_id": 1,"opetor_name": "Airtel","ctry_id": "CD"},
{"opetor_id": 2,"opetor_name": "Vodacom","ctry_id": "CD"},
{"opetor_id": 3,"opetor_name": "Orange","ctry_id": "CD"},
{"opetor_id": 4,"opetor_name": "Tigo","ctry_id": "CD"}]}

一旦验证两个 json 文件中的 operator_id 相同,我就会在 RecyclerView 中传递数据,这是我的代码:

String cmd_id = new String();
String cmd_name = new String();
String cmd_instruction = new String();
String cmd_commentaire = new String();
...
for (int z = 0; z < jArrayMenu.length(); z++)
{
JSONObject MnuDetail = jArrayMenu.getJSONObject(z);
Integer operatorIdMenu = MnuDetail.getInt("opetor_id");
//cmd_operator_test.add(MnuDetail.getInt("opetor_id"));
if (OperaID.equals(operatorIdMenu))
{
cmd_id=(MnuDetail.getString("cmd_id"));
cmd_name=(MnuDetail.getString("cmd_name"));
cmd_instruction=(MnuDetail.getString("cmd_instruction"));
cmd_commentaire=(MnuDetail.getString("cmd_commentaire"));
CustomAdapter customAdapter = new CustomAdapter(MainActivity.this, cmd_id, cmd_name, cmd_instruction, cmd_commentaire);
//set the Adapter to RecyclerView
rv.setAdapter(customAdapter);
}
}

这是我的适配器:

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

String cmd_id = new String();
String cmd_name = new String();
String cmd_instruction = new String();
String cmd_commentaire = new String();
ArrayList<String> cmd_operator = new ArrayList<>();

Context context;

public CustomAdapter(Context context, String cmd_id, String cmd_name, String cmd_instruction, String cmd_commentaire) {
this.context = context;
this.cmd_id = cmd_id;
this.cmd_name = cmd_name;
this.cmd_instruction = cmd_instruction;
this.cmd_commentaire = cmd_commentaire;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// inflate the item Layout
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);
MyViewHolder vh = new MyViewHolder(v); // pass the view to View Holder
return vh;
}

@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
// set the data in items
//holder.name.setText(cmd_id.get(position));
holder.email.setText(cmd_name);
//holder.mobileNo.setText(cmd_instruction.get(position));
// implement setOnClickListener event on item view.
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// display a toast with person name on item click
//Toast.makeText(context, cmd_name.get(position), Toast.LENGTH_SHORT).show();
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
Toast.makeText(MainActivity.this, "Please grant the permission to call", Toast.LENGTH_SHORT).show();
requestPermissions();
}else {
//Toast.makeText(orange.this, "Recycler View Item: " + i, Toast.LENGTH_LONG).show();
//final Intent myIntent;
Intent myIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + cmd_instruction.replace("#","") + Uri.encode("#")));
startActivity(myIntent);
}
}
});
}

@Override
public int getItemCount() {
return cmd_id.length();
}

public class MyViewHolder extends RecyclerView.ViewHolder {
TextView name, email, mobileNo;// init the item view's

public MyViewHolder(View itemView) {
super(itemView);

// get the reference of item view's
name = (TextView) itemView.findViewById(R.id.name);
email = (TextView) itemView.findViewById(R.id.email);
mobileNo = (TextView) itemView.findViewById(R.id.mobileNo);
}
}
}

当我运行我的应用程序时,ussd_code json 文件中 opetor_id 等于 1 的地方它只给我最后一个值,否则我有五个 opetor_id 但仍然具有最后的值。不知道为什么其他的没有出现。

最佳答案

您正在遍历循环并每次创建自定义适配器的新实例。因此,到循环结束时,只会保留最后一个实例。

因此,您需要做的是创建一个模型,其中包含变量 cmd_id、cmd_name、cmd_instruction 和 cmd_commentaire。然后创建一个model类型的ArrayAdapter。

当您遍历循环时,只需向该 ArrayAdapter 添加新模型,不要每次都创建适配器的新实例。

更新:创建 JSON 的 POJO。

List<MyPojo> lstPojo = new ArrayList<~>;
for (int z = 0; z < jArrayMenu.length(); z++)
{
JSONObject MnuDetail = jArrayMenu.getJSONObject(z);
int operatorIdMenu = MnuDetail.getInt("opetor_id");
//cmd_operator_test.add(MnuDetail.getInt("opetor_id"));
if (OperaID == operatorIdMenu)
{
lstPojo.add(new MyPojo(MnuDetail.getString("cmd_id"),
MnuDetail.getString("cmd_name"),
MnuDetail.getString("cmd_instruction"),
MnuDetail.getString("cmd_commentaire")));
}

}
CustomAdapter customAdapter = new CustomAdapter(MainActivity.this,
android.R.layout.simple_spinner_item,
lstPojo);
rv.setAdapter(customAdapter);

希望这有帮助。

关于java - 根据RecyclerView中的json文件Id传递Json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53758170/

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