问题
I am developing Android Application. In which i am having list view with text and image. i need to set the imageview visible at run time. ie. when i clicked the view it should appear and when i click next view previous view have to disappear and corresponding imageview in clicking view have to appear.
Now i am success in displaying text but i am strucking in appearance of imageview. Inside the onclick of item list i cannot set the resource for imageview
Below is my code.
public class ListofCategory extends Activity{
ListView listview;
List<String> mycategoryId,mycategoryName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.listofcategory);
listview=(ListView)findViewById(R.id.category_listview);
mycategoryId=new ArrayList<String>();
mycategoryName=new ArrayList<String>();
getData();
listview.setAdapter(new MyListAdapter(this));
//setListAdapter(new MyListAdapter(this));
}
class MyListAdapter extends BaseAdapter{
private Context context;
MyListAdapter(Context context)
{
this.context=context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mycategoryId.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View view, ViewGroup viewgroup) {
ViewHolder holder = null;
LayoutInflater inflater = LayoutInflater.from(context);
if (view == null)
{
view = inflater.inflate(R.layout.mycategorylist, null);
holder = new ViewHolder();
holder.categoryname = (TextView) view.findViewById(R.id.title);
holder.categorycount = (TextView) view.findViewById(R.id.description);
holder.arrow=(ImageView)view.findViewById(R.id.arrow_imageview);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
}
holder.categoryname.setText(mycategoryName.get(position));
holder.categoryname.setTextSize(18.0f);
//holder.arrow.setImageResource(R.drawable.arrow);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
Problem occurs here.. This holder value ask me to change as final but here i cannot make holder to final
holder.arrow.setImageResource(R.drawable.arrow);
//finish();
}
});
return view;
}
}
class ViewHolder
{
TextView categoryname;
TextView categorycount;
ImageView arrow;
}
}
Please anyone give me a better solution..its very urgent.. Thanks in advance
回答1:
Create a final pointer for the imageView.
final ImageView tempView = holder.arrow;
than in the onclick:
tempView.setImageResource(R.drawable.arrow);
Added to the class a field:
MyListAdapter myAdapter;
In the oncreate method do this:
myAdapter = new MyListAdapter(this);
listview.setAdapter(myAdapter);
On the onclick event do this:
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
Problem occurs here.. This holder value ask me to change as final but here i cannot make holder to final
tempView.setImageResource(R.drawable.arrow);
myAdapter.notifyDataSetChange();
}
});
That should work.
回答2:
Try this:
ViewHolder tmpHolder = null;
if( view == null ) {
.
.
.
tmpHolder = new ViewHolder();
} else {
tmpHolder = (ViewHolder) view.getTag();
}
final ViewHolder holder = tmpHolder;
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
holder.arrow.setImageResource(R.drawable.arrow);
//finish();
}
});
回答3:
As far as I can understand,
I think instead of using view onClickListener use, the layout onClickListener which is used in R.layout.mycategorylist.
Or else, you can use holder.categoryname onClickListener or holder.categorycount listeners right.
and You can declare ViewHolder holder ; as global variable in your MyListAdapter right, then there is no issue for declaring final or something.
Hope it helps
回答4:
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
// TODO Auto-generated method stub
ImageView image;
RelativeLayout layout = (RelativeLayout) view;//Layout of mycategorylist
image = (ImageView) layout.getChildAt(0);//image is added first here.change accordingly
image.setImageDrawable(....imageId);
}
回答5:
public class ListofCategory extends Activity{
ListView listview;
List<String> mycategoryId,mycategoryName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.listofcategory);
listview=(ListView)findViewById(R.id.category_listview);
mycategoryId=new ArrayList<String>();
mycategoryName=new ArrayList<String>();
getData();
listview.setAdapter(new MyListAdapter(this));
//setListAdapter(new MyListAdapter(this));
}
class MyListAdapter extends BaseAdapter{
private Context context;
private ImageView previous;
MyListAdapter(Context context)
{
this.context=context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mycategoryId.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View view, ViewGroup viewgroup) {
ViewHolder holder = null;
LayoutInflater inflater = LayoutInflater.from(context);
if (view == null)
{
view = inflater.inflate(R.layout.mycategorylist, null);
holder = new ViewHolder();
holder.categoryname = (TextView) view.findViewById(R.id.title);
holder.categorycount = (TextView) view.findViewById(R.id.description);
holder.arrow=(ImageView)view.findViewById(R.id.arrow_imageview);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
}
holder.categoryname.setText(mycategoryName.get(position));
holder.categoryname.setTextSize(18.0f);
//holder.arrow.setImageResource(R.drawable.arrow);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
if(previous!=null){
previous.setVisibility(View.Invisible);
v.setVisibility(View.Visible);
previous = v;
}
}
});
return view;
}
}
class ViewHolder
{
TextView categoryname;
TextView categorycount;
ImageView arrow;
}
来源:https://stackoverflow.com/questions/7978689/imageview-in-listview-cannot-change-at-runtime-in-android