在twoway-gridView

时间:2016-02-22 05:00:10

标签: java android gridview


我想在双向gridview https://github.com/jess-anders/two-way-gridview中使用TextView。 我可以成功使用带有双向gridview的ArrayAdapter。它可以正常工作。 但是当我从How to add a TextView to a GridView in Android调整示例时 访问TextView时出现NullPointerException。
这是我的代码 MainActivity.java:

public class MainActivity extends AppCompatActivity {
    static final String[] numbers = new String[] {
            "A", "B", "C", "D", "E",
            "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O",
            "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TwoWayGridView gridview = (TwoWayGridView) findViewById(R.id.gridview);
        gridview = (TwoWayGridView) findViewById(R.id.gridview);
//        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
//                android.R.layout.simple_list_item_1, numbers);
        TextViewAdapter adapter = (TextViewAdapter) new TextViewAdapter(this, numbers);
        gridview.setAdapter(adapter);

    }

}

layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<com.jaikra.sarit.wecare.TwoWayGridView.TwoWayGridView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridView1"
    android:numColumns="auto_fit"
    android:gravity="center"
    android:columnWidth="100dp"
    android:stretchMode="columnWidth"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</com.jaikra.sarit.wecare.TwoWayGridView.TwoWayGridView>

item.xml:

<TextView
    android:id="@+id/grid_item_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:layout_marginTop="5px"
    android:textSize="15px"
    xmlns:android="http://schemas.android.com/apk/res/android">
</TextView>

TextViewAdapter.java:

public class TextViewAdapter extends BaseAdapter {
    private Context context;
    private final String[] textViewValues;

    public TextViewAdapter(Context context, String[] textViewValues) {
        this.context = context;
        this.textViewValues = textViewValues;
    }

    @Override
    public int getCount() {
        return textViewValues.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null) {

            gridView = new View(context);

            gridView = inflater.inflate(R.layout.layout, null);

            // set value into textview
            TextView textView = (TextView) gridView
                    .findViewById(R.id.grid_item_label);
            textView.setText(this.textViewValues[position]);

            textView.setTextColor(Color.GREEN);

        } else {
            gridView = (View) convertView;
        }

        return gridView;
    }
}

问题:

FATAL EXCEPTION: main java.lang.NullPointerException at com.jaikra.sarit.wecare.TextViewAdapter.getView(TextViewAdapter.java:54)

错误:
这是第54和55行。

    TextView textView = (TextView) gridView
            .findViewById(R.id.grid_item_label);
    textView.setText(this.textViewValues[position]);

问题: 如何解决这个问题?
我应该阅读什么样的编程主题?

几次尝试后:
我开始相信twoway-gridview在幕后面有一些与普通gridView不同的东西。我已经向原始开发者公开了这个问题,但欢迎任何帮助。

2 个答案:

答案 0 :(得分:1)

对于GridView的项目,您应该inflate

View view = inflater.inflate(R.layout.item, null);
TextView textView= (TextView)view.findViewById(R.id.label);
textView.setText(this.textViewValues[position]);
textView.setTextColor(Color.GREEN);

希望这有帮助。

<强>更新

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null) {
            gridView = inflater.inflate(R.layout.item, null);

            // set value into textview
            TextView textView = (TextView) gridView.findViewById(R.id.label);
            textView.setText(this.textViewValues[position]);

            textView.setTextColor(Color.GREEN);

        } else {
            gridView = (View) convertView;
        }

        return gridView;
    }

答案 1 :(得分:0)

不要将gridView设置为新实例,请更改为:

gridView = convertView;

然后膨胀它。

<强> EDITED

在我的适配器中,我使用ViewHolders,这就是我要做的事情:

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

    ViewHolder holder;
    View vi = convertView;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        vi = inflater.inflate(R.layout.layout, null);

        holder = new ViewHolder();
        holder.textView = (TextView) vi.findViewById(R.id.grid_item_label);

        vi.setTag(holder);

    } else {
        holder = (ViewHolder)vi.getTag();
    }

    textView.setText(this.textViewValues[position]);

    textView.setTextColor(Color.GREEN);

    return vi;

}

别忘了添加:

public static class ViewHolder{

    public TextView textView;

}