从不同的布局设置TextView的可见性不起作用的Android

时间:2016-04-15 21:14:36

标签: android listview

我正在使用微调器来隐藏不同布局/ 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 = ACTION_DOWN,keyCode = KEYCODE_ALT_RIGHT,scanCode = 100,   metaState = META_ALT_ON | META_ALT_RIGHT_ON,flags = 0x8,   repeatCount = 151480,eventTime = 14043549,downTime = 5803662,deviceId = 1,   source = 0x301}

我已经尝试过搜索但仍然无法正常工作:(已经花费了5个小时了。我仍然无法使用上面的代码隐藏textview,我应该从中做些什么?

非常感谢任何帮助!

更新

忘了告诉feed_item.xml用于 ListView 及其自定义适配器

CustomAdapter

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

1 个答案:

答案 0 :(得分:1)

您可以使用共享偏好设置。

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

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

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保存共享首选项密钥名称