后ListView项点击将图片添加到列表视图 [英] Adding image to listview after listview item click
问题描述
我正在开发具有1的TextView其中显示内容,一个ImageView的一个ListView的应用程序。我要的是,当我点击我要设置与我有刻度线图像的ImageView ListView控件。其工作罚款。当我点击列表视图,刻度线图像加载在其上我点击的ListView项。
当我滚动时,问题就出现了。当我滚动,我可以看到一些其他的列表视图项楼下有一个刻度标记加载。不知道图片是如何在那个位置设置。
我看过的地方,当在ListView滚动,刷新视图。是造成问题的原因?
谁能帮怎么能iresolve呢?我想显示图像(加载)在其上我点击和ListView项现在其他ListView项。
下面是XML声明的ListView项目
method_item.xml
<?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直> <的TextView
机器人:ID =@ + ID / textView1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentLeft =真
机器人:layout_alignParentTop =真
机器人:textAppearance =机器人:ATTR / textAppearanceMedium
机器人:文字颜色=@机器人:彩色/白
机器人:文字=这是一个复选框按钮,在反应了用户点击我的大串测试它并检查它的外观检查/> < ImageView的
机器人:ID =@ + ID / imageView1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / textView1
机器人:layout_centerHorizontal =真/> < ImageView的
机器人:ID =@ + ID / imageView2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignBottom =@ + ID / imageView1
机器人:layout_alignParentRight =真
机器人:layout_marginRight =34dp/>< / RelativeLayout的>
下面是类片段的一部分:
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.method_list_main); 最终的ListView列表=(ListView控件)findViewById(R.id.listView1); 适配器=新MethodLazyAdapter(this,ARRAY.$p$pparationSteps,ARRAY.timeTo$p$ppare,ARRAY.arrowValue,noOfSteps,list);
list.setAdapter(适配器); list.setOnItemClickListener(新OnItemClickListener(){ @覆盖
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,
长ID){
// TODO自动生成方法存根
RelativeLayout的LL =(RelativeLayout的)视图。
ImageView的图像=(ImageView的)ll.findViewById(R.id.imageView2); 如果(打勾[位置]){
勾选[位置] = FALSE;
image.setImageResource(0);
}
其他{
勾选[位置] =真;
image.setImageResource(R.drawable.select_right);
} 的System.out.println(位置是:+位置);
}
});
}
最初全部打勾[I]值设置为false。
下面是适配器类的getView功能
公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
查看VI = convertView;
如果(convertView == NULL)
VI = inflater.inflate(R.layout.method_item,NULL); TextView的文本=(TextView的)vi.findViewById(R.id.textView1);
text.setText(preparationSteps \\ [位置\\]);
返回VI;
}
试试这个得到你想要的东西。
在公共区域中添加这个变量
公共SparseBooleanArray检查=新SparseBooleanArray();
然后 onItemClick
list.setOnItemClickListener(新OnItemClickListener(){ @覆盖
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,
长ID){
布尔STAT = checked.get(位置,FALSE);
checked.put(位置,STAT!);
adapter.setChecked();
的System.out.println(位置是:+位置);
}
});
和适配器类
如果在activiy电源适配器没有子类公共区域添加变量
私人SparseBooleanArray检查=新SparseBooleanArray();
和此方法添加到类
公共无效setChecked(SparseBooleanArray CH){
检查= CH;
notifyDataSetChanged();
}
的或者,如果它的子类中使用我们的 cheked
变量,我们定义了它的
然后 getView
法
公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
查看VI = convertView;
如果(convertView == NULL)
VI = inflater.inflate(R.layout.method_item,NULL); TextView的文本=(TextView的)vi.findViewById(R.id.textView1);
text.setText(preparationSteps \\ [位置\\]); ImageView的图像=(ImageView的)vi.findViewById(R.id.imageView2);
如果(checked.get(位置,FALSE)){
image.setImageResource(0);
}
其他{
image.setImageResource(R.drawable.select_right);
}
返回VI;
}
请让我知道如果这个帮你。
I am developing an app which has a listView having 1 textView which displays the content and one imageView. What i want is, when i click the listView i want to set the imageView with the tick mark image which i have. Its working fine. Once i click the listView, the tick mark image is loaded on that listview item on which i clicked.
The problem arises when i scroll. When i scroll, i could see some other listview item down below has a tick mark loaded. not sure how the image was set on that position.
I have read somewhere that when the listView is scrolled, the view is refreshed. Is that causing the problem?
Can anyone help how can iresolve this? I want the image to be shown(loaded) on the listView item on which i clicked and now other listView item.
Below is the xml stating listView items
method_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/white"
android:text="This is a check box button which reacts upon the check that user clicks. I am testing it with the big string and checking how it looks" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignParentRight="true"
android:layout_marginRight="34dp" />
</RelativeLayout>
Below is the part of class snippet:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.method_list_main);
final ListView list = (ListView)findViewById(R.id.listView1);
adapter=new MethodLazyAdapter(this,ARRAY.preparationSteps,ARRAY.timeToPrepare,ARRAY.arrowValue,noOfSteps,list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
RelativeLayout ll = (RelativeLayout) view;
ImageView image = (ImageView)ll.findViewById(R.id.imageView2);
if(tick[position]){
tick[position] = false;
image.setImageResource(0);
}
else{
tick[position] = true;
image.setImageResource(R.drawable.select_right);
}
System.out.println("Position is: "+position);
}
});
}
Initially all tick[i] value is set to false.
Below is the getView function of adapter class
public View getView(final int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.method_item, null);
TextView text = (TextView)vi.findViewById(R.id.textView1);
text.setText(preparationSteps\[position\]);
return vi;
}
Try this to get what you want
in public area add this variable
public SparseBooleanArray checked = new SparseBooleanArray();
then in onItemClick
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
boolean stat = checked.get(position, false);
checked.put(position, !stat);
adapter.setChecked();
System.out.println("Position is: "+position);
}
});
and in Adapter Class
If adapter not sub class in activiy add variable in public area
private SparseBooleanArray checked = new SparseBooleanArray();
and add this method to class
public void setChecked(SparseBooleanArray ch){
checked = ch;
notifyDataSetChanged();
}
Or if it sub class use our cheked
variable we defined it up
then in getView
method
public View getView(final int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.method_item, null);
TextView text = (TextView)vi.findViewById(R.id.textView1);
text.setText(preparationSteps\[position\]);
ImageView image = (ImageView)vi.findViewById(R.id.imageView2);
if(checked.get(position, false)){
image.setImageResource(0);
}
else{
image.setImageResource(R.drawable.select_right);
}
return vi;
}
Please let me know if this help you.
这篇关于后ListView项点击将图片添加到列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!