0

我不想使用我从Android Developer Site找到的这个示例中的图像,而是想在 gridView 中使用编辑文本。我会使用什么样的适配器?我什至需要适配器吗?

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

好的,所以我尝试这样做,但它不起作用。它一直在崩溃。

#进口...

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


}
4

1 回答 1

1

您需要一个自定义的 Gridview。换句话说,自定义适配器
教程将为您提供帮助。

于 2012-12-21T06:00:39.410 回答