2012-12-21 58 views
0

而不是使用像這個例子中的圖片我從Android Developer Site找到,我想在gridView中使用編輯文本。我會使用什麼樣的適配器?我甚至需要一個適配器嗎?Android中的文本適配器

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    GridView gridview = (GridView) findViewById(R.id.gridview); 
    gridview.setAdapter(new ImageAdapter(this)); 

    gridview.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
      Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

好,所以我想這樣做,但它不工作。它不斷崩潰。

#imports ....

public class MainActivity extends Activity{ 

    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_scout); 

     GridView gridView = (GridView) findViewById(R.id.gridView); 

     //final String[] letters = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I"}; 

     //ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, letters); 

     gridView.setAdapter(new ImageAdapter(this)); 

     gridView.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View v, 
        int position, long id) { 
       Toast.makeText(getApplicationContext(), ((TextView) v).getText(), Toast.LENGTH_SHORT).show(); 
      } 


     }); 

    } 
} 

ImageAdapter.java

//used for adding Edit texts to the grid view in the main activity 
#imports.... 

public class ImageAdapter extends BaseAdapter{ 

    Context context; 

    public ImageAdapter(Context context) 
    { 
     this.context = context; 
    } 


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     View gridView; 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     if(convertView == null) 
     { 
      gridView = new View(context); 
      gridView = inflater.inflate(R.layout.grid_items, null); 

      EditText editText = (EditText) gridView.findViewById(R.id.grid_item_edit_text); 
     } 
     gridView = (View)convertView; 
     return gridView; 
    } 

    @Override 
    public int getCount() { 
     return 9;//only 9 pegs on the center rack 
    } 

    @Override 
    public Object getItem(int position) { 

     return null; 
    } 

    @Override 
    public long getItemId(int position) { 

     return 0; 
    } 


} 
+0

在你的問題中,「THIS」是指什麼?放置鏈接... –

+0

@ rasen58爲此製作自定義的網格視圖,並且您想要示例,然後告訴我。 –

回答

1

你需要一個定製的GridView。換句話說,一個自定義適配器
這個tutorial將幫助你。

+0

謝謝。它幫助 – rasen58

+0

很高興它有幫助。請你可以upvote!提前致謝 –