如何在Android中检查GridView是否为空?

时间:2012-07-11 12:12:21

标签: java android

我使用基础适配器在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; 
    }




}

3 个答案:

答案 0 :(得分:2)

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

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

if (SpellAdapter.isEmpty()) {...}或者我认为您需要做类似的事情:

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

您可以找到此信息herehere

编辑:

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

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

在你的内部使用View v = gridView.getChildAt(x);

然后你执行if (v instanceof Button)并在if内检查按钮是否可见。如果它是可见的,则在boolean gridViewEmpty之类的变量中设置false并打破循环。否则,将gridViewEmpty设置为true,然后显示其他按钮。

答案 1 :(得分:0)

你需要得到孩子的数量。

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

答案 2 :(得分:0)

这个问题已经很老了,但我还想加入这个......

如果您在片段中显示GridView,或者您要在另一个GridView中添加Activity,然后又返回到Activity,那么您必须签入OnCreateOnResume

以下对我有用:

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);
    }


}

您可以创建一个方法来检查GridView是否为空,并在OnCreateOnResume中调用它,如下所示:

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);
    }

}

如果您只是在OnCreate办理登机手续,则必须关闭并重新打开Activity,以查看是否存在与Gridview /的差异Adapter