Android 如何更改弹出窗口的内容

Android 如何更改弹出窗口的内容,android,listview,popupwindow,Android,Listview,Popupwindow,我正在尝试编写一个应用程序,其中用户单击列表视图中的项目。然后会出现一个弹出窗口,其中包含有关用户单击的项目的一些信息。弹出窗口显示时没有问题,但代码不会更改文本视图或按钮的内容。任何反馈都将不胜感激 以下是listview和弹出窗口的代码: public class MyRoutes extends LinearLayout { private Context context = null; private View contentView = null; priva

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

以下是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();}

            }
        });


    }



}
公共类MyRoutes扩展了LinearLayout{
私有上下文=null;
私有视图contentView=null;
私有列表=空;
私有整数大小=0;
私人停车场平面布置;
私有视图弹出窗口=null;
公共静态PopupWindow myroutes_popupwin=null;
公共MyRoutes(上下文上下文、全部列表、int actionbarsize){
超级(上下文);
this.context=上下文;
this.list=all;
this.size=actionbarsize;
getLayout();
}
私有void getLayout(){
LayoutInflater LayoutInflater=(LayoutInflater)context.getSystemService(Service.LAYOUT\u INFLATER\u Service);
LinearLayout.LayoutParams LayoutParams=新的LinearLayout。
LayoutParams(LinearLayout.LayoutParams.MATCH_父项,LinearLayout.LayoutParams.MATCH_父项);
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.flate(R.layout.myroutes,null);
setLayoutParams(layoutParams);
这个.addView(contentView);
最终ListView lv=(ListView)findViewById(R.id.navigation\U routes);
ArrayAdapter ArrayAdapter=新的ArrayAdapter(context,android.R.layout.simple\u list\u item\u 1,list);
低压设置适配器(阵列适配器);
按钮关闭=(按钮)findViewById(R.id.navigation\u close\u routes);
nlayoutingflater=(LayoutInflater)context.getSystemService(Service.LAYOUT\u INFLATER\u Service);
popup=NLayoutFlater.充气(R.layout.route,空);
最终PopupWindow myroutes_popupwin=新PopupWindow(上下文);
myroutes_popupwin.setContentView(弹出窗口);
myroutes\u popupwin.setWindowLayoutMode(
ViewGroup.LayoutParams.MATCH_父级,
ViewGroup.LayoutParams.WRAP_内容);
//myroutes_popupwin=新的PopupWindow(弹出,LayoutParams.MATCH_父项,LayoutParams.WRAP_内容);
close.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
if(myroutes\u popupwin.isShowing()){
myroutes_popupwin.discouse();
}否则{
setVisibility(View.GONE);
}
}
});
lv.setOnScrollListener(新的OnScrollListener(){
@凌驾
公共无效onScrollStateChanged(AbsListView视图,int scrollState){
//TODO自动生成的方法存根
myroutes_popupwin.discouse();
}
@凌驾
public void onScroll(AbsListView视图,int firstVisibleItem,
int visibleItemCount,int totalItemCount){
//TODO自动生成的方法存根
myroutes_popupwin.discouse();
}
});
lv.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
字符串温度=列表获取(位置);
if(myroutes\u popupwin.isShowing()){
myroutes_popupwin.discouse();
}
试一试{
lv.setFocusableInTouchMode(真);
//做一些事情来阅读物品信息。
//myroutes_弹出窗口。显示下拉列表(视图);
myroutes_popupwin.显示位置(视图,重心,0,0);
TextView trailname=(TextView)findViewById(R.id.myroutes_name);//这就是我遇到的问题,代码无法读取TextView
trailname.getText().toString();
}catch(异常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>

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

您必须在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));

这解决了问题!非常感谢。也谢谢你的提示。