gpt4 book ai didi

java - 在 twoway-gridView 中访问 TextView

转载 作者:行者123 更新时间:2023-11-30 01:32:36 28 4
gpt4 key购买 nike


我想在双向 GridView 中使用 TextView https://github.com/jess-anders/two-way-gridview .我可以成功地将 ArrayAdapter 与双向 GridView 一起使用。它按我的意愿正常工作。但是当我改编来自 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 不同的想法。我已经向原始开发人员公开了这个问题,但欢迎在这里提供任何帮助。

最佳答案

对于 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;
}

关于java - 在 twoway-gridView 中访问 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35546090/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com