java - Android baseAdapter 在向上滚动时停止评估 if 语句

标签 java android listview baseadapter

我有一个 ListView ,其中包含一个 baseadapter,它以 Json 格式从数据库中获取数据。所有这些都有效我的问题是我有一个蓝色背景的 ImageButton 但是如果 IF 语句中的某些条件评估为 true 那么它会将 ImageButton 背景更改为浅橙色。这在我向下滚动 ListView 时有效,但是当我向上滚动 ListView 时,一堆不符合要求的其他行将图像变为橙色。我不知道这是怎么发生的这是我的代码

public class LocalFeed_CustomView extends BaseAdapter {

    JSONObject names;
    Context ctx;
    LayoutInflater myiflater;
Activity m;
    ImageButton reputation_add;




    public LocalFeed_CustomView(JSONObject arr,Context c) {
        ctx = c;
        names = arr;
    }


    @Override
    public int getCount() {
        try {
            JSONArray jaLocalstreams = names.getJSONArray("localstreams");
            return jaLocalstreams.length();
        } catch (Exception e) {
            Toast.makeText(ctx,"Error: Please try again",Toast.LENGTH_LONG).show();
            return names.length();
        }


    }




    @Override
    public Object getItem(int position) {
        return position;
    }

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

    @Override
    public View getView(int position,View convertView, ViewGroup parent) {
        try {
            if(convertView==null) {

                LayoutInflater li = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = li.inflate(R.layout.customadapter, null);
            }

             reputation_add= (ImageButton)convertView.findViewById(R.id.reputation_add);


            SharedPreferences myaccount= ctx.getSharedPreferences("userInfo", MODE_PRIVATE);
            // This gets user ID and matches it with Data_ID which come from the database
            int Profile_ID = myaccount.getInt("id",0);

                JSONArray jaLocalstreams = names.getJSONArray("localstreams");
                final JSONObject jsonObject = jaLocalstreams.getJSONObject(position);
                int Data_ID=   jsonObject.getInt("myid");

           // IF the statement is true then the ImageButton Background changes color
            if(Data_ID==Profile_ID)
            {
                reputation_add.setBackgroundResource((R.drawable.borders));
            }



            return convertView;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;


    }

}

正如您在上面看到的,只有当 Data_ID=Profile_ID 评估为 true 时,ImageButton 才应该更改背景颜色,并且在向下滚动 Listview 时再次完美地工作,但是当返回时,ImageButton 在行上变为橙色它不应该。这里有一些图片来澄清这一点。 First & Foremost 在橙色背景下方的图片上,复选标记所在的位置;是 ImageButton,如前所述,原始颜色为蓝色。 该图像的左侧是 Profile_ID,它是 32 然后,如果您按名称查找,那里有一个数字,它是 Data_ID,它是 32 因此图像背景变为橙色正如它假设的那样。

下一张图片是当我向上滚动并查看此行上的数字时,您可以看到 Profile_ID 是 32,但 Data_ID 是 0,但它仍然将背景变成橙色,这不应该发生,因为很明显 32== 0 为假。任何建议将不胜感激。

最佳答案

我认为这个问题是因为ListView的回收机制引起的。在 getView() 内部,每次你有一个 if 语句时,你还必须设置一个 else 语句来做相反的事情。否则,当一个项目被回收时(当你向上滚动时,很可能),条件已经发生并且背景已经改变。您正在使用具有交替背景的相同回收项目,如果条件不再成立,您永远不会将其设置回原始背景。应该是:

...
if (condition_holds) {
    reputation_add.setBackgroundResource((R.drawable.borders));
} else {
    reputation_add.setBackgroundResource((R.drawable.original_background));
}

此外,我看到您没有使用 ViewHolder,而是总是调用 findViewById()。这是不明智的,尤其是当您有大量 View 时。看我之前的一个帖子: https://stackoverflow.com/questions/32775508/issue-in-display-phone-contact-list-in-android-application-very-slow/32775803#32775803

关于java - Android baseAdapter 在向上滚动时停止评估 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32793157/

相关文章:

java - 仅显示 Spring 启动验证的默认消息 - MethodArgumentNotValidException

java - 奇怪的SQLException : No suitable driver found for jdbc:postgresql

java - LoopJ Android 异步 Http 客户端 - java.net.SocketTimeoutException

java - 带有 XML 数据库的 Android 歌曲歌词应用程序

java - Android 比较两个列表变量后列表中的值错误

java - 到底什么时候请求运行时许可?

android SearchView 小部件 : how to display the voice query text to the search text box?

java - TransformException多个dex文件

android - 没有从 CustomAdapter 调用 getView

android - 如何在 ListView 中卡住页眉和页脚