一个GridView,点击能够互换,执行报LinearLayout cannot be cast to android.widget.GridView这个错误
privateRandomrandom;privateGridViewgridView;privateList<Map<String,Object>>dataList;p...
private Random random;
private GridView gridView;
private List<Map<String,Object>>dataList;
private int[] icon={R.drawable.img01,R.drawable.img02,R.drawable.img03,R.drawable.img04,R.drawable.img05,R.drawable.img06};
private SimpleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView=(GridView) findViewById(R.id.gridView);
gridView.setOnItemClickListener(this);
dataList=new ArrayList<Map<String,Object>>();
getData();
adapter=new SimpleAdapter(MainActivity.this, getData(), R.layout.item, new String[]{"image","text"}, new int[]{R.id.image});
gridView.setAdapter(adapter);
}
private List<Map<String,Object>> getData() {
for(int i=1;i<=50;i++)
{
Map<String,Object>map=new HashMap<String, Object>();
random=new Random();
int a=random.nextInt(6);
map.put("image", icon[a]);
//map.put("text", iconName[i]);
dataList.add(map);
}
return dataList;
}
private ImageView imageFirst;
private ImageView imageSecond;
private int firstX;
private int firstY;
private int secondX;
private int secondY;
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if(imageFirst==arg1)
{
imageFirst.setColorFilter(null);
imageFirst=null;
return;
}
if (imageFirst == null) {
imageFirst = (ImageView) arg1;
firstX = (int) arg1.getX();
firstY = (int) arg1.getY();
imageFirst.setColorFilter(Color.parseColor("#55ff0000"));
} else {
imageSecond = (ImageView) arg1;
secondX = (int) arg0.getX();
secondY = (int) arg0.getY();
exchange(imageFirst,imageSecond);
imageFirst.setColorFilter(null);
imageFirst=null;
imageSecond=null;
}
}
private void exchange(ImageView image1,ImageView image2)
{
image1.setX(secondX);
image1.setY(secondY);
image2.setX(firstX);
image2.setY(firstY);
return;
} 展开
private GridView gridView;
private List<Map<String,Object>>dataList;
private int[] icon={R.drawable.img01,R.drawable.img02,R.drawable.img03,R.drawable.img04,R.drawable.img05,R.drawable.img06};
private SimpleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView=(GridView) findViewById(R.id.gridView);
gridView.setOnItemClickListener(this);
dataList=new ArrayList<Map<String,Object>>();
getData();
adapter=new SimpleAdapter(MainActivity.this, getData(), R.layout.item, new String[]{"image","text"}, new int[]{R.id.image});
gridView.setAdapter(adapter);
}
private List<Map<String,Object>> getData() {
for(int i=1;i<=50;i++)
{
Map<String,Object>map=new HashMap<String, Object>();
random=new Random();
int a=random.nextInt(6);
map.put("image", icon[a]);
//map.put("text", iconName[i]);
dataList.add(map);
}
return dataList;
}
private ImageView imageFirst;
private ImageView imageSecond;
private int firstX;
private int firstY;
private int secondX;
private int secondY;
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if(imageFirst==arg1)
{
imageFirst.setColorFilter(null);
imageFirst=null;
return;
}
if (imageFirst == null) {
imageFirst = (ImageView) arg1;
firstX = (int) arg1.getX();
firstY = (int) arg1.getY();
imageFirst.setColorFilter(Color.parseColor("#55ff0000"));
} else {
imageSecond = (ImageView) arg1;
secondX = (int) arg0.getX();
secondY = (int) arg0.getY();
exchange(imageFirst,imageSecond);
imageFirst.setColorFilter(null);
imageFirst=null;
imageSecond=null;
}
}
private void exchange(ImageView image1,ImageView image2)
{
image1.setX(secondX);
image1.setY(secondY);
image2.setX(firstX);
image2.setY(firstY);
return;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询