我正在开发Android应用程序。其中我有列表视图与文字和图像。我需要在运行时将imageview设置为可见。即。当我点击视图时,它应该出现,当我点击下一个视图之前的视图必须消失,并且点击视图中的相应图像视图必须出现。Listview中的Imageview无法在运行时更改android
现在,我成功地显示文字,但我在图像视图的外观屡次。在项目列表的onclick内我无法设置图像视图的资源
以下是我的代码。
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非常紧迫的..谢谢提前
感谢您的答复。虽然使用此代码最终问题已解决,但当我点击另一个列表项时,前一个可见的图像也可见。 – deepa
您需要调用adapter.NotifiyDataSetChange()才能自行刷新数据。 – Raz
我必须使用adapter.NotifiyDataSetChange()。因为我没有拿适配器的对象。当我重写notifiyDatasetChange它保持相同.. – deepa