TextView的文本值的onclick刷新 [英] TextView text value refreshed onclick

查看:167
本文介绍了TextView的文本值的onclick刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,我想显示上下文菜单每一个网格单元被窃听时一个gridview。我想这个上下文菜单来显示一个TextView的动态值(抽头网格元素的位置)。我创造我自己的上下文菜单,因为我不想警报或默认的Andr​​oid文本菜单。
另外我使用的SDK:

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" />

下面是我的java.class为GridView

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

        }


  • 我第一次点击一格元素,我的上下文菜单显示了在TextView的正确文本值

  • 第2次和每一个其他时间,菜单仍然存在,只是TextView的文本值是空的,我需要这个值是刷新每次用户在网格中抽头的元素

  • 布局为GridView活动

    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>
    

    什么我错过了?

    推荐答案

    尝试通过使用检索的 TAB1 的你的 ContextualMenuView 的实例( contextualMenuView 的)

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

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

    另外,你可以尝试虚增您在onCreate()方法的 ContextualMenuView 的和实例化的的TextView 的存在呢?

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

    这篇关于TextView的文本值的onclick刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆