public class Gridview2Adapter extends BaseAdapter {
public Gridview2Adapter(Context context,List<String> list)
{
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 20;//按钮的个数
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View convertview, ViewGroup arg2) {
convertview=inflater.inflate(R.layout.gridview2_item, null);
g2_btn=(Button) convertview.findViewById(R.id.g2_btn);
//List<String> band=setChannel_band();
//给每一个button设置监听事件,即是让textview的内容显示为点击的按钮上的text作为频道
String str=(String)g2_btn.getText();
channel.setText(str);
return convertview;
}
}
public class MainActivity extends Activity {
GridView gridview2;
TextView channel;
List<String> channel_band=new ArrayList<String>();
//设置按钮默认的文字
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview2=(GridView)findViewById(R.id.gridview2);
gridview2.setAdapter(new Gridview2Adapter(this,channel_band));
channel=(TextView) findViewById(R.id.channel);
for(int j=0;j<19;j++){
channel_band.add("频道"+j);
}
gridview2.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
for(int i = 0;i<channel_band.size();i++){
if(position == i){
channel_band.get(i).setSelect(true);
}else{
channel_band.get(i).setSelect(false);
}
}
mAdapter.choiceState(position);
Toast.makeText(MainActivity.this, position+"被点击", Toast.LENGTH_SHORT).show();
}
});
}
越想越乱,不知道怎么写了