gpt4 book ai didi

java - 在 ViewHolder 内部类中访问 RecyclerView.Adapter 数据

转载 作者:行者123 更新时间:2023-12-02 11:18:54 26 4
gpt4 key购买 nike

我的应用程序涉及跟踪车辆通过检查门的移动。单击 NavigationView 中的菜单项后,门 id 将传递到 onNavigationItemSelected 方法:-

          case R.id.gate_two:
Intent intent2 = new Intent(getApplicationContext(), VehicleActivity.class);
intent2.putExtra(VehicleActivity.EXTRA_POSITION, 2);
startActivity(intent2);
finish();
break;

门 ID 被传递到 VehicleFragment 并按如下方式检索:

public class VehicleFragment extends Fragment {
private int gate;
private static String passvehicle_url = "http://192.168.123.123:8080/test/pass_vehicle.php";
private static String vehicle_url = "http://192.168.123.123:8080/test/track_vehicle.php";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RecyclerView recyclerView = (RecyclerView) inflater.inflate(
R.layout.recycler_view, container, false);
gate = getArguments().getInt("gate");
ContentAdapter adapter = new ContentAdapter(recyclerView.getContext(),this);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);

recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return recyclerView;
}

我通过在构造函数中传递VehicleFragment对象来访问ContentAdapter中的gate id:

public static class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ViewHolder> {

private int LENGTH=1 ;
private int gate_id=0 ;
private final String[] vehicles ;
private final String[] vehicle_type ;
VehicleFragment vf;

public ContentAdapter(Context context, VehicleFragment vf) {
this.vf = vf;
gate_id = vf.gate;

HttpURLConnection c = null;
try {
URL url = new URL(vehicle_url);

现在我需要访问 ViewHolder 静态内部类中的门 ID,但我无法做到。点击通行按钮,车辆将通过闸门

public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView vehicles;
public TextView vehicle_type;
public Button pass;

public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.vehicle_status, parent, false));

vehicles = (TextView) itemView.findViewById(R.id.vehicle_no);
vehicle_type = (TextView) itemView.findViewById(R.id.vehicle_type);
pass = (Button) itemView.findViewById(R.id.pass_vehicle);
pass.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
try {
URL url = new URL(passvehicle_url);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("timecard_no", timecard_no.getText().toString())
.appendQueryParameter("gate_id", id will be passed here );

请就如何解决该问题以及条款的设计提出建议。

最佳答案

从类ViewHolder中删除静态关键字

public static class ViewHolder extends RecyclerView.ViewHolder 

 public class ViewHolder extends RecyclerView.ViewHolder 

否则将gate_id更改为静态

private static int gate_id=0 ;

然后就可以直接访问gate_id了

关于java - 在 ViewHolder 内部类中访问 RecyclerView.Adapter 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50076027/

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