TextView文本值刷新onclick

时间:2012-12-22 15:07:01

标签: android gridview textview contextmenu

我在一个活动中有一个gridview,我每次点击一个网格元素时都会尝试显示一个上下文菜单。我希望此上下文菜单在textview中显示动态值(点击的网格元素的位置)。我正在创建自己的上下文菜单,因为我不想要警报或默认的android上下文菜单。 我也在使用SDK:

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

这是我的gridview的java.class

    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));

        }
  • 我第一次点击网格元素时,我的上下文菜单会在textview中显示正确的文本值
  • 第二次和每隔一次,菜单仍然存在,但textview文本值为空,每次用户点击网格中的元素时,我都需要“刷新”此值

gridview活动的布局

<?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>

我膨胀的上下文菜单的布局:

<?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>

我错过了什么?

1 个答案:

答案 0 :(得分:1)

尝试使用 ContextualMenuView 实例( contextualMenuView )检索 tab1

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

另外,您是否可以尝试在onCreate()方法中充实 ContextualMenuView 并在那里实例化 TextView