合并适配器某些语句在错误的时间运行

时间:2013-09-02 11:00:07

标签: android android-listview android-arrayadapter

我正在使用MergeAdapter来显示带有节标题的List。这个列表显示正常但有时候getView方法中的某些内容有时会被触发。

这是触发的内容。

 @Override
public View getView(int position, View convertView, ViewGroup parent)
{
   ...
    try {
        JSONObject thisRow = items.get(position);
        ...
        View.OnClickListener myListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getContext(), ID, Toast.LENGTH_SHORT).show();
            }
        };

        JSONObject alterations = thisRow.getJSONArray("alteration").getJSONObject(0);


        // This if statement gets trigger when it shouldn't
        if (!alterations.getString("text").equals("false")) {
            CharSequence currText = time.getText();
            time.setText(currText+" vs" +ID);
            Log.d("FUApp", "Alterations 'text' isn't false at "+ID);
            Button alter = (Button) centreView.findViewById(R.id.is_alt);
            String s = String.valueOf(position);
            alter.setText(s);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            alter.setLayoutParams(lp);
            alter.setOnClickListener(myListener);
        }
    ...
    return centreView;
}

我设置了一个标签以显示位置的值,这是我的应用程序的屏幕截图。

enter image description here

如您所见,按钮正在显示位置为43,按钮不应该在那里显示。

我完全不知道为什么会这样。任何人都可以帮我解决这个问题吗?

编辑:当位置(例如35)离开视图时,似乎是在进入屏幕的行上触发了if语句。仍不确定如何阻止它出现。

1 个答案:

答案 0 :(得分:0)

我删除了我的旧答案,因为它根本不好。给我带来了问题。

问题是当视图被回收时我没有将按钮重置为0dp

我添加了这些行

 Button alter = (Button) centreView.findViewById(R.id.is_alt);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 0);
        alter.setLayoutParams(lp);
        final JSONObject alterations = thisRow.getJSONArray("alteration").getJSONObject(0);
        if (!alterations.getString("text").equals("false")) {

它现在可以正常工作。