customAdapter for object isnt working

五迷三道 提交于 2019-12-13 21:28:23

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/21164478/customadapter-for-object-isnt-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!