【问题标题】:Set visibility of TextView from different Layout not working Android从不同的布局设置 TextView 的可见性不起作用 Android
【发布时间】:2016-04-15 21:14:35
【问题描述】:

我正在使用微调器从不同的布局/xml 中隐藏 textview,但我的代码无法正常工作,我坚持这样做:(

    LayoutInflater layoutInflater =
                    (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View addView = layoutInflater.inflate(R.layout.feed_item, null);
    final LinearLayout asdasd = (LinearLayout) addView.findViewById(R.id.ll2);

                        first2 = (TextView) addView.findViewById(R.id.txtfirst2);

                        first = (TextView) addView.findViewById(R.id.txtfirst);

    final Spinner mySpinner=(Spinner) findViewById(R.id.spinner);

            mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                    String imc_met= mySpinner.getSelectedItem().toString();
                    if (imc_met.toString().equals("1")){
                        Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_SHORT).show();
                        first.setVisibility(View.GONE);
                        first2.setVisibility(View.GONE);
                        asdasd.invalidate();
                    }
 }
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

feed_item.xml

<LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:paddingLeft="@dimen/feed_item_profile_info_padd"
                android:id="@+id/ll2">

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:paddingLeft="@dimen/feed_item_profile_info_padd" >

                    <TextView
                    android:id="@+id/txtfirst2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textSize="15sp"
                    android:text="1st"
                    android:textColor="#212121"
                    android:layout_weight="1" />


                    <TextView
                    android:id="@+id/txtsecond2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textSize="15sp"
                    android:text="2nd"
                    android:textColor="#212121"
                    android:layout_weight="1" />


                    <TextView
                    android:id="@+id/txtthird2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textSize="15sp"
                    android:text="3rd"
                    android:textColor="#212121"
                    android:layout_weight="1" />

                    <TextView
                        android:id="@+id/txtfourth2"
                        android:layout_width="fill_parent"
                        android:textSize="15sp"
                        android:layout_height="wrap_content"
                        android:text="4th"
                        android:textColor="#212121"
                        android:layout_weight="1" />

                    <TextView
                        android:id="@+id/txtxquarley"
                        android:layout_width="fill_parent"
                        android:textSize="15sp"
                        android:layout_height="wrap_content"
                        android:text="Final"
                        android:textColor="#212121"
                        android:layout_weight="1"
                        />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:paddingLeft="@dimen/feed_item_profile_info_padd" >


                    <TextView
                    android:id="@+id/txtfirst"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textSize="20sp"
                    android:text="0"
                    android:textColor="#212121"
                    android:layout_weight="1" />

                    <TextView
                    android:id="@+id/txtsecond"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textSize="20sp"
                    android:text="0"
                    android:textColor="#212121"
                    android:layout_weight="1" />

                <TextView
                    android:id="@+id/txtthird"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textSize="20sp"
                    android:text="0"
                    android:textColor="#212121"
                    android:layout_weight="1" />

                    <TextView
                        android:id="@+id/txtfourth"
                        android:layout_width="fill_parent"
                        android:textSize="20sp"
                        android:layout_height="wrap_content"
                        android:text="0"
                        android:textColor="#212121"
                        android:layout_weight="1" />

                    <TextView
                        android:id="@+id/txtquarterley"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:textSize="20sp"
                        android:text="0"
                        android:textColor="#212121"
                        android:layout_weight="1"
                        />
                </LinearLayout>

logcat 没有错误,但我认为这是问题所在,我不明白。

W/ViewRootImpl:由于没有窗口焦点而丢弃事件:KeyEvent { 动作=ACTION_DOWN,keyCode=KEYCODE_ALT_RIGHT,scanCode=100, 元状态=META_ALT_ON|META_ALT_RIGHT_ON,标志=0x8, 重复计数=151480,事件时间=14043549,停机时间=5803662,设备ID=1, 源=0x301 }

我已经尝试过搜索,但仍然无法正常工作:(在这上面花了 5 个小时。我仍然无法使用上面的代码隐藏文本视图,我该怎么办?

非常感谢任何帮助!

更新

忘了告诉 ListView 及其 Custom Adapter 中使用了 feed_item.xml。

自定义适配器

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final int gradeid;
        final int subjectid;
        final int studentid;
        final String first;
        final String second;
        final String third;
        final String fourth;
        final String subjname;
        final String remrks;

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.feed_item, null);

        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();

        final FeedItem item = feedItems.get(position);

        TextView FirstG = (TextView) convertView.findViewById(R.id.txtfirst);
        TextView SecondG = (TextView) convertView.findViewById(R.id.txtsecond);
        TextView ThirdG = (TextView) convertView.findViewById(R.id.txtthird);
        TextView FourthG = (TextView) convertView.findViewById(R.id.txtfourth);
        TextView subname = (TextView) convertView.findViewById(R.id.subjectname);
        TextView quarterley = (TextView) convertView.findViewById(R.id.txtquarterley);
        TextView remarks = (TextView) convertView.findViewById(R.id.txtremarks);



        gradeid = item.getSgradeid();
        subjectid = item.getSubjectid();
        studentid = item.getStudentid();
        first = item.getFirst();
        second = item.getSecond();
        third = item.getThird();
        fourth = item.getFourth();
        subjname = item.getStudent_name();

        int subj_sum = 0;
        int finalgrade = 0;
        int rmrks1 = 0;
        int rmrks2 = 0;

        subj_sum = Integer.parseInt(first.toString()) + Integer.parseInt(second.toString()) +
                Integer.parseInt(third.toString()) + Integer.parseInt(fourth.toString());
        finalgrade = subj_sum / 4;

        if (first.toString().equals("0") || second.toString().equals("0") ||
                third.toString().equals("0") || fourth.toString().equals("0")) {
            quarterley.setText("0");

        }
        else {
            quarterley.setText(Integer.toString(finalgrade));
            rmrks1 = Integer.parseInt(quarterley.getText().toString());
            rmrks2 = rmrks1;
            if (rmrks2 > 75 || rmrks2 < 100) {
                remarks.setText("Passed");
            }
            else if (rmrks2 < 74) {
                remarks.setText("Failed");
            }
        }



        FirstG.setText(first);
        SecondG.setText(second);
        ThirdG.setText(third);
        FourthG.setText(fourth);
        subname.setText(subjname);

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        return convertView;

【问题讨论】:

  • 你看到 Toast 消息了吗?
  • @Pooya 是的,吐司正在工作
  • 清理并重建?!您的代码肯定会终止文本视图,并且您可能不需要 invalidate()。文本视图的声明也是最终的吗?并且绝对没有在代码的其他地方分配一些东西?
  • @DanielWilson 已经尝试过了,但还是一样。查看我的更新^
  • 啊,这有很大的不同 :) 我们可能需要查看更多代码,但通常您以非常不同的方式在列表中增加视图 - 通过使用适配器本身的 getView() 函数.列表经过优化以回收视图,因此您需要指导它们如何表现出与普通视图不同的行为。

标签: android listview


【解决方案1】:

您可以使用共享首选项。

在微调器的 OnItemSelect 侦听器中设置共享首选项;并在相应的活动中检索它们。

以下代码来自How to set a default value to SharedPreferences programmatically?,是设置 SharedPreference(s) 的示例:-

SharedPreferences prefs = getActivity().getSharedPreferences(
        PREFS_NAME, 0);
if (prefs.getInt("key_weight", null) == null) {
    Editor editor = prefs.edit();
    editor.putInt("key_weight", 75);
    editor.commit();
}

以下代码是从 SharedPreferences 中检索的示例:-

    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    final boolean devmode = sp.getBoolean(getResources().getString(R.string.sharedpreferencekey_developermode),false);

注意 R.string.sharedpreferencekey_developermode 拥有共享首选项键名称

【讨论】:

    猜你喜欢
    • 2011-12-16
    • 2016-01-09
    • 2019-09-24
    • 1970-01-01
    • 2012-05-01
    • 2012-12-17
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    相关资源
    最近更新 更多