使用onClickListner将数据发送到下一个活动的一些问题

时间:2016-08-25 17:30:11

标签: java android arrays listview android-studio

我正在尝试将数据(ID值)从一个活动发送到其他活动 但它不会发送正确的数据,我希望它只将Clicked Item的ID值发送到下一个活动,这是我的代码

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 Response如何看起来像http://146.185.178.83/resttest/order

2 个答案:

答案 0 :(得分:1)

你的代码中有一个愚蠢的错误。我在你的代码中编辑过单行。我认为你正在变得相同&#34; orderId&#34;每次而不是实际&#34; orderId&#34;。检查一下。我希望你的问题能解决。

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);
        }
    });

答案 1 :(得分:0)

请尝试 代替

intent.putExtra("parameter_name", orderId);

请放

intent.putExtra("parameter_name", orderModelList.get(position).getId());