i am currently in the middle of making a basic gridview with custom layout. although i am only showing text in the gridview for now. i follow some tutorial online and follow everything step by step, i dont get any error from the android studio and i run it. But when i run it, it only show a blank white page. i did initialize the adapter this time, but it still show nothing. help what did i miss in this.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<TextView
android:id="@+id/firsttext"
android:layout_width="match_parent"
android:layout_height="@dimen/ButtonSize"
android:text="@string/hello"
android:gravity="center"/>
<GridView
android:layout_below="@+id/firsttext"
android:id="@+id/firstList"
android:layout_width="match_parent"
android:layout_height="200dp"
android:columnWidth="100dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth">
</GridView>
</RelativeLayout>
gridlist.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/gridtext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello"
android:textSize="@dimen/ButtonSize" />
</RelativeLayout>
gridadapter.java
public class gridAdapter extends BaseAdapter {
Context context;
String[] names;
LayoutInflater layoutInflater;
public gridAdapter(Context context,String[] names) {
this.context = context;
this.names = names;
}
@Override
public int getCount() {
return names.length;
}
@Override
public Object getItem(int position) {
return names[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View gridView = convertView;
if (convertView != null){
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
gridView = layoutInflater.inflate(R.layout.gridlist,null);
}
TextView textView = (TextView)gridView.findViewById(R.id.gridtext);
textView.setText(names[position]);
return gridView;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
String[] Names = {"great","good","average","great","good","average","great","good","average","great","good","average","great","good","average","great","good","average"};
GridView grid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
grid = (GridView)findViewById(R.id.firstList);
gridAdapter adapter = new gridAdapter(this,Names);
grid.setAdapter(adapter);
}
}
Change this line to: in gridadapter:
if (convertView == null) {
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
gridView = layoutInflater.inflate(R.layout.gridlist, parent, false);
}
请勿在网格列表中使用match_parent
,而TextView
的layout_heiht
通过wrap_content
对其进行补充,并为TextView
设置特殊颜色(白色除外)。
In adapter change this :
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView= layoutInflater.inflate(R.layout.gridlist,null);
TextView textView = (TextView)convertView.findViewById(R.id.gridtext);
textView.setText(names[position]);
return convertView;
}
Use RecyclerView instead of GridView in future. Your problem is with setting color for TextView, check to set textColor to black
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.