gpt4 book ai didi

java - 使用 onClickListener 将数据发送到下一个 Activity 的一些问题

转载 作者:行者123 更新时间:2023-12-01 09:34:07 27 4
gpt4 key购买 nike

我正在尝试将数据(ID 值)从一个 Activity 发送到另一个 Activity 但它不会发送正确的数据,我希望它仅将单击的项目的 ID 值发送到下一个 Activity ,这是我的代码

public class Order extends AppCompatActivity {

private ListView lvUsers;
private ProgressDialog dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
dialog = new ProgressDialog(this);
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.setMessage("Loading, please wait.....");

lvUsers = (ListView) findViewById(R.id.lvUsers);
new JSONTask().execute("http://146.185.178.83/resttest/order");

}

public class JSONTask extends AsyncTask<String, String, List<OrderModel> > {

@Override
protected void onPreExecute(){
super.onPreExecute();
dialog.show();
}

@Override
protected List<OrderModel> doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;

try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();

InputStream stream = connection.getInputStream();

reader = new BufferedReader(new InputStreamReader(stream));

StringBuffer buffer = new StringBuffer();

String line ="";
while ((line=reader.readLine()) !=null){
buffer.append(line);
}

String finalJson = buffer.toString();

JSONArray parentArray = new JSONArray(finalJson);

List<OrderModel> orderModelList = new ArrayList<>();

Gson gson = new Gson();
for (int i = 0; i < parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
OrderModel orderModel = gson.fromJson(finalObject.toString(), OrderModel.class);
orderModelList.add(orderModel);
}
return orderModelList;

}catch (MalformedURLException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if(connection !=null) {
connection.disconnect();
}
try {
if (reader !=null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(List<OrderModel> result) {
super.onPostExecute(result);
dialog.dismiss();
OrderAdapter adapter = new OrderAdapter(getApplicationContext(), R.layout.row_order, result);
lvUsers.setAdapter(adapter);

}
}
public class OrderAdapter extends ArrayAdapter {

public List<OrderModel> orderModelList;
private int resource;
private LayoutInflater inflater;
public OrderAdapter(Context context, int resource, List<OrderModel> objects) {
super(context, resource, objects);
orderModelList = objects;
this.resource = resource;
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder = null;

if(convertView == null){
holder = new ViewHolder();
convertView=inflater.inflate(resource, null);
holder.bOrderNo = (Button) convertView.findViewById(R.id.bOrderNo);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}

final int orderId = orderModelList.get(position).getId();
holder.bOrderNo.setText("Order No: " + orderModelList.get(position).getOrderId());
holder.bOrderNo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Intent intent = new Intent(Order.this, OrderSelected.class);
intent.putExtra("parameter_name", orderId);
startActivity(intent);
}
});
return convertView;
}

class ViewHolder{
private Button bOrderNo;
}
}

}

持有者在循环中执行,我猜这就是为什么它不会发送正确的 ID。我如何让它只发送点击的 orderId 的 ID

您可以检查此链接以查看 json 响应的样子 http://146.185.178.83/resttest/order

最佳答案

你的代码中有一个愚蠢的错误。我在你的代码中编辑了一行。我认为您每次都会得到相同的“orderId”而不是实际的“orderId”。检查一下这个。我希望您的问题能够得到解决。

final int index = position;
holder.bOrderNo.setText("Order No: " + orderModelList.get(position).getOrderId());
holder.bOrderNo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Intent intent = new Intent(Order.this, OrderSelected.class);
intent.putExtra("parameter_name", orderModelList.get(index).getId());
startActivity(intent);
}
});

关于java - 使用 onClickListener 将数据发送到下一个 Activity 的一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39151398/

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