如何根据您所处的活动隐藏CustomAdapter中的元素?

时间:2017-07-16 06:19:20

标签: java android arraylist android-arrayadapter

我正在尝试制作一款需要你猜数字的游戏。我让用户选择他们想要的位数。

我的自定义适配器最初是为3位数字构建的,当用户选择2位数字时,一切正常,但是当app运行时,digit_3总是0,我只是想隐藏它,我该怎么做?

See screenshot for how it looks like, I want to hide the marked part when user enters the 2 digit activity

继承人:

package com.apphorizon.guessthenumber;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;


public class ListAdapter extends ArrayAdapter<List> {

    public ListAdapter(Context context, ArrayList<List> arrayList) {
        super(context, 0, arrayList);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.list_item, parent, false);
        }


        List currentItem = getItem(position);

        TextView num1View = (TextView)listItemView.findViewById(R.id.num_1_view);
        num1View.setText(String.valueOf(currentItem.getNum1()));

        TextView num2View = (TextView)listItemView.findViewById(R.id.num_2_view);
        num2View.setText(String.valueOf(currentItem.getNum2()));

        TextView num3View = (TextView) listItemView.findViewById(R.id.num_3_view);
        num3View.setText(String.valueOf(currentItem.getNum3()));

        TextView correctPositionView = (TextView)listItemView.findViewById(R.id.correct_position_view);
        correctPositionView.setText(String.valueOf(currentItem.getCorrectPosition()));

        TextView wrongPositionView = (TextView)listItemView.findViewById(R.id.wrong_position_view);
        wrongPositionView.setText(String.valueOf(currentItem.getWrongPositionCorrectNumber()));

        return listItemView;
    }
}

这里是List类

package com.apphorizon.guessthenumber;

public class List {

    private int mNum1;
    private int mNum2;
    private int mNum3;
    private int mCorrectPosition;
    private int mWrongPositionCorrectNumber;

    public List(int num1, int num2, int num3, int correctPosition, int wrongPositionCorrectNumber){
        mNum1 = num1;
        mNum2 = num2;
        mNum3 = num3;
        mCorrectPosition = correctPosition;
        mWrongPositionCorrectNumber = wrongPositionCorrectNumber;
    }

    public List(int num1, int num2, int correctPosition, int wrongPositionCorrectNumber){
        mNum1 = num1;
        mNum2 = num2;
        mCorrectPosition = correctPosition;
        mWrongPositionCorrectNumber = wrongPositionCorrectNumber;
    }

    public int getNum1(){
        return mNum1;
    }

    public int getNum2(){
        return mNum2;
    }

    public int getNum3(){
        return mNum3;
    }

    public int getCorrectPosition(){
        return mCorrectPosition;
    }

    public int getWrongPositionCorrectNumber(){
        return mWrongPositionCorrectNumber;
    }
}

该应用目前仅针对2位和3位数字进行了自定义,但还要添加更多数据!

1 个答案:

答案 0 :(得分:0)

如果是2位数字,您希望将最后一个文本视图的可见性设置为不可见。

因此,我建议您在List课程中进行以下更改。

private int num1 = -1;
private int num2 = -1;
private int num3 = -1;

这样,如果num1num2num3为-1,则可以通过适配器中的此更改使相应的文本视图不可见

if(currentItem.getNum1() != -1) {
    TextView num1View = (TextView)listItemView.findViewById(R.id.num_1_view);
    num1View.setText(String.valueOf(currentItem.getNum1()));
}
else listItemView.findViewById(R.id.num_1_view).setVisibility(View.INVISIBLE);

同样适用于num2num3

  

该应用目前仅针对2位和3位数字进行了自定义,但还要添加更多数据!

通过此更改,您将能够实现添加更多内容的目标,因为这也将支持1位数字。如果您不想要1位数字,请不要将上述代码应用于num1,只需应用num2num3