【问题标题】:How to get the object from GridView itemClick如何从 GridView itemClick 中获取对象
【发布时间】:2014-08-28 16:25:48
【问题描述】:

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

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.

    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();
        }
          }
         }
     });
   }
 }

Country.java(模型类)

public class Country 
{

private String name;
private String about;
    ArrayList<City> cities;

public Country()
{
    cities = new ArrayList<City>();
}

   public Country(String name, String about)
{
    this.name= name;
    this.about = about;
}


public int getNoOfCities()
{
    return cities.size();
}


public long getNoOfCitizens()
{
       Long aPop= 0L;
    for(City aCity: cities)
    {
             aPop += aCity.getPopulation();
    }
    return aPop;
}



public String getName(){
    return name;

} 
public String getAbout(){
    return about;

} 
}

【问题讨论】:

标签: android android-gridview


【解决方案1】:

onItemClick 方法中,只需执行list2.get(position) 即可获得您的国家/地区。

【讨论】:

  • 但是我在 arrayList 和适配器中添加了对象,所以在 girdview 中它显示了对象本身。我想在 gridview 中显示值(名称和来自 Country.java 类的内容)。再次单击gridview中的listItems时,我想获取对象,以便我的模型类可以从对象中检索值。
  • 然后您必须创建一个自定义适配器来执行此操作。简单的 ArrayAdapter 不知道你要显示什么。
【解决方案2】:

修正你的代码

     countrygGridView.setOnItemClickListener(new OnItemClickListener()
      {
    @Override
    public void onItemClick(AdapterView<?> parent, View v,int position, long id) 
        {
                 Country cnt = (Country)countrygGridView.getAdapter().getItem(position);
         });
       }

【讨论】:

    【解决方案3】:

    onItemClickGridView 中,您将获得您点击的项目的位置

    Country country = countries.get(position);
    

    Refer this

    【讨论】:

      【解决方案4】:

      (在onItemClick方法中,放:

      Country aCountry = countries.get(position);
      

      您肯定会得到一个匹配项,您不需要手动签入列表,因为您在适配器中设置的列表在检索网格视图中的项目时以相同的顺序使用。

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

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

      与:

      if(txtName!=null && !txtName.equals("") &&  txtAbout != null && !txtAbout.equals(""))
      

      【讨论】:

        【解决方案5】:

        如果要使用 ArrayAdapter 显示 nameabout,则必须重写 Country 类中的 toString 方法。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-04-13
          • 1970-01-01
          • 2019-01-13
          • 2010-11-17
          • 2010-11-23
          • 1970-01-01
          相关资源
          最近更新 更多