自定义gird适配器代码的活动是空白的?

时间:2017-01-16 14:03:30

标签: android gridview android-adapter custom-object

我正在测试一个带有自定义对象的简单网格适配器,该应用程序在设备上运行时没有问题,但是保持空白而不是使用指定的网格项对活动进行膨胀。这是我的代码。

主要活动

package com.example.nobodyme.errorrepository;

  import android.app.Activity;
  import android.os.Bundle;
  import android.support.v7.app.ActionBarActivity;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.GridView;
  import android.widget.TextView;
  import android.widget.Toast;
  import android.view.View;
  import android.widget.AdapterView.OnItemClickListener;

  import java.util.ArrayList;

  public class MainActivity extends ActionBarActivity {

  GridView gridView;


  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  ArrayList<GridItems> gridItems = new ArrayList<>();
  gridItems.add(new GridItems("Android", 2));
  gridItems.add(new GridItems("Java", 3));

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

  gridView.setAdapter(new GridViewAdapter(this, gridItems));



  }

  }

GridViewAdapter

    package com.example.nobodyme.errorrepository;

    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.List;



    public class GridViewAdapter extends ArrayAdapter<GridItems> {

        private Context context;
        int b;

        public GridViewAdapter(Context context, List<GridItems> gridItems) {
            super(context, 0, gridItems);
            b=gridItems.size();
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            View gridView = convertView;

            if(gridView == null)
            {

                gridView = LayoutInflater.from(getContext()).inflate(
                                           R.layout.grid_view, parent, false);

            }

            GridItems currentgriditem = getItem(position);

            TextView mMaintextView = (TextView) gridView.findViewById(R.id.grid_item_main);
            mMaintextView.setText(currentgriditem.getTitle());


            TextView mUnansweredtextView = (TextView) gridView.findViewById(R.id.grid_item_unanswered);
            mUnansweredtextView.setText(currentgriditem.getUnansweredNo());


            return gridView;

        }

        @Override
        public int getCount() {
            return b;
        }


        @Override
        public long getItemId(int position) {
            return 0;
        }

    }

GridItems

package com.example.nobodyme.errorrepository;

/**
 * Created by nobodyme on 15/1/17.
 */

public class GridItems {

    /** Grid title*/
    private String mTitle;

    /** NO of unanswered items in the gird **/
    private Integer mUnansweredNo;

    public GridItems(String Title, Integer UnansweredNo) {
        mTitle = Title;
        mUnansweredNo = UnansweredNo;
    }



    public String getTitle() {
        return mTitle;
    }


    public Integer getUnansweredNo() {
        return mUnansweredNo;
    }


}

我根据评论编辑了代码,应用程序仍然崩溃。

4 个答案:

答案 0 :(得分:3)

您正在覆盖getCount并在适配器中返回零。请勿覆盖此项,或返回正确数量的项目。

答案 1 :(得分:2)

请检查以下代码:

@Override
public int getCount() {
    return gridItems.size();
}

而不是0返回,您应该返回列表大小。

答案 2 :(得分:1)

您需要在arrayAdapter上定义gridItems List并在构造函数上设置它 并覆盖getCount以返回gridItems(adapterGridItems)大小

    private List<GridItems> adapterGridItems;

    public GridViewAdapter(Context context, List<GridItems> gridItems) {
            super(context, 0, gridItems);
            //set adapterGridItems
            this.adapterGridItems=gridItems;
     }
     @Override
     public int getCount() {
         return adapterGridItems.size();
     }

请检查

mMaintextView.setText(currentgriditem.getUnansweredNo());

你传递的是一个整数,所以mMainTextView.setText会找到一个带有currentgriditem.getUnansweredNo()id的资源,我想你试图将currentgriditem.getUnansweredNo()设置为字符串,这样就行了

mMaintextView.setText(currentgriditem.getUnansweredNo()+"");

你确定这是mMaintextView.setText(currentgriditem.getUnansweredNo()+&#34;&#34;);

而不是

mUnansweredtextView.setText(currentgriditem.getUnansweredNo()+""); 

答案 3 :(得分:0)

为了告诉适配器要显示多少个GridItem,我们需要覆盖@samsad所示的getCount方法。

编译器抱怨它无法识别符号网格项的原因是因为您还没有创建一个字段来存储适配器中GridItems的引用。

尝试在适配器中为GridItem的ArrayList创建一个字段以解决问题。