Meyka Jograt
Meyka Jograt

Reputation: 314

Set visibility of TextView from different Layout not working Android

Im using spinner to hide textview from different layout/xml but my code is not working and Im stuck on this :(

    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>

There's no error in logcat but I think this is the problem and I don't get it.

W/ViewRootImpl﹕ Dropping event due to no window focus: 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 }

Already tried I've search but it still not working :( Been spending 5 hours on this. And still I can't hide the textview using the code above what should I do from this?

Any help is much appreciated!

UPDATE

Forgot to tell that feed_item.xml is used in ListView and its Custom Adapter.

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;

Upvotes: 0

Views: 200

Answers (1)

MikeT
MikeT

Reputation: 57073

You could use shared preferences.

Set the Shared Preference(s) in the spinner's OnItemSelect listener; and retrieve them in the respective activity.

The following code, from How to set a default value to SharedPreferences programmatically?, is an example for setting the 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();
}

The following code is an example of retrieving from the SharedPreferences :-

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

Noting that R.string.sharedpreferencekey_developermode holds the Shared Preferences Key Name

Upvotes: 1

Related Questions