customAdapter对象心不是工作 [英] customAdapter for object isnt working
问题描述
我有一个模型类。现在从我的活动我要设置模型类及放值;用我的自定义适配器显示他们的GridView。之后,我需要将对象从GridView的onItemClick存储在一个变量(类类型)。我也做了以下codeS。但它不工作。我去哪儿了?
activity_country.xml
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:paddingLeft =@扪/ activity_horizontal_margin
机器人:paddingRight =@扪/ activity_horizontal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=。CountryActivity>
<的TextView
机器人:ID =@ + ID / nTextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentLeft =真
机器人:layout_alignParentTop =真
机器人:layout_marginLeft =21dp
机器人:layout_marginTop =26dp
机器人:文字=名称/><的TextView
机器人:ID =@ + ID / aTextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignLeft =@ + ID / nTextView
机器人:layout_below =@ + ID / nTextView
机器人:layout_marginTop =24dp
机器人:文字=关于/><的EditText
机器人:ID =@ + ID / CountryNameEditText
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignBaseline =@ + ID / nTextView
机器人:layout_alignBottom =@ + ID / nTextView
机器人:layout_marginLeft =48dp
机器人:layout_toRightOf =@ + ID / nTextView
机器人:EMS =10>< requestFocus的/>
< /&的EditText GT;<的EditText
机器人:ID =@ + ID / CountryAboutEditText
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignLeft =@ + ID / CountryNameEditText
机器人:layout_alignTop =@ + ID / aTextView
机器人:EMS =10/><按钮
机器人:ID =@ + ID / saveCountryButton
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignLeft =@ + ID / CountryAboutEditText
机器人:layout_below =@ + ID / CountryAboutEditText
机器人:layout_marginLeft =16DP
机器人:layout_marginTop =22dp
机器人:文本=保存
安卓的onClick =saveCountry
/>< GridView控件
机器人:ID =@ + ID / countryGridView
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / saveCountryButton
机器人:layout_centerHorizontal =真
机器人:为numColumns =2>
< / GridView的>
< / RelativeLayout的>
CountryActivity.java
公共类CountryActivity扩展活动
{
ArrayList的<国家>国家=新的ArrayList<国家>();
国家aCountry;的EditText CountryNameTxtBox;
的EditText CountryAboutTxtBox;
GridView控件countrygGridView;@覆盖
保护无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_country);
//显示操作栏中的向上按钮。
setupActionBar(); CountryNameTxtBox =(EditText上)findViewById(R.id.CountryNameEditText);
CountryAboutTxtBox =(EditText上)findViewById(R.id.CountryAboutEditText);
countrygGridView =(GridView控件)findViewById(R.id.countryGridView);
} 公共无效saveCountry(查看视图)
{
字符串改为txtName = CountryNameTxtBox.getText()的toString()。
字符串txtAbout = CountryAboutTxtBox.getText()的toString()。
showGrid();
}公共无效showGrid()
{
/ *
ArrayAdapter<国家>适配器=新ArrayAdapter<国家>(这一点,android.R.layout.simple_list_item_1,国家);
countrygGridView.setAdapter(适配器);
* /
countrygGridView.setAdapter(新myAdapter(这一点,国家));
countrygGridView.setOnItemClickListener(新OnItemClickListener()
{
@覆盖
公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的ID)
{
对(国家mCountry:国)
{
如果(countries.contains(aCountry))
{
Toast.makeText(CountryActivity.this,countries.get(位置).getName(),2000).show();
}
}
}
});
}
countrygrid.xml
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=横向> <的TextView
机器人:ID =@ + ID / LABEL1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@ + /标记
机器人:layout_marginTop =15sp
机器人:TEXTSIZE =15sp>
< / TextView的><的TextView
机器人:ID =@ + ID / LABEL2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@ + /标记
机器人:layout_marginTop =15sp
机器人:TEXTSIZE =15sp>
< / TextView的>
< / LinearLayout中>
myAdapter.java
公共类myAdapter延伸BaseAdapter
{上下文mycontext;
ArrayList的<国家>国家;公共myAdapter(上下文C,ArrayList的<国家> OBJ)
{
this.mycontext = C;
this.countries = OBJ;
}
公众诠释的getCount(){
// TODO自动生成方法存根
返回countries.size();
}公共对象的getItem(INT为arg0){
// TODO自动生成方法存根
返回countries.get(为arg0);
}众长getItemId(INT为arg0){
// TODO自动生成方法存根
返回0;
}公共查看getView(INT位置,查看convertview,父母的ViewGroup)
{
LayoutInflater吹气=(LayoutInflater)mycontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看GridView控件; 如果(convertview == NULL)
{
GridView控件=新景(mycontext);
GridView控件= inflater.inflate(R.layout.countrygrid,NULL); TextView的txtvw1 =(TextView中)gridView.findViewById(R.id.label1);
txtvw1.setText(countries.get(位置).getName()); TextView的txtvw2 =(TextView中)gridView.findViewById(R.id.label2);
txtvw2.setText(countries.get(位置).getAbout()); }
其他
{
GridView控件=(查看)convertview;
}
返回GridView控件;
}
}
* 及其countryActivity.java-显示错误,当我试图设置内showGrid()适配器*
countrygGridView.setAdapter(新myAdapter(这一点,国家));
公共类myAdapter
{
不扩展任何
这应该是
公共类myAdapter扩展了BaseAdapter // orArrayAdapter
{
如果您仍然有职位的堆栈跟踪以获得更多帮助问题
I have a model class. Now from my activity i want to set the values in model class & show them in gridview using my custom adapter. After that i need to store the object in a variable(class type) from gridview's onItemClick. I have done the below codes. But its not working. Where did i go wrong?
activity_country.xml
<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>
CountryActivity.java
public class CountryActivity extends Activity
{
ArrayList<Country> countries = new ArrayList<Country>();
Country aCountry;
EditText CountryNameTxtBox;
EditText CountryAboutTxtBox;
GridView countrygGridView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_country);
// Show the Up button in the action bar.
setupActionBar();
CountryNameTxtBox = (EditText)findViewById(R.id.CountryNameEditText);
CountryAboutTxtBox = (EditText)findViewById(R.id.CountryAboutEditText);
countrygGridView = (GridView)findViewById(R.id.countryGridView);
}
public void saveCountry(View view)
{
String txtName = CountryNameTxtBox.getText().toString();
String txtAbout = CountryAboutTxtBox.getText().toString();
showGrid();
}
public void showGrid()
{
/*
ArrayAdapter<Country> adapter = new ArrayAdapter<Country>(this,android.R.layout.simple_list_item_1, countries);
countrygGridView.setAdapter(adapter);
*/
countrygGridView.setAdapter(new myAdapter(this,countries));
countrygGridView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View v,int position, long id)
{
for(Country mCountry: countries)
{
if(countries.contains(aCountry))
{
Toast.makeText(CountryActivity.this,countries.get(position).getName(), 2000).show();
}
}
}
});
}
countrygrid.xml
<?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>
myAdapter.java
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;
}
}
*Its showing error in countryActivity.java- when i am trying to set the adapter inside showGrid() *
countrygGridView.setAdapter(new myAdapter(this,countries));
public class myAdapter
{
does not extend anything
It should be
public class myAdapter extends BaseAdapter //orArrayAdapter
{
If you still have a problem post the stacktrace for further help
这篇关于customAdapter对象心不是工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!