点击按钮更改列表视图的内容(在同一屏幕上)

时间:2017-03-08 05:48:29

标签: android listview

所以在我的MainActivity中有3个房间(来自服务器)。当我点击它们时弹出窗口打开。这个弹出窗口将显示roomId列表(点击列表视图项目)。这样可以正常工作。

现在我需要这样。在弹出单击下一个按钮下一个房间用api来获取它的模块是调用并更新列表视图。 我是怎么做到的?

问题是: - 无论何时去做总是打开新的弹出窗口。

enter image description here

如图所示,我需要更改工具栏右箭头按钮上列表视图的内容。 我的listview是popup.so当我更改listview的内容时,另一个新的弹出窗口打开新列表。

这是我的第一个列表视图(我加载了所有图像)

modules_list.setOnItemClickListener(
                        new AdapterView.OnItemClickListener() {
                            public void onItemClick(AdapterView<?> arg0,
                                                    android.view.View arg1, int arg2, long arg3) {


                                editor.putInt("response_position",arg2).commit();

                                editor.putString("roomid",response.getRooms()[arg2+1].getRooms_id().getRoomId()).commit();

                                String serverURL = "http://dashboard.droidhomes.in/api/module?room_id=" + response.getRooms()[arg2].getRooms_id().getRoomId();

                                // Use AsyncTask execute Method To Prevent ANR11 Problem
                                new GetallModules().execute(serverURL);


                            }
                        });

GetAllmodules类获取Room的所有模块和开关: -

private class GetallModules extends AsyncTask<String, Void, Void> {

    // Required initialization

    // private final HttpClient Client = new DefaultHttpClient();
    private String Content;
    private String Error = null;

    private ProgressDialog Dialog = new ProgressDialog(
            MainActivity.this);

    protected void onPreExecute() {
        // NOTE: You can call UI Element here.

        Dialog.setMessage("Please wait..");
        Dialog.show();

    }

    // Call after onPreExecute method
    protected Void doInBackground(String... urls) {

        /************ Make Post Call To Web Server ***********/
        BufferedReader reader = null;
        try {

            // Defined URL where to send data

            URL url = new URL(urls[0]);

            // Send POST data request

            pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
            editor = pref.edit();

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            String auth_token = pref.getString("auth_token", "");
            conn.setRequestProperty("Authorization", auth_token);

            reader = new BufferedReader(new InputStreamReader(
                    conn.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line = null;

            // Read Server Response
            while ((line = reader.readLine()) != null) {
                // Append server response in string
                sb.append(line + "\n");
            }

            // Append Server Response To Content String
            Content = sb.toString();
        } catch (Exception ex) {
            Error = ex.getMessage();
        } finally {
            try {

                reader.close();
            } catch (Exception ex) {
            }
        }

        /*****************************************************/
        return null;
    }

    protected void onPostExecute(Void unused) {
        // NOTE: You can call UI Element here.

        // Close progress dialog
        // Content=Content.replaceAll("<string>","");
        // Content=Content.replaceAll("</string>", "");
        Dialog.dismiss();

        if (Error != null) {

            Toast toast = Toast.makeText(getApplicationContext(),
                    "Bad request", Toast.LENGTH_LONG);
            toast.show();
            // uiUpdate.setText("Output : " + Error);

        } else {

            // Show Response Json On Screen (activity)
            // uiUpdate.setText(Content);

            /****************** Start Parse Response JSON Data *************/

            // String OutputData = "";
            // JSONObject jsonResponse;

            Gson gson = new Gson();

            final SearchResponse response = gson.fromJson(Content,
                    SearchResponse.class);

            if (response.getStatus() == true) {


                //response.getAuth_token()

                if (response.getModule().length > 0) {



                    LayoutInflater li1 = LayoutInflater.from(MainActivity.this);
                    View promptsView = li1.inflate(R.layout.prompts_modulesdialog1, null);

                    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
                    alertDialogBuilder.setView(promptsView);

                    ListView list_modules = (ListView) promptsView.findViewById(R.id.lv_modules);
                    TextView tv_roomname = (TextView) promptsView.findViewById(R.id.tv_roomName);

                    ImageView iv_left=(ImageView)promptsView.findViewById(R.id.iv_left);
                    ImageView iv_right=(ImageView)promptsView.findViewById(R.id.iv_right);

                    ArrayList<String> switches = new ArrayList<String>();

                    iv_right.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {


                            String room_id=pref.getString("roomid","");

                            String serverURL = "http://dashboard.droidhomes.in/api/module?room_id=" + room_id;

                            // Use AsyncTask execute Method To Prevent ANR11 Problem
                            new GetallModules().execute(serverURL);




                        }
                    });
                    for (int i = 0; i < response.getModule().length; i++) {

                        tv_roomname.setText(response.getModule()[i].getRoom_title());

                        for (int j = 0; j < response.getModule()[i].getSwitches().length; j++) {
                            if (response.getModule()[i].getModule_name().equals(response.getModule()[i].getSwitches()[j].getModule_name_switches())) {

                                switches.add(response.getModule()[i].getSwitches()[j].getSwitch_name());

                            }


                        }


                    }
                    ToggleButtonListAdapter t1=new ToggleButtonListAdapter(MainActivity.this, switches);
                    list_modules.setAdapter(t1);

                    t1.notifyDataSetChanged();
                    AlertDialog alertDialog = alertDialogBuilder.create();
                    alertDialog.show();


                } else {

                    Toast.makeText(getApplicationContext(), "No module defined for this room!!!!!!", Toast.LENGTH_SHORT).show();



                }

            } else {

                Toast.makeText(getApplicationContext(), "status:-" + response.getStatus(), Toast.LENGTH_SHORT).show();
            }


        }

    }

}

Same Scenario like this.

请帮助我!!

0 个答案:

没有答案
相关问题