Listview中的Imageview在运行时无法在android中更改

时间:2011-11-02 10:15:33

标签: android android-listview

我正在开发Android应用程序。其中我有文本和图像的列表视图。我需要在运行时设置imageview。即。当我点击它应该出现的视图时,当我点击下一个视图时,前一个视图必须消失,并且必须出现点击视图中的相应图像视图。

现在我在显示文字方面取得了成功,但我在使用imageview时看起来很震撼。在项目列表的onclick中,我无法为imageview设置资源

以下是我的代码。

    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;

}

    }

请有人给我一个更好的解决方案..非常紧急..提前致谢

5 个答案:

答案 0 :(得分:1)

为imageView创建一个最终指针。

final ImageView tempView = holder.arrow;

比onclick:

tempView.setImageResource(R.drawable.arrow);

在课程中添加了一个字段:

MyListAdapter myAdapter;

在oncreate方法中执行以下操作:

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

在onclick事件中执行此操作:

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

     }    
 });   

这应该有效。

答案 1 :(得分:1)

试试这个:

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

     }
 });

答案 2 :(得分:0)

据我所知,

我认为不是使用view onClickListener,而是使用layout onClickListener中使用的R.layout.mycategorylist

或者,您可以使用holder.categoryname onClickListenerholder.categorycount listeners权利。

并且您可以声明ViewHolder holder ; as global variable in your MyListAdapter正确,然后声明final或其他内容没有问题。

希望有所帮助

答案 3 :(得分:0)

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

    }

答案 4 :(得分:0)

     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;

}