ImageView的列表视图中无法在Android的运行时改变 [英] Imageview in Listview cannot change at runtime in android

查看:98
本文介绍了ImageView的列表视图中无法在Android的运行时改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Android应用程序。其中我有文字和图片列表视图。我需要设置的ImageView在运行时可见。 IE浏览器。当我点击的观点应该出现,当我点击下一个视图previous观点已经消失,在点击查看对应的ImageView有出现。

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.

现在我在显示文本的成功,但我在ImageView的外观strucking。项目列表的onclick里面我不能设置ImageView的资源

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

下面是我的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;

}

    }

请人给我一个更好的solution..its非常紧迫的..谢谢提前

Please anyone give me a better solution..its very urgent.. Thanks in advance

推荐答案

创建了ImageView的最后指针。

Create a final pointer for the imageView.

final ImageView tempView = holder.arrow;

比在onclick

than in the onclick:

tempView.setImageResource(R.drawable.arrow);

添加到类的字段:

Added to the class a field:

MyListAdapter myAdapter;

在onCreate方法做到这一点:

In the oncreate method do this:

myAdapter = new MyListAdapter(this);
listview.setAdapter(myAdapter);

在onclick事件做到这一点:

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

     }    
 });   

这应该工作。

这篇关于ImageView的列表视图中无法在Android的运行时改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆