如何更改弹出窗口的内容

时间:2015-06-26 18:55:41

标签: android listview popupwindow

我正在尝试编写一个应用,其中用户点击列表视图中的项目。然后会出现一个弹出窗口,其中包含有关用户点击的项目的一些信息。弹出窗口显示没有问题,但代码不会更改TextViews或Buttons的内容。任何反馈将不胜感激。

以下是listview和弹出窗口的代码:

public class MyRoutes extends LinearLayout {

    private Context context = null;
    private View contentView = null;
    private List<String> list=null;
    private int size = 0;
    private LayoutInflater nlayoutInflater;
    private View popup = null;
    public static PopupWindow myroutes_popupwin=null;



    public MyRoutes(Context context , List<String> all , int actionbarsize) {
        super(context);
        this.context = context;
        this.list=all;
        this.size=actionbarsize;
        getLayout();
    }

    private void getLayout() {


        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.
                LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);   

        layoutParams.topMargin = (int) ((1.0)*(int)(size));
        layoutParams.leftMargin = (int)((0.0)*(int)(size));
        layoutParams.bottomMargin=(int) ((0.0)*(int)(size));
        layoutParams.rightMargin= (int)((0.0)*(int)(size));        

        contentView = layoutInflater.inflate(R.layout.myroutes, null);
        contentView.setLayoutParams(layoutParams);
        this.addView(contentView);
        final ListView lv = (ListView) findViewById(R.id.navigation_routes);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1, list);
        lv.setAdapter(arrayAdapter);

        Button close = (Button) findViewById(R.id.navigation_close_routes);

        nlayoutInflater = (LayoutInflater) context.getSystemService(Service.LAYOUT_INFLATER_SERVICE);   
        popup = nlayoutInflater.inflate(R.layout.route, null);

        final PopupWindow myroutes_popupwin = new PopupWindow(context);
        myroutes_popupwin.setContentView(popup);
        myroutes_popupwin.setWindowLayoutMode(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

//      myroutes_popupwin=new PopupWindow(popup, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

        close.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(myroutes_popupwin.isShowing()){
                    myroutes_popupwin.dismiss();
                }else{
                contentView.setVisibility(View.GONE);
                }

            }
        });

        lv.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub
                myroutes_popupwin.dismiss();
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                // TODO Auto-generated method stub
                myroutes_popupwin.dismiss();
            }
        });

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                String temp = list.get(position);
                if(myroutes_popupwin.isShowing()){
                    myroutes_popupwin.dismiss(); 
                }
                try{


                lv.setFocusableInTouchMode(true);

                // doing some stuff to read item information.                   

//              myroutes_popupwin.showAsDropDown(view);
                myroutes_popupwin.showAtLocation(view, Gravity.CENTER, 0, 0);




                TextView trailname = (TextView) findViewById(R.id.myroutes_name); // THIS IS where I have problem, the code is not able to read the textview
                trailname.getText().toString();


                }catch(Exception e){e.printStackTrace();}

            }
        });


    }



}

这是弹出窗口的代码

<?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:paddingTop="0dp"
    android:paddingRight="5dp"
    android:paddingLeft="5dp"
    android:background="#C0000000"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/myroutes_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Trail Name"
        android:paddingBottom="2dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="5dp"        
        android:textStyle="italic" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Date"
        android:textSize="15sp"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"
        android:textStyle="italic" 
        android:paddingRight="5dp"
        android:paddingTop="3dp"
        android:text="Date" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Total Distance"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="10dp"
        android:text="Total Distance"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="italic" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Average Speed"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="5dp"
        android:text="Average Speed"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="italic" />
     <TextView
        android:id="@+id/textView4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Duration"
        android:paddingBottom="15dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="5dp"
        android:text="Duration"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="italic" />
<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="0dp"
    android:background="#00000000"
    android:orientation="horizontal" >
     <Button
         android:id="@+id/myroutes_display"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:drawableTop="@drawable/add"
         android:layout_marginLeft="1dp"
         android:text="display"
         android:paddingBottom="5dp"
         android:textSize="10sp"
         android:background="#00000000"
         android:textColor="@color/AliceBlue"/>

     <Button
         android:id="@+id/imageButton2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:drawableTop="@drawable/add"
         android:layout_marginLeft="1dp"
         android:text="resume"
         android:paddingBottom="5dp"
         android:textSize="10sp"
         android:background="#00000000"
         android:textColor="@color/AliceBlue"/>

     </LinearLayout>

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="0dp"
    android:background="#00000000"
    android:orientation="horizontal" >
     <Button
         android:id="@+id/imageButton3"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:drawableTop="@drawable/add"
         android:layout_marginLeft="1dp"
         android:text="share"
         android:paddingTop="5dp"
         android:paddingBottom="10dp"
         android:textSize="10sp"
         android:background="#00000000"
         android:textColor="@color/AliceBlue"/>

     <Button
         android:id="@+id/imageButton4"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:drawableTop="@drawable/add"
         android:layout_marginLeft="1dp"
         android:text="delete"
         android:paddingTop="5dp"
         android:paddingBottom="10dp"
         android:textSize="10sp"
         android:background="#00000000"
         android:textColor="@color/AliceBlue"/>

     </LinearLayout>





</LinearLayout>

2 个答案:

答案 0 :(得分:2)

您必须在ContentView上致电findViewById()并致电setText()更改您的内容。

TextView textView = (TextView) popup.getContentView().findViewById(R.id.textView_id);
textView.setText("New Content");


一个小提示:如果你在PopupWindow上设置了一个背景,你可以免费获得PopupWindow之外的触摸的解雇行为。

popup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

答案 1 :(得分:0)

结帐AlertDialogDialogFragment。这些应该比构建自己的弹出式解决方案更容易使用。