Java 滚动时,ListView项会发生更改

Java 滚动时,ListView项会发生更改,java,android,Java,Android,我正在填写一个列表视图,下面是代码: ArrayAdapter<CharSequence> listAdapter1; ListView toc; toc=(ListView)findViewById(R.id.listView1); listAdapter1=ArrayAdapter.createFromResource(Glossary.this,R.array.arabicalphabet,R.layout.glossarysimplerow); toc.setAdapter(

我正在填写一个列表视图,下面是代码:

ArrayAdapter<CharSequence> listAdapter1;
ListView toc;
toc=(ListView)findViewById(R.id.listView1);
listAdapter1=ArrayAdapter.createFromResource(Glossary.this,R.array.arabicalphabet,R.layout.glossarysimplerow);
toc.setAdapter(listAdapter1);
ArrayAdapter列表适配器1;
列表视图目录;
toc=(ListView)findViewById(R.id.listView1);
listAdapter1=ArrayAdapter.createFromResource(Glossary.this,R.array.arabicalphabet,R.layout.glossarysimplerow);
目录设置适配器(列表适配器1);
我在xml文件中设置了一个列表选择器,当单击列表中的任何项目时,其颜色将变为蓝色,以显示它已被选中。问题是,当我滚动listview时,选择会改变,例如,如果我将列表从a填充到Z,如果我选择字母“B”并滚动多次,我会选择字母“M”。。。
有什么帮助吗?

是的,我一周前遇到了这个问题,原因是“当您滚动listview时,第一个显示项目的位置为0”,因此您的更改视图将影响新项目

我的解决办法是:

1) 。使用
ScrollView
并在其内部使用
LinearLayout
,而不是使用
ListView

2) 。将数组
TextView[]
添加到
LinearLayout

代码:

布局:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp">
    <LinearLayout
        android:id="@+id/ll_inflater"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </LinearLayout>
</ScrollView>

爪哇:

intpreps=-1;
int sdk=android.os.Build.VERSION.sdk\u int;
LinearLayout ll_充气机=(LinearLayout)findViewById(R.id.ll_充气机);
TextView[]tv_Subs=新的TextView[array.size()]//arrary是arrayData
对于(int i=0;i
在Android中,
AdapterView
的子项(如
ListView
)启用了缓存,因此当用户滚动时,屏幕上的项目会从内存中消失,
getView()
会为滚动后显示在视图上的项目调用

getView()
方法中传递的
视图
被重用,并且具有以前在
列表视图中可见的
视图
的属性。这提高了
列表视图的性能
,但需要检查所有可能的条件,并重置在
getView()
中接收到的视图的所有属性

现在,您使用的通用
ArrayAdapter
没有在单击时更改颜色的实现,也没有任何方法来保存已单击视图的记录。所以滚动的结果是出乎意料的

您可以实现自己的
适配器
类,如下所示,它将解决您的问题:

public class MyAdapter extends ArrayAdapter<String> {
    Context context;
    List<String> lstStrings;
    List<Boolean> isClicked;

    public MyAdapter (Context context, int resource, List<String> lstStrings) {
        super(context, resource, lstStrings);
        this.context = context;
        lstStrings = lstStrings;
        isClicked = new ArrayList<>(Arrays.asList(new Boolean[lstStrings.length]));
        Collections.fill(isClicked, new Boolean(false));
    }   

    @Override
    public int getCount() {
        return lstStrings.size();
    }

    @Override
    public String getItem(int position) {
        return lstStrings.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = LayoutInflater.from( context).inflate(R.layout.list_item, null);

        TextView txtTheText = (TextView) convertView.findViewById(R.id.txtTheText);

        txtTheText .setText(lstStrings.get(position));

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isClicked.set(!isClicked.get(position));
            }
        });

        if (isClicked.get(position))
            convertView.setBackgroundColor(Color.argb(255, 255, 0, 0));
        else
            convertView.setBackgroundColor(Color.argb(255, 0, 0, 255));

        return convertView;
    }
}
公共类MyAdapter扩展了ArrayAdapter{
语境;
列表字符串;
点击列表;
公共MyAdapter(上下文上下文、int资源、列表字符串){
super(上下文、资源、字符串);
this.context=上下文;
lstStrings=lstStrings;
isClicked=newarraylist(Arrays.asList(new Boolean[lstStrings.length]);
Collections.fill(isClicked,new Boolean(false));
}   
@凌驾
public int getCount(){
返回lstStrings.size();
}
@凌驾
公共字符串getItem(int位置){
返回lstStrings.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回0;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
if(convertView==null)
convertView=LayoutFlater.from(上下文)。充气(R.layout.list_项,空);
TextView txtText=(TextView)convertView.findViewById(R.id.txtText);
setText(lstStrings.get(position));
convertView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
isClicked.set(!isClicked.get(位置));
}
});
如果(单击。获取(位置))
setBackgroundColor(Color.argb(255,255,0,0));
其他的
setBackgroundColor(Color.argb(255,0,0255));
返回视图;
}
}

我有一个解决方案。单击一个项目时,记录其
位置
。然后覆盖
onConfigurationChanged(Configuration newConfig)
,并调用
ListView.setSelection(position)

添加适配器代码。您应该使用ViewHolder模式,或者确保您已经定义了if(something)->change color为blue和else->change back为您想要的默认颜色。请发布完整的适配器代码。。如果没有,请使用BaseAdpater with viewholder模式。我认为这是由于视图的循环,为每个项目状态维护一个布尔值,即它是否已被选中。没有java适配器代码,只有我发布的代码。请查看此想法:问题是我必须使用listview,有办法解决吗?我没有别的办法。很抱歉,没什么,只是打字错误,修好了。
public class MyAdapter extends ArrayAdapter<String> {
    Context context;
    List<String> lstStrings;
    List<Boolean> isClicked;

    public MyAdapter (Context context, int resource, List<String> lstStrings) {
        super(context, resource, lstStrings);
        this.context = context;
        lstStrings = lstStrings;
        isClicked = new ArrayList<>(Arrays.asList(new Boolean[lstStrings.length]));
        Collections.fill(isClicked, new Boolean(false));
    }   

    @Override
    public int getCount() {
        return lstStrings.size();
    }

    @Override
    public String getItem(int position) {
        return lstStrings.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = LayoutInflater.from( context).inflate(R.layout.list_item, null);

        TextView txtTheText = (TextView) convertView.findViewById(R.id.txtTheText);

        txtTheText .setText(lstStrings.get(position));

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isClicked.set(!isClicked.get(position));
            }
        });

        if (isClicked.get(position))
            convertView.setBackgroundColor(Color.argb(255, 255, 0, 0));
        else
            convertView.setBackgroundColor(Color.argb(255, 0, 0, 255));

        return convertView;
    }
}