2014-01-16 97 views
0

我有一個模型類。現在從我的活動中,我想設置模型類&中的值使用我的自定義適配器在gridview中顯示它們。之後,我需要將對象存儲在GridView的onItemClick變量(類類型)中。我已經完成了下面的代碼。但它不工作。我哪裏做錯了?customAdapter for object is not

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

* 其在countryActivity.java-顯示錯誤,當我試圖設置內showGrid()適配器*

countrygGridView.setAdapter(new myAdapter(this,countries)); 

回答

2
public class myAdapter 
{ 

不延長任何東西

它應該是

public class myAdapter extends BaseAdapter //orArrayAdapter 
{ 

如果你仍然有問題發佈stacktrace以獲得進一步幫助

+0

Lol..silly wrong .. ty。我已編輯它.. :-) –

+0

我如何添加標題爲此GridView中的每列? –

+0

檢查並實現自己http://stackoverflow.com/questions/12337331/custom-listview-with-different-row-layouts-to-be-inflated-depending-on-webservic – Raghunandan