Android 如何从GridView项目中获取对象单击

Android 如何从GridView项目中获取对象单击,android,android-gridview,Android,Android Gridview,我有一个名为countryActivity的活动和一个名为Country的模型类。我想在模型类中设置值,将每个对象存储在arraylist中,并在gridview中显示对象值。单击项目时,我希望检索对象。下面是我的代码。我哪里做错了,怎么办?帮帮我好吗 activity\u country.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://

我有一个名为countryActivity的活动和一个名为Country的模型类。我想在模型类中设置值,将每个对象存储在arraylist中,并在gridview中显示对象值。单击项目时,我希望检索对象。下面是我的代码。我哪里做错了,怎么办?帮帮我好吗

activity\u 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.

    CountryNameTxtBox = (EditText)findViewById(R.id.CountryNameEditText);
    CountryAboutTxtBox = (EditText)findViewById(R.id.CountryAboutEditText);
    countrygGridView = (GridView)findViewById(R.id.countryGridView);

}
if((txtName!="" || txtName!=null) &&  (txtAbout!="" || txtAbout != null))
    {
      aCountry = new Country(txtName, txtAbout);
      countries.add(aCountry);
    }

    showGrid();

}


public void showGrid()
{
     ArrayList<Country> list2 = new ArrayList<Country>();

    for(Country arrCountry : countries)
    { 
             list2.add(aCountry);

    } 


      ArrayAdapter<Country> adapter2 = new ArrayAdapter<Country>(this,android.R.layout.simple_list_item_1, list2);
      countrygGridView.setAdapter(adapter2);


 countrygGridView.setOnItemClickListener(new OnItemClickListener()
  {
@Override
public void onItemClick(AdapterView<?> parent, View v,int position, long id) 
    {
     aCountry = (Country) v.getTag();
      for(Country mCountry: countries)
      {
       if(countries.contains(aCountry))
        {
        Toast.makeText(CountryActivity.this, "match", 2000).show();
        }
          }
         }
     });
   }
 }
公共类CountryActivity扩展活动
{
ArrayList国家/地区=新的ArrayList();
国家;
EditText CountryNameTxtBox;
EditText CountryAboutXtbox;
GridView countrygGridView;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_country);
//在操作栏中显示“向上”按钮。
CountryNameTxtBox=(EditText)findViewById(R.id.CountryNameEditText);
CountryAboutTextBox=(EditText)findViewById(R.id.CountryAboutEditText);
countrygGridView=(GridView)findViewById(R.id.countryGridView);
}
if((txtName!=“”| | txtName!=null)和&(txtAbout!=“”| | txtAbout!=null))
{
a国家=新国家(txtName,txtAbout);
国家。添加(a国家);
}
showGrid();
}
公共void showGrid()
{
ArrayList list2=新的ArrayList();
适用于(国家/地区:国家)
{ 
清单2.添加(一个国家);
} 
ArrayAdapter adapter2=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,list2);
countrygGridView.setAdapter(适配器2);
countrygGridView.setOnItemClickListener(新的OnItemClickListener()
{
@凌驾
public void onItemClick(AdapterView父视图、视图v、整型位置、长id)
{
A国家=(国家)诉getTag();
适用于(国家/地区:国家/地区)
{
if(国家/地区(国家/地区))
{
Toast.makeText(CountryActivity.this,“match”,2000).show();
}
}
}
});
}
}
Country.java(模型类)

公共类国家
{
私有字符串名称;
私人串;
阵列式城市;
公共国家()
{
城市=新阵列列表();
}
公共国家(字符串名称、字符串关于)
{
this.name=name;
这个;
}
公共int getNoOfCities()
{
返回城市。大小();
}
公共长getNoOfCitizens()
{
长aPop=0L;
适用于(城市:城市)
{
aPop+=aCity.getPopulation();
}
返回aPop;
}
公共字符串getName(){
返回名称;
} 
公共字符串getAbout(){
返回大约;
} 
}

onimclick
方法中,只需执行
list2.获取(位置)
即可获得您的国家/地区

修复您的代码

     countrygGridView.setOnItemClickListener(new OnItemClickListener()
      {
    @Override
    public void onItemClick(AdapterView<?> parent, View v,int position, long id) 
        {
                 Country cnt = (Country)countrygGridView.getAdapter().getItem(position);
         });
       }
countrygGridView.setOnItemClickListener(新的OnItemClickListener()
{
@凌驾
public void onItemClick(AdapterView父视图、视图v、整型位置、长id)
{
Country cnt=(国家)countrygGridView.getAdapter().getItem(位置);
});
}

onimclick
中,对于
网格视图
,您将获得所单击项目的位置

Country country = countries.get(position);
(在onItemClick方法中,只需输入:

Country aCountry = countries.get(position);
如果确实获得了匹配项,则不需要手动签入列表,因为在网格视图中检索项目时,将以相同的顺序使用在适配器中设置的列表

您应该使用“等于”,而不是“==”,使用&&而不是| |,来比较字符串,因为我认为您希望所有条件同时发生,并在检查字符串的实际值之前检查null,所以请替换:

if((txtName!="" || txtName!=null) &&  (txtAbout!="" || txtAbout != null))
与:

若要显示名称和关于使用ArrayAdapter的信息,必须重写Country类中的toString方法

String toString() {
  return this.name + " " + this.about;
}

但是我在arrayList和adapter中添加了对象,所以在girdview中它显示了对象本身。再次单击gridview中的listItems时,我想获取该对象,以便我的模型类可以从该对象检索值。然后,您必须创建一个自定义适配器来执行此操作。简单的ArrayAdapter无法知道要显示什么。我做得对吗?:-)我认为这可以解决问题:
if(txtName!=null && !txtName.equals("") &&  txtAbout != null && !txtAbout.equals(""))
String toString() {
  return this.name + " " + this.about;
}