TextView text value refreshed onclick

谁都会走 提交于 2019-12-24 07:18:10

问题


I have a gridview in an activity where I am trying to display a contextual menu each time a grid element is tapped. I want this contextual menu to display a dynamic value in a textview (the position of the tapped grid element). I am creating my own contextual menu because I don't want an alert or the default android contextmenu. Also I am using SDKs:

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />

Here is my java.class for the gridview

    public class Grid_Items extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_grid_items);       
    GridView gridView = (GridView) findViewById(R.id.gridViewLayout);
    gridView.setAdapter(new ImageAdapter(this));
    gridView.setOnItemClickListener(new OnItemClickListener() {

    View contextualMenuView = null;         

        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            // RelativeLayout that contains parent layout
            RelativeLayout rootLayout = (RelativeLayout)findViewById(R.id.root);

            // in case there is a context menu open, remove it
            if(contextualMenuView!=null) {
                rootLayout.removeView(contextualMenuView);
                }

            // inflate contextual menu
            contextualMenuView = getLayoutInflater().inflate(R.layout.contextual_menu, rootLayout);

            // sets values for the textview
            TextView tv = (TextView) findViewById(R.id.tab1);
            tv.setText("tab1 = "+String.valueOf(position));

        }
  • The first time I click on a grid element, my context menu shows up with the correct text value in the textview
  • The 2nd time and every other times, the menu is still there but the textview text value is empty, I need this value to be "refreshed" everytime the user taps on an element in the grid

layout for the gridview activity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridViewLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center">

</GridView>

</RelativeLayout>

Layout for the contextual menu I inflate:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contextualMenuLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#ffffff">

<!-- --> 

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"   
    android:layout_marginRight="10dip"
    android:layout_marginLeft="10dip"
    android:layout_marginTop="10dip"
    android:layout_marginBottom="10dip"
    android:textSize="28sp"

    android:textColor="#ff0000">
</TextView>

What have I missed?


回答1:


Try to retrieve tab1 by using your ContextualMenuView instance (contextualMenuView):

tv = (TextView) contextualMenuView.findViewById(R.id.tab1); 

Also, could you try to inflate your ContextualMenuView in the onCreate() method and instantiate your TextView there too?



来源:https://stackoverflow.com/questions/14003892/textview-text-value-refreshed-onclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!