显示ListView

时间:2016-05-06 14:13:50

标签: android json database listview

我有这个Activity从我的数据库中的表中获取数据,并在ListView中显示这些数据。但问题是我想在这个ListView中点击itens,一旦用户点击一个项目,它应该将他重定向到另一个显示该项目细节的屏幕。我只显示订单号和日期,一旦点击它,它应该显示更多细节。我该怎么做?

到目前为止,这是我的代码:

public class Orders extends Fragment {

    private String jsonResult;

    //Here I get the order id and date
    private String url = "https://www.example.com/orders.php";
    private ListView listView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.orders, container, false);

        listView = (ListView) v.findViewById(R.id.listView1);
        TextView textView3 = (TextView) v.findViewById(R.id.textView16);

        //Getting current date and displaying inside the textView
        String currentDate = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault()).format(new Date());
        textView3.setText(Html.fromHtml(currentDate));

        accessWebService();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                                    long id) {

                String item = ((TextView) view).getText().toString();

                String[] parts = item.split(" ");
                String order_nbr = parts[0];
                String order_id = order_nbr.replaceAll("[#]","");

                Intent intent = new Intent(getActivity(), OrderDetails.class);
                intent.putExtra("order_id", order_id);
                System.out.println(order_id);
                startActivity(intent);

            }
        });

        return v;
    }

    // Async Task to access the web
    private class JsonReadTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            HashMap<String,String> data = new HashMap<>();
            data.put("order_id",MainScreen.USERID);

            RegisterUserClass ruc = new RegisterUserClass();

            String result = ruc.sendPostRequest(url,data);

            jsonResult = result;

            return null;
        }

        private StringBuilder inputStreamToString(InputStream is) {
            String rLine = "";
            StringBuilder answer = new StringBuilder();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));

            try {
                while ((rLine = rd.readLine()) != null) {
                    answer.append(rLine);
                }
            }

            catch (IOException e) {
                // e.printStackTrace();
                Toast.makeText(getActivity().getApplicationContext(),
                        "Error..." + e.toString(), Toast.LENGTH_LONG).show();
            }
            return answer;
        }

        @Override
        protected void onPostExecute(String result) {
            ListDrwaer();
        }
    }// end async task

    public void accessWebService() {
        JsonReadTask task = new JsonReadTask();
        // passes values for the urls string array
        task.execute(new String[] { url });
    }

    // build hash set for list view
    public void ListDrwaer() {
        List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();

        try {
            JSONObject jsonResponse = new JSONObject(jsonResult);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("orders");

            for (int i = 0; i < jsonMainNode.length(); i++) {
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                String order_number = jsonChildNode.optString("orderid");
                String order_dateNhour = jsonChildNode.optString("order_date");

                String[] parts = order_dateNhour.split(" ");
                String date = parts[0];
                String hour = parts[1];

                String[] test1 = date.split("-");
                String year = test1[0];
                String month = test1[1];
                String day = test1[2];


                String outPut = "#" + order_number + "         " + day+"/"+ month +"/"+ year + "         " + hour + "      >";
                employeeList.add(createEmployee("orders", outPut));
            }
        } catch (JSONException e) {
            Toast.makeText(getActivity().getApplicationContext(), "Error" + e.toString(),
                    Toast.LENGTH_SHORT).show();
        }

        SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), employeeList,
                android.R.layout.simple_list_item_1,
                new String[]{"orders"}, new int[] { android.R.id.text1 });
        listView.setAdapter(simpleAdapter);
    }

    private HashMap<String, String> createEmployee(String name, String number) {
        HashMap<String, String> employeeNameNo = new HashMap<String, String>();
        employeeNameNo.put(name, number);
        return employeeNameNo;
    }
}

1 个答案:

答案 0 :(得分:0)

毕竟我能够解决我的问题!我创建了一个列表的ArrayList,用于存储我想在所有订单的下一个屏幕中显示的所有数据(即使我只想在ListView 3中显示):

List<List<String>> order_details = new ArrayList<List<String>>();

这样,列表#1将用于第一个订单详细信息,#2将用于第二个订单详细信息,并且它继续。单击此ListView中的项目后,我在此ArrayList中搜索哪个列表具有此特定订单号,然后我将所有存储在该列表中的有关此特定订单的数据发送到下一个活动(订单详细信息)。它运作得很好!

相关问题