Android 对象的customAdapter不工作
我有一个模型班。现在,在我的活动中,我想在模型类中设置值,并使用我的自定义适配器在gridview中显示它们。之后,我需要将对象存储在gridview的onItemClick中的变量(类类型)中。我已经完成了以下代码。但它不起作用。我哪里出错了 activity\u country.xmlAndroid 对象的customAdapter不工作,android,gridview,custom-adapter,Android,Gridview,Custom Adapter,我有一个模型班。现在,在我的活动中,我想在模型类中设置值,并使用我的自定义适配器在gridview中显示它们。之后,我需要将对象存储在gridview的onItemClick中的变量(类类型)中。我已经完成了以下代码。但它不起作用。我哪里出错了 activity\u country.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schem
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".CountryActivity" >
<TextView
android:id="@+id/nTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="21dp"
android:layout_marginTop="26dp"
android:text="Name" />
<TextView
android:id="@+id/aTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/nTextView"
android:layout_below="@+id/nTextView"
android:layout_marginTop="24dp"
android:text="About" />
<EditText
android:id="@+id/CountryNameEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/nTextView"
android:layout_alignBottom="@+id/nTextView"
android:layout_marginLeft="48dp"
android:layout_toRightOf="@+id/nTextView"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/CountryAboutEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/CountryNameEditText"
android:layout_alignTop="@+id/aTextView"
android:ems="10" />
<Button
android:id="@+id/saveCountryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/CountryAboutEditText"
android:layout_below="@+id/CountryAboutEditText"
android:layout_marginLeft="16dp"
android:layout_marginTop="22dp"
android:text="Save"
android:onClick="saveCountry"
/>
<GridView
android:id="@+id/countryGridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/saveCountryButton"
android:layout_centerHorizontal="true"
android:numColumns="2">
</GridView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:layout_marginTop="15sp"
android:textSize="15sp" >
</TextView>
<TextView
android:id="@+id/label2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:layout_marginTop="15sp"
android:textSize="15sp" >
</TextView>
</LinearLayout>
不延伸任何东西
应该是
public class myAdapter extends BaseAdapter //orArrayAdapter
{
如果仍有问题,请发布stacktrace以获取进一步帮助如何在此gridView中为每列添加标题?请检查并自行实现
public class myAdapter extends BaseAdapter
{
Context mycontext;
ArrayList<Country> countries;
public myAdapter(Context c, ArrayList<Country> obj)
{
this.mycontext=c;
this.countries = obj;
}
public int getCount() {
// TODO Auto-generated method stub
return countries.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return countries.get(arg0);
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertview, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) mycontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if(convertview==null)
{
gridView =new View(mycontext);
gridView =inflater.inflate(R.layout.countrygrid, null);
TextView txtvw1 = (TextView) gridView.findViewById(R.id.label1);
txtvw1.setText(countries.get(position).getName());
TextView txtvw2 = (TextView) gridView.findViewById(R.id.label2);
txtvw2.setText(countries.get(position).getAbout());
}
else
{
gridView = (View) convertview;
}
return gridView;
}
}
countrygGridView.setAdapter(new myAdapter(this,countries));
public class myAdapter
{
public class myAdapter extends BaseAdapter //orArrayAdapter
{