1

我在GridView使用基本适配器时添加了按钮,当单击按钮然后隐藏时,按钮取决于字长。单击所有按钮后,GridView我想检查是否GridView为空显示另一个按钮。我想检查是否GridView为空GridViewActivity.java

这是代码。

GridViewActivity.java

public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spellword);

        txt=(TextView)findViewById(R.id.txtview);
        image=(ImageView)findViewById(R.id.imgview1);

        map.put("melon", R.drawable.melon);
        image.setImageResource(map.get("melon"));


        gridView=(GridView)findViewById(R.id.grid1);
        gridView.setAdapter(new SpellAdapter(this,words,word));

        detector = new SimpleGestureFilter(this, this);

    }

SpellAdapter.java

public class SpellAdapter extends BaseAdapter{

    public Context context;
    public char[] word;

    public SpellAdapter(Context context, char[] word, String orglWord)
    {
        this.context=context;
        this.word=word;

    }

    public int getCount() {
        count=word.length;
        return count;
    }

    public Object getItem(int position) {

        return null;
    }

    public long getItemId(int position) {

        return 0;
    }


    public View getView(final int position, View convertView, ViewGroup arg2) {

        View v = convertView;
        if (v == null) 
        {  
             LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             v = vi.inflate(R.layout.buttonlist, null);
        } 

        final Button btn= (Button)v.findViewById(R.id.letterbtn);

        btn.setText(word[position]+"");

        btn.setOnClickListener(new OnClickListener(){

            public void onClick(View v) 
            {
                letters=btn.getText();
                String word = letters.toString();
                btn.setVisibility(View.GONE);

            }


        });

        return v; 
    }




}
4

3 回答 3

2

if (gridView.getChildCount() == 0) {...}

如果这不起作用,请尝试:

if (SpellAdapter.isEmpty()) {...}或者我认为您需要执行以下操作:

if (gridView.getAdapter().isEmpty()) {...}

您可以在此处此处找到此信息

编辑:

哦,我想我现在明白你的意思了。你只是隐藏按钮,你不删除它。所以你需要使用一个循环。首先,获取孩子的数量。然后做一个for,比如:

for ( int x = 0; x < childcount; x++)

里面供你使用View v = gridView.getChildAt(x);

Then you do if (v instanceof Button) and inside the if you check if the button is visible or not. If it is visible, set false inside a variable like boolean gridViewEmpty and break the loop. Else, set gridViewEmpty to true and then display your other buttons.

于 2012-07-11T12:16:35.810 回答
0

you need to get child count.

if(gridView.getChildCount() == 0){
-------
-------
}
于 2012-07-11T12:29:54.860 回答
0

This question is very old but, I would also just like to add to this...

If you are displaying a GridView in a fragment or you are adding to the GridView in another Activity and then returning to that Activity you will have to have to check in OnCreate and OnResume.

The following worked for me:

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.studentfragment, null);

    gridView = (GridView) v.findViewById(R.id.gridView);

    empty = (TextView) v.findViewById(R.id.esc);

    if (gridView.getAdapter().isEmpty()){
        empty.setVisibility(View.VISIBLE);
    }else {
        empty.setVisibility(View.GONE);
    }

    return v;
}


@Override
public void onResume(){
    super.onResume();

    if (gridView.getAdapter().isEmpty()){
        empty.setVisibility(View.VISIBLE);
    }else {
        empty.setVisibility(View.GONE);
    }


}

OR you can just create a method that check if GridView is empty and call it in OnCreate and OnResume like this:

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.studentfragment, null);

    gridView = (GridView) v.findViewById(R.id.gridView);

    empty = (TextView) v.findViewById(R.id.esc);

    checkIfGridIsEmpty();

    return v;
}


@Override
public void onResume(){
    super.onResume();

    checkIfGridIsEmpty();

}

private void checkIfGridIsEmpty() {

    if (gridView.getAdapter().isEmpty()){
        empty.setVisibility(View.VISIBLE);
    }else {
        empty.setVisibility(View.GONE);
    }

}

If you are only checking in OnCreate, then you will have to closed and re-opened the Activity in order to see if there was differences made to Gridview/Adapter.

于 2017-07-14T07:22:54.723 回答