-1

我有一个带有一些 TextViews 和 EditTexts 的自定义 ListView,我想防止回收器在项目滚动出视图时删除数据。我查看了其他帖子,但我还没有找到解决方案。请帮忙。

这是我的代码。

Java :


import android.text.Editable;
import android.view.LayoutInflater
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;



public class CustomAdapter extends BaseAdapter {
    private int[] fif={0,0,0,0,0,0},tw={0,0,0,0,0,0},scorePerm={0,0,0,0,0,0},scorePerm2=       {0,0,0,0,0,0};
    private int scoreTemp=0;`






private static final String TAG = CustomAdapter.class.getSimpleName();
ArrayList<DataModel> listArray;

public CustomAdapter() {
    listArray = new ArrayList<DataModel>(5);
    listArray.add(new DataModel("Player 1", 1 ));
    listArray.add(new DataModel("Player 2", 2));
    listArray.add(new DataModel("Player 3", 3));
    listArray.add(new DataModel("Player 4", 4));
    listArray.add(new DataModel("Player 5", 5));
    listArray.add(new DataModel("Player 6", 6));

}

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

@Override
public Object getItem(int i) {
    return listArray.get(i);    
}

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

public int getViewTypeCount() {                 

    return getCount();
}

@Override
public int getItemViewType(int position) {

    return position;
}

@Override
public View getView(int index, View view, final ViewGroup parent) {

   if (view == null) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
       view = inflater.inflate(R.layout.list, parent,false);

    }



    view.setClickable(true);
    view.setFocusable(true);
    view.setBackgroundResource(android.R.drawable.menuitem_background);

    final DataModel dataModel = listArray.get(index);

    TextView textView = (TextView) view.findViewById(R.id.player_un);
    textView.setText(dataModel.getName());

    Button button0 = (Button) view.findViewById(R.id.plus50);

    Button button1 = (Button) view.findViewById(R.id.minus50);

    Button button2 = (Button) view.findViewById(R.id.plus20);

    Button button3 = (Button) view.findViewById(R.id.minus20);

    Button ok = (Button) view.findViewById(R.id.un_ad_ok);

    final Button reset = (Button) view.findViewById(R.id.un_ad_res);

    final TextView score = (TextView) view.findViewById(R.id.score);
    score.setText(R.string.scorepoints);

    final EditText fifty = (EditText) view.findViewById(R.id.score50);
    fifty.setText(R.string.scorepoints);

    final EditText two = (EditText) view.findViewById(R.id.score20);
    two.setText(R.string.scorepoints);

    final EditText other = (EditText) view.findViewById(R.id.scoreplus);
    other.setText(R.string.scorepoints);



    //other code 



    return view;
}
}


ListView XML :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Uno" >

 <EditText
     android:id="@+id/scoreplus"
     android:layout_width="50dp"
     android:layout_height="40dp"
     android:layout_alignLeft="@+id/plus20"
     android:layout_alignParentRight="true"
     android:layout_alignParentTop="true"
     android:descendantFocusability="beforeDescendants"
     android:hint="@string/scorepoints"
     android:inputType="number" />

 <Button
     android:id="@+id/minus20"
     style="?android:attr/buttonStyleSmall"
     android:layout_width="30dp"
     android:layout_height="40dp"
     android:layout_alignParentRight="true"
     android:layout_below="@+id/scoreplus"
     android:text="@string/minus" />

 <EditText
     android:id="@+id/score20"
     android:layout_width="40dp"
     android:layout_height="40dp"
     android:layout_alignBaseline="@+id/minus20"
     android:layout_alignBottom="@+id/minus20"
     android:layout_toLeftOf="@+id/minus20"
     android:inputType="number"
     android:text="@string/scorepoints"
     android:descendantFocusability="beforeDescendants"
     android:textSize="12sp" />

 <Button
     android:id="@+id/plus20"
     style="?android:attr/buttonStyleSmall"
     android:layout_width="30dp"
     android:layout_height="40dp"
     android:layout_alignTop="@+id/score20"
     android:layout_toLeftOf="@+id/score20"
     android:text="@string/plus" />

 <ImageView
     android:id="@+id/Image20"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignBottom="@+id/score20"
     android:layout_toLeftOf="@+id/plus20"
     android:contentDescription="@string/player"
     android:src="@drawable/ic_launcher" />

 <Button
     android:id="@+id/minus50"
     style="?android:attr/buttonStyleSmall"
     android:layout_width="30dp"
     android:layout_height="40dp"
     android:layout_alignTop="@+id/score20"
     android:layout_toRightOf="@+id/score50"
     android:text="@string/minus" />

 <EditText
     android:id="@+id/score50"
     android:layout_width="40dp"
     android:layout_height="40dp"
     android:layout_alignBaseline="@+id/minus50"
     android:layout_alignBottom="@+id/minus50"
     android:layout_toRightOf="@+id/plus50"
     android:inputType="number"
     android:text="@string/scorepoints"
     android:descendantFocusability="beforeDescendants"
     android:textSize="12sp" />

 <Button
     android:id="@+id/plus50"
     style="?android:attr/buttonStyleSmall"
     android:layout_width="30dp"
     android:layout_height="40dp"
     android:layout_alignTop="@+id/score50"
     android:layout_toRightOf="@+id/Image50"
     android:text="@string/plus" />

 <ImageView
     android:id="@+id/Image50"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignBottom="@+id/plus50"
     android:layout_alignParentLeft="true"
     android:contentDescription="@string/player"
     android:src="@drawable/ic_launcher" />

 <TextView
     android:id="@+id/score"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignBaseline="@+id/scoreplus"
     android:layout_alignBottom="@+id/scoreplus"
     android:layout_toRightOf="@+id/score50"
     android:text="@string/scorepoints"
     android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
     android:id="@+id/player_un"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignBaseline="@+id/score"
     android:layout_alignBottom="@+id/score"
     android:layout_alignParentLeft="true"
     android:text="@string/player"
     android:textAppearance="?android:attr/textAppearanceLarge" />

 <Button
     android:id="@+id/un_ad_ok"
     style="?android:attr/buttonStyleSmall"
     android:layout_width="wrap_content"
     android:layout_height="40dp"
     android:layout_alignParentLeft="true"
     android:layout_alignRight="@+id/minus50"
     android:layout_below="@+id/score50"
     android:text="@string/ok" />

 <Button
     android:id="@+id/un_ad_res"
     style="?android:attr/buttonStyleSmall"
     android:layout_width="50dp"
     android:layout_height="40dp"
     android:layout_alignLeft="@+id/Image20"
     android:layout_alignParentRight="true"
     android:layout_below="@+id/minus20"
     android:enabled="false"
     android:text="@string/cancel" />

4

1 回答 1

2

您没有按照应有的方式实现列表视图的适配器。

请观看“ listView 的世界”,或者阅读这篇文章以了解如何处理 viewHolders。

如果您希望将文本保存在 editText 中,您应该在文本更改后立即更新数据,或者在用户触摸 listView 时更新数据(这有点困难)。

你得到这个“错误”的原因是 listView 重新使用在它之外滚动的旧视图,所以你需要将丢失的数据存储在某个地方,并始终将视图更新为正确的数据。

于 2013-07-14T21:07:48.190 回答