android开发问题 重写BaseAdapter中的getView,position为0时,改变不了相应位置的图片。问题补充中有代码 15
packageshannil.Colorful_Ball;importandroid.app.Activity;importandroid.os.Bundle;impor...
package shannil.Colorful_Ball;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
public class Colorful_Ball extends Activity
{
private GridView gridview;
private TextView textview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridview = (GridView)findViewById(R.id.gridView1);
textview = (TextView)findViewById(R.id.textView1);
gridview.setNumColumns(4);
final MyGridViewAdapter adapter=new MyGridViewAdapter(this);
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new GridView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
// TODO Auto-generated method stub
adapter.imageview[position].setImageResource(R.drawable.orange);
textview.setText(Integer.toString(position));
}
});
}
}
package shannil.Colorful_Ball;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.content.Context;
import android.widget.ImageView;
import android.widget.GridView;
public class MyGridViewAdapter extends BaseAdapter
{
private Context mContext;
public ImageView[] imageview = new ImageView[16];
public MyGridViewAdapter (Context c)
{
mContext=c;
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return mThumbIds.length;
}
@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return mThumbIds[position];
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return position;
//return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
if(convertView==null)
{
imageview[position]=new ImageView(mContext);
imageview[position].setLayoutParams(new GridView.LayoutParams(70, 70));
imageview[position].setScaleType(ImageView.ScaleType.CENTER_CROP);
}
else
{
imageview[position]=(ImageView) convertView;
}
imageview[position].setImageResource(mThumbIds[position]);
return imageview[position];
}
private int[] mThumbIds={//显示的图片数组
R.drawable.grey,R.drawable.grey,R.drawable.grey,R.drawable.grey,
R.drawable.grey,R.drawable.grey,R.drawable.grey,R.drawable.grey,
R.drawable.grey,R.drawable.grey,R.drawable.grey,R.drawable.grey,
R.drawable.grey,R.drawable.grey,R.drawable.grey,R.drawable.grey
};
} 展开
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
public class Colorful_Ball extends Activity
{
private GridView gridview;
private TextView textview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridview = (GridView)findViewById(R.id.gridView1);
textview = (TextView)findViewById(R.id.textView1);
gridview.setNumColumns(4);
final MyGridViewAdapter adapter=new MyGridViewAdapter(this);
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new GridView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
// TODO Auto-generated method stub
adapter.imageview[position].setImageResource(R.drawable.orange);
textview.setText(Integer.toString(position));
}
});
}
}
package shannil.Colorful_Ball;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.content.Context;
import android.widget.ImageView;
import android.widget.GridView;
public class MyGridViewAdapter extends BaseAdapter
{
private Context mContext;
public ImageView[] imageview = new ImageView[16];
public MyGridViewAdapter (Context c)
{
mContext=c;
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return mThumbIds.length;
}
@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return mThumbIds[position];
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return position;
//return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
if(convertView==null)
{
imageview[position]=new ImageView(mContext);
imageview[position].setLayoutParams(new GridView.LayoutParams(70, 70));
imageview[position].setScaleType(ImageView.ScaleType.CENTER_CROP);
}
else
{
imageview[position]=(ImageView) convertView;
}
imageview[position].setImageResource(mThumbIds[position]);
return imageview[position];
}
private int[] mThumbIds={//显示的图片数组
R.drawable.grey,R.drawable.grey,R.drawable.grey,R.drawable.grey,
R.drawable.grey,R.drawable.grey,R.drawable.grey,R.drawable.grey,
R.drawable.grey,R.drawable.grey,R.drawable.grey,R.drawable.grey,
R.drawable.grey,R.drawable.grey,R.drawable.grey,R.drawable.grey
};
} 展开
4个回答
展开全部
你这里只有16个元素,一屏就显示完了,敢不敢多整点,你会发现新问题,一团糟,呵呵~~~
更多追问追答
追问
不是你说的这个问题~你都不知道我要干的是什么
追答
知道你什么意思,想要个GridView,点一下某项,就换个图片嘛。
你把
if(convertView==null)
**********************
imageview[position].setImageResource(mThumbIds[position]);
这一段换成这个:
if(imageview[position] == null)
{
imageview[position]=new ImageView(mContext);
imageview[position].setLayoutParams(new GridView.LayoutParams(70, 70));
imageview[position].setScaleType(ImageView.ScaleType.CENTER_CROP);
imageview[position].setImageResource(mThumbIds[position]);
}
试试吧老弟
还有,你别光叫唤,你试试用你的代码,多加点元素,让他超过一个屏幕试试,看看你的会有什么问题再说!
展开全部
convertView 不要写if else 每次都new一个新的.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把if(convertView==null)
去掉
去掉
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
好像看出来什么问题,不知道你解决了么?能再说详细点么?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询