Android 在下一个listview(子菜单)中不显示listview中的选定项

Android 在下一个listview(子菜单)中不显示listview中的选定项,android,json,listview,Android,Json,Listview,我是android新手。我制作了listview,其中数据来自listview中的服务器,…我想在下一页制作另一个listview,其中子类别将来自主类别。为此,我需要获取所选项目id以供使用。 这是我的代码,它在我运行应用程序时显示空吐司 public class TypeMenu extends AppCompatActivity { private String TAG = TypeMenu.class.getSimpleName(); String bid;

我是android新手。我制作了listview,其中数据来自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);
        }

     }
}
公共类类型菜单扩展AppCompative活动{
私有字符串标记=TypeMenu.class.getSimpleName();
串标;
私人对话;
私有ListView lv;
私有静态最终字符串标记_BID=“BID”;
//获取联系人JSON的URL
私有静态字符串url=“……com/brtemp/index.php”;
ArrayList联系人列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity类型菜单);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
contactList=新的ArrayList();
lv=(ListView)findViewById(R.id.list);
新建GetContacts().execute();
//论单产品的选择
//启动编辑产品屏幕
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
pDialog=新建进度对话框(TypeMenu.this);
setMessage(“加载书籍详细信息”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
//从选定的ListItem获取值
TextView TextView=(TextView)view.findViewById(R.id.id);
//其中,list_content是listview_item.xml中TextView的id
String text=textView.getText().toString();
System.out.println(“选择项=:”+文本);
Intent in=新的Intent(getApplicationContext(),子菜单.class);
//将pid发送到下一个活动
in.putExtra(标签投标,文本);
startActivityForResult(in,100);
//Toast.makeText(getApplicationContext(),“Toast”+bid,Toast.LENGTH\u LONG.show();
}
});
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例android.R.id.home:
onBackPressed();
返回true;
违约:
返回super.onOptionsItemSelected(项目);
}
}
/**
*异步任务类通过HTTP调用获取json
*/
私有类GetContacts扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
pDialog=新建进度对话框(TypeMenu.this);
setMessage(“请稍候…”);
pDialog.setCancelable(假);
pDialog.show();
//Toast.makeText(getApplicationContext(),“Toast”,Toast.LENGTH_LONG.show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
HttpHandler sh=新的HttpHandler();
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(url);
Log.e(标签,“来自url的响应:+jsonStr”);
if(jsonStr!=null){
试一试{
JSONArray jsonArry=新JSONArray(jsonStr);
for(int i=0;i
这是我的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>

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();
HashMap selected=contactList.get(位置);
字符串id=新的