我正在尝试创建一个GridView应用程序,就像我使用BaseAdapter创建自定义ListView一样。
但是在这个GridView Mainactivity.java
中,在类CustomAdapter中,扩展BaseAdapter无法返回images.length
,其中images是一个整数数组,我已经在其中获取了所有必需的图像。
此外,我无法使用getLayoutInflater()
,它显示无法解析方法getLayoutInflater()
;
下面我附上了一个屏幕截图,你可以看到我试图返回长度,但它显示错误,类似布局充气器
[在第39行显示错误] [1]
[在第59行显示错误] [2]
以下是代码段。
Mainactivity.java
class customadapter extends BaseAdapter {
@Override
public int getCount() {
return images.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
// Everytime the user wants to display one item to the user, it simply calls the
//getview method , specifies the position of the item
view = getLayoutInflater().inflate(R.layout.custom, null);
return null;
}
}
请打开以下链接以获取代码段快照
答案 0 :(得分:0)
你必须在custom.xml
中进行更改
在此,LinerLayout
width
wrap_content
和height
wrap_content
为inflate
。
你必须View view = LayoutInflater.from(mContext).inflate(R.layout.custom, parent, false);
布局如下面的适配器。
fill_value
答案 1 :(得分:0)
您需要在适配器中设置图像以及正确设置布局
public class MainActivity extends AppCompatActivity
{
int[] images =
{R.drawable.Harvey, R.drawable.Litt,R.drawable.Mike,R.drawable.harvey,
R.drawable.mark_wahlberg
, R.drawable.oliver};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView)findViewById(R.id.grid1);
customadapter adapter = new customadapter(images);
gridView.setAdapter(adapter);
}
}
这将有助于我猜测。
class customadapter extends BaseAdapter
{
private int[] images;
public customadapter(int[] images) {
this.images = images;
}
@Override
public int getCount()
{
return images.length;
}
@Override
public Object getItem(int i)
{
return null;
}
@Override
public long getItemId(int i)
{
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup)
{
// Everytime the user wants to display one item to the user, it simply calls the
//getview method , specifies the position of the item
view = getLayoutInflater().inflate(R.layout.custom,null);
return null;
}
}
答案 2 :(得分:0)
像这样更改你的代码,
<?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:id="@+id/relativeID"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/messageRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.design.widget.TextInputLayout
android:id="@+id/inputLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="My Hint Text"
app:hintEnabled="true"
app:hintAnimationEnabled="false">
<EditText
android:hint="Type here"
android:id="@+id/messageEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</RelativeLayout>