Java 列表视图项所选背景已更改,其他项也会更改
我是列表视图中的新手,我想点击列表视图中的项目,它会改变背景颜色,但在滚动之后,它也会改变其他项目的背景视图。我不知道我错在哪里 代码段: ListColorChange.javaJava 列表视图项所选背景已更改,其他项也会更改,java,android,listview,Java,Android,Listview,我是列表视图中的新手,我想点击列表视图中的项目,它会改变背景颜色,但在滚动之后,它也会改变其他项目的背景视图。我不知道我错在哪里 代码段: ListColorChange.java package com.example.spinnerfilter; public class ListColorChange extends Activity { private ListView phproductinfo; ArrayList<ProductInfo> arrayLis
package com.example.spinnerfilter;
public class ListColorChange extends Activity {
private ListView phproductinfo;
ArrayList<ProductInfo> arrayListProf;
ChangeColorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
init();
}
int mselected = -1;
public void init() {
phproductinfo = (ListView) findViewById(R.id.phlist);
arraylistInit();
adapter = new ChangeColorAdapter(ListColorChange.this, arrayListProf);
phproductinfo.setAdapter(adapter);
phproductinfo.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
mselected = position;
ProductInfo info = arrayListProf.get(position);
info.setOngoing(true);
adapter.notifyDataSetChanged();
}
});
}
private void arraylistInit() {
String name[] = { "India", "Austrai", "Xyxz", "Pask", "New", "India",
"Austrai", "Xyxz", "Pask", "New", "India", "Austrai", "Xyxz",
"Pask", "New", "India", "Austrai", "Xyxz", "Pask", "New",
"India", "India", "Austrai", "Xyxz", "Pask", "New", "India",
"India", "Austrai", "Xyxz", "Pask", "New", "India", "India",
"Austrai", "Xyxz", "Pask", "New", "India" };
arrayListProf = new ArrayList<ProductInfo>();
for (int i = 0; i < name.length; i++) {
ProductInfo info = new ProductInfo();
info.setDisplay_name(name[i]);
info.setOngoing(false);
arrayListProf.add(info);
}
}
public class ChangeColorAdapter extends BaseAdapter
{
public ChangeColorAdapter(ListColorChange listColorChange,
ArrayList<ProductInfo> arrayListProf) {
// TODO Auto-generated constructor stub
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arrayListProf.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arrayListProf.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
ViewHolder vh;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater
.from(ListColorChange.this);
convertView = inflater.inflate(R.layout.productlist, null);
TextView product_name = (TextView) convertView
.findViewById(R.id.prod_name);
vh = new ViewHolder(convertView);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
if (arrayListProf != null && arrayListProf.size() > 0) {
ProductInfo pi = arrayListProf.get(position);
if (pi.getDisplay_name() != null) {
vh.product_name.setText(pi.getDisplay_name());
}
if (arrayListProf.get(position).isOngoing() && mselected == position) {
convertView.setBackgroundColor(Color.parseColor("#ff0000"));
}
}
return convertView;
}
class ViewHolder {
TextView product_name;
TextView qty;
TextView unit;
int position;
boolean ispu = false;
public ViewHolder(View view) {
// TODO Auto-generated constructor stub
product_name = (TextView) view.findViewById(R.id.prod_name);
qty = (TextView) view.findViewById(R.id.qty);
unit = (TextView) view.findViewById(R.id.unit);
}
}
}
}
package com.example.spinnerfilter;
公共类ListColorChange扩展活动{
私有ListView phproductinfo;
ArrayList ArrayListrof;
变色适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
init();
}
int mselected=-1;
公共void init(){
phproductinfo=(ListView)findViewById(R.id.phlist);
arraylistInit();
adapter=新的ChangeColorAdapter(ListColorChange.this,ArrayListrof);
phproductinfo.setAdapter(适配器);
phproductinfo.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共链接(AdapterView arg0、视图arg1、,
整数位置,长arg3){
//TODO自动生成的方法存根
mselected=位置;
ProductInfo=arrayListProf.get(位置);
正在进行的信息设置(正确);
adapter.notifyDataSetChanged();
}
});
}
私有无效arraylistInit(){
字符串名称[]={“印度”、“澳大利亚”、“Xyxz”、“Pask”、“新”、“印度”,
“Austrai”、“Xyxz”、“Pask”、“New”、“India”、“Austrai”、“Xyxz”,
“帕斯克”、“新”、“印度”、“澳大利亚”、“Xyxz”、“帕斯克”、“新”,
“印度”、“印度”、“澳大利亚”、“Xyxz”、“帕斯克”、“新”、“印度”,
“印度”、“澳大利亚”、“Xyxz”、“帕斯克”、“新”、“印度”、“印度”,
“澳大利亚”、“Xyxz”、“帕斯克”、“新”、“印度”};
arrayListProf=新的ArrayList();
for(int i=0;i0){
ProductInfo pi=arrayListProf.get(位置);
if(pi.getDisplay_name()!=null){
vh.product_name.setText(pi.getDisplay_name());
}
if(arrayListProf.get(position).isongoong()&&mselected==position){
convertView.setBackgroundColor(Color.parseColor(#ff0000”);
}
}
返回视图;
}
类视图持有者{
TextView产品名称;
文本视图数量;
文本视图单元;
内部位置;
布尔值ispu=false;
公共视图持有者(视图){
//TODO自动生成的构造函数存根
product\u name=(TextView)view.findViewById(R.id.prod\u name);
数量=(TextView)view.findViewById(R.id.qty);
unit=(TextView)view.findViewById(R.id.unit);
}
}
}
}
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/phlist"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
更新代码
if (arrayListProf.get(position).isOngoing() && mselected ==position) {
convertView.setBackgroundColor(Color.parseColor("#ff0000"));
convertView.setTag(vh);
}
这是由于在
ListView
上滚动后,单击的位置与滚动后的位置不同而导致的问题。您可以通过允许对象记住是否由Onclick
listnere选中,轻松解决此问题
假设这是您拥有的类,其类型为列表
已提供给适配器
class ProductInfo{
// here you already have your instance variables
boolean isSelected;
}
当您在ProductInfo
项上单击Onclick
时,将相应ProductInfo
的此实例变量设置为true
,并更改适配器中相应项的背景,您必须从阵列适配器
或BaseAdapter
进行扩展
在ListView的
适配器中填充列表时,可以检查isSelected
是否为真,然后选择相应项目的背景。否则不要
通过这样做,您将独立于具有相应滚动条的项目位置。因此,您的ProductInfo
将保留实际选定的背景更改
更新
我仍然是对的,所以我写了一段代码片段,其中的输出可以检查,
我尽可能地保持代码的简单性,并在单个文件中编写代码以简化操作,而且代码也可以在上获得,您可以在上正确检查代码
public class CustomListViewActivity extends Activity {
private ListView listView;
private ArrayList<ProductInfo> productInfos;
private ArrayAdapter<ProductInfo> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_list_view2);
initializeUI();
}
private void initializeUI() {
listView = (ListView)findViewById(R.id.CustomListViewActivity_listView_two);
productInfos = new ArrayList<>();
for (int i = 0; i < 50; i++) {
ProductInfo productInfo = new ProductInfo();
productInfo.setText("Product_1_"+i);
productInfos.add(productInfo);
}
adapter = new MyAdapter(getApplicationContext(), R.layout.single_item_custom_one, productInfos);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ProductInfo productInfo = (ProductInfo) listView.getItemAtPosition(position);
productInfo.setSelected(true);
adapter.notifyDataSetChanged();
}
});
}
private class MyAdapter extends ArrayAdapter {
private ArrayList<ProductInfo> a_productInfos;
private Context a_context;
private LayoutInflater a_layoutInflater;
public MyAdapter(Context context, int resource, ArrayList<ProductInfo> a_productInfos) {
super(context, resource, a_productInfos);
this.a_productInfos = a_productInfos;
this.a_context = context;
a_layoutInflater = LayoutInflater.from(this.a_context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
row = a_layoutInflater.inflate(R.layout.single_item_custom_one, parent, false);
holder = new ViewHolder();
holder.product_name = (TextView) row.findViewById(R.id.single_item_custom_one_textView);
holder.item_LinearLayout = (LinearLayout) row.findViewById(R.id.single_item_custom_one_linearLayout);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
final ProductInfo productInfo = a_productInfos.get(position);
holder.product_name.setText(""+productInfo.getText());
if (productInfo.isSelected) {
holder.item_LinearLayout.setBackgroundColor(Color.parseColor("#ff44ff"));
}else {
holder.item_LinearLayout.setBackgroundColor(Color.parseColor("#ffffff"));
}
return row;
}
class ViewHolder {
TextView product_name;
LinearLayout item_LinearLayout;
}
@Override
public int getCount() {
return super.getCount();
}
}
private class ProductInfo {
private String text;
private boolean isSelected;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
}
}
公共类CustomListViewActivity扩展活动{
私有列表视图列表视图;
私有ArrayList产品信息;
专用阵列适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u custom\u list\u view2);
初始化
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/CustomListViewActivity_listView_two"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/single_item_custom_one_linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/single_item_custom_one_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
android:paddingRight="?android:attr/listPreferredItemPaddingRight"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#000" />
</LinearLayout>