如何在Android的弹出窗口中更新列表视图

时间:2016-05-20 05:12:14

标签: android listview popup

我有一个标题为All Post的TextView。我显示弹出窗口点击TextView并在弹出窗口中填入两个项目的列表视图。并试图实现这一点 - 每当点击列表视图项目时该项目将在TextView和之前的TextView标题中显示All Post将在listview中添加到clickable项目的位置。但是我的listview没有更新,关闭了弹出窗口。

这是我的弹出窗口代码。

      String[] values = new String[] { "All Post", "My Post", "R-Post"};
      ArrayList<String> filter_PostType = new ArrayList<String>();
      str_LastSelectionValue  =  sharedPreferences.getString("filter_post_title", "");

       if(str_LastSelectionValue.equals("NoVal"))
        {
            editor.putString("filter_post_title","All Post");
            editor.commit();
        }
        else
        {
            str_LastSelectionValue  = sharedPreferences.getString("filter_post_title", "");
        }
        textSearch.setText(str_LastSelectionValue);
        Log.e("", " textSearch.getText().toString().trim() = " +   
        textSearch.getText().toString().trim());

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

                        popup_searchview = new PopupWindow(CustomActionActivity.this);
                        View layout = getLayoutInflater().inflate(R.layout.allpostsearch_popup, null);
                        popup_searchview.setContentView(layout);
                        popup_searchview.setHeight(220);
                        popup_searchview.setWidth(250);

                        popup_searchview.setOutsideTouchable(true);
                        popup_searchview.setFocusable(true);
                        popup_searchview.setBackgroundDrawable(new BitmapDrawable());
                        popup_searchview.showAsDropDown(v);


                        filter_list = (ListView) layout.findViewById(R.id.filter_ListView);
                        arr = new ArrayList<String>(Arrays.asList(values));
                        filter_PostType.clear();

                        for (int i = 0; i < arr.size(); i++) {
                            String str_PostType = arr.get(i);

                            if (!str_PostType.equals(str_LastSelectionValue)) {
                                filter_PostType.add(str_PostType);

                            }
                        }

                        filterPost_adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.filter_popup_list_item, R.id.text_filter_title, filter_PostType);
                        filter_list.setAdapter(filterPost_adapter);

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

                                String selectedFromList = (filter_list.getItemAtPosition(position).toString());
                                editor.putString("filter_post_title", selectedFromList);
                                editor.commit();
                                textSearch.setText(selectedFromList);
                                popup_searchview.dismiss();

                                removeItemFromList(position);
                                filterPost_adapter.notifyDataSetChanged();

                                arr = new ArrayList<String>(Arrays.asList(values));
                                filter_PostType.clear();
                                for (int i = 0; i < arr.size(); i++)
                                {
                                    String str_PostType = arr.get(i);

                                    if (!str_PostType.equals(textSearch.getText().toString().trim()))
                                    {

                                        filter_PostType.add(str_PostType);
                                        int filterSize = filter_PostType.size();
                                        Log.e(" UnMatched ", " In ListItem Click filterSize = " + filterSize);
                                    }
                                }


                                filterPost_adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.filter_popup_list_item, R.id.text_filter_title, filter_PostType);
                                filter_list.setAdapter(filterPost_adapter);

                             }
                        });
                   }
            });

1 个答案:

答案 0 :(得分:1)

使用类似的东西:

    String[] values = new String[] { "All Post", "My Post", "R-Post"};
        ArrayList<String> filter_PostType = new ArrayList<String>();
        str_LastSelectionValue  =  sharedPreferences.getString("filter_post_title", "NoVal");

         if(str_LastSelectionValue.equals("NoVal"))
          {
              editor.putString("filter_post_title","All Post");
              editor.commit();
          }
          else
          {
              str_LastSelectionValue  = sharedPreferences.getString("filter_post_title", "");
          }
          textSearch.setText(str_LastSelectionValue);
          Log.e("", " textSearch.getText().toString().trim() = " +   
          textSearch.getText().toString().trim());

      textSearch.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v)
                  {
                           str_LastSelectionValue = sharedPreferences.getString("filter_post_title", ""); 
                          popup_searchview = new PopupWindow(CustomActionActivity.this);
                          View layout = getLayoutInflater().inflate(R.layout.allpostsearch_popup, null);
                          popup_searchview.setContentView(layout);
                          popup_searchview.setHeight(220);
                          popup_searchview.setWidth(250);

                          popup_searchview.setOutsideTouchable(true);
                          popup_searchview.setFocusable(true);
                          popup_searchview.setBackgroundDrawable(new BitmapDrawable());
                          popup_searchview.showAsDropDown(v);


                          filter_list = (ListView) layout.findViewById(R.id.filter_ListView);
                          if(!filter_PostType.isEmpty())
                          filter_PostType.clear();

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


                              if (!values[i].equals(str_LastSelectionValue)) {
                                  filter_PostType.add(values[i]);

                              }
                          }

                          filterPost_adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.filter_popup_list_item, R.id.text_filter_title, filter_PostType);
                          filter_list.setAdapter(filterPost_adapter);

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

                                  String selectedFromList = (filter_list.getItemAtPosition(position).toString());
                                  editor.putString("filter_post_title", selectedFromList);
                                  editor.commit();
                                  str_LastSelectionValue=selectedFromList;
                                  textSearch.setText(selectedFromList);
                                  popup_searchview.dismiss();

                               }
                          });
                     }
              });


}

在用户选择某些内容后,您无需更改适配器,因为下次用户单击textview时,您将使用新适配器创建一个新的弹出窗口。

如果您有任何不明白的地方,请发表评论。

希望这会有所帮助:)