未在列表视图中显示下一个列表视图中的所选项目(子菜单)

时间:2016-12-18 11:08:04

标签: android json listview

我是新的android..i已经制作了listview,其中数据来自listview中的服务器,..我想在下一页制作另一个列表视图,其中子类别将来自主要类别。因为我需要获取所选项目ID意图。 这是我的代码,当我运行应用程序时,它显示空的吐司。

public class TypeMenu extends AppCompatActivity {

    private String TAG = TypeMenu.class.getSimpleName();
    String bid;

    private ProgressDialog pDialog;
    private ListView lv;
    private static final String TAG_BID = "bid";

        // URL to get contacts JSON
    private static String url = "......com/brtemp/index.php";
    ArrayList<HashMap<String, String>> contactList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_type_menu);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        contactList = new ArrayList<>();

        lv = (ListView) findViewById(R.id.list);

        new GetContacts().execute();

        // on seleting single product
        // launching Edit Product Screen
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                pDialog = new ProgressDialog(TypeMenu.this);
                pDialog.setMessage("Loading book details.");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();
                // getting values from selected ListItem

                TextView textView = (TextView) view.findViewById(R.id.id);

                //where list_content is the id of TextView in listview_item.xml

                String text = textView.getText().toString();
                System.out.println("Choosen item= : " + text);

                Intent in = new Intent(getApplicationContext(), SubMenu.class);
                // sending pid to next activity
                in.putExtra(TAG_BID ,text);
                startActivityForResult(in, 100);
                //Toast.makeText(getApplicationContext(),"Toast" +  bid,Toast.LENGTH_LONG).show();
            }
        });
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

     /**
     * Async task class to get json by making HTTP call
     */
    private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(TypeMenu.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();
            //Toast.makeText(getApplicationContext(),"Toast",Toast.LENGTH_LONG).show();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            HttpHandler sh = new HttpHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url);

            Log.e(TAG, "Response from url: " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONArray jsonArry = new JSONArray(jsonStr);

                    for (int i = 0; i < jsonArry.length(); i++) {

                        JSONObject c = jsonArry.getJSONObject(i);
                        String id = c.getString("id");
                        String type = c.getString("type");
                        HashMap<String, String> contact = new HashMap<>();

                        contact.put("id", id);
                        contact.put("type", type);
                        contactList.add(contact);
                    }
                } catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Json parsing error: " + e.getMessage(),
                                    Toast.LENGTH_LONG)
                                    .show();
                        }
                    });
               }
            } else {
                Log.e(TAG, "Couldn't get json from server.");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                           "Couldn't get json from server. Check LogCat for possible errors!",
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });

            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * */
            ListAdapter adapter = new SimpleAdapter(
                    TypeMenu.this, contactList,
                    R.layout.list_item, new String[]{ "type"},
                    new int[]{
                    R.id.type});

            lv.setAdapter(adapter);
        }

     }
}

这是我的list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <!-- Product id (pid) - will be DDEN - used to pass to other activity -->

    <TextView
        android:id="@+id/id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/type"
        android:textSize="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

onItemClick

中尝试此操作
HashMap<String, String> selected = contactList.get(position);
String id = new ArrayList<>(selected.keySet()).get(0);
String type = selected.get(id);

Toast.makeText(getActivity(), "id = " + id + " type = " + type, Toast.LENGTH_LONG).show();