2017-03-08 48 views
1

我有一個RecyclerView,有很多卡可容納4 EditText。當我在一張卡的一個EditText中輸入數值時,它會在隨機卡中填充相同的值。令人驚訝的是不跳EditTexts例如:Android RecyclerView -Multiple Edittext同時改變

如果卡1的edittext1我輸入值,將填寫相同的值到edittext1在CARD8,如果我在CARD8更改值,將值更改回在卡1。有人可以告訴我爲什麼會發生這種情況。

預先感謝您

這裏是我的代碼:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {  
private String[] mDataset; 
public String[][] data = new String[30][4]; 


public static class ViewHolder extends RecyclerView.ViewHolder { 
    // each data item is just a string in this case 
    public LinearLayout mCardView; 

    public ViewHolder(LinearLayout v) { 
     super(v); 
     mCardView = v; 
    } 
} 

// Provide a suitable constructor (depends on the kind of dataset) 
public MyAdapter(String[] myDataset) { 
    mDataset = myDataset; 
} 

// Create new views (invoked by the layout manager) 
@Override 
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, 
               int viewType) { 

    LinearLayout v = (LinearLayout) LayoutInflater.from(parent.getContext()) 
      .inflate(R.layout.cards, parent, false); 

    ViewHolder vh = new ViewHolder(v); 
    return vh; 
} 


// Replace the contents of a view (invoked by the layout manager) 
@Override 
public void onBindViewHolder(ViewHolder holder, final int position) { 
    // - get element from your dataset at this position 
    // - replace the contents of the view with that element 
    final EditText txt = (EditText) holder.mCardView.findViewById(R.id.ip1_text); 
    final EditText txt2 = (EditText) holder.mCardView.findViewById(R.id.ip2_text); 

    final EditText txt3 = (EditText) holder.mCardView.findViewById(R.id.ip3_text); 
    final EditText txt4 = (EditText) holder.mCardView.findViewById(R.id.ip4_text); 
    txt.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      data[position][0] = s.toString(); 
      Log.d("DATA" + position + "0", s.toString()); 
     } 
    }); 
    txt2.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      data[position][1] = s.toString(); 
      Log.d("DATA" + position + "1", s.toString()); 
     } 
    }); 
    txt3.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      data[position][2] = s.toString(); 
      Log.d("DATA" + position + "2", s.toString()); 
     } 
    }); 
    txt4.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      data[position][3] = s.toString(); 
      Log.d("DATA" + position + "3", s.toString()); 
     } 
    }); 

    TextView t = (TextView) holder.mCardView.findViewById(R.id.serNo); 
    t.setText(mDataset[position]); 


} 

// Return the size of your dataset (invoked by the layout manager) 
@Override 
public int getItemCount() { 
    return mDataset.length; 
} 

public String[][] getData() { 
    return data; 
} 

這是我的名片XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:weightSum="1"> 
<!-- A CardView that contains a TextView --> 
<android.support.v7.widget.CardView 
    android:id="@+id/card_view" 
    android:layout_width="375dp" 
    android:layout_height="wrap_content" 
    android:layout_margin="4dp" 
    android:backgroundTint="#eeeeee" 
    card_view:cardCornerRadius="4dp"> 

    <TextView 
     android:id="@+id/serNo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:text="number" 
     android:layout_margin="20dp" 
     android:textColor="@color/cardview_dark_background" /> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center"> 

     <EditText 
      android:id="@+id/ip1_text" 
      android:layout_width="100dp" 
      android:layout_height="30dp" 
      android:layout_alignParentLeft="true" 
      android:layout_marginTop="20dp" 
      android:background="@color/cardview_light_background" 
      android:inputType="number" 
      android:maxLength="2" /> 

     <EditText 
      android:id="@+id/ip3_text" 
      android:layout_width="80dp" 
      android:layout_height="30dp" 
      android:layout_below="@+id/ip1_text" 
      android:layout_marginBottom="10dp" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="10dp" 
      android:background="@color/cardview_light_background" 
      android:inputType="number" 
      android:maxLength="1" /> 

     <EditText 
      android:id="@+id/ip2_text" 
      android:layout_width="100dp" 
      android:layout_height="30dp" 
      android:layout_marginLeft="20dp" 
      android:layout_marginTop="20dp" 
      android:layout_toRightOf="@+id/ip1_text" 
      android:background="@color/cardview_light_background" 
      android:inputType="number" 
      android:maxLength="2" /> 

     <EditText 
      android:id="@+id/ip4_text" 
      android:layout_width="80dp" 
      android:layout_height="30dp" 
      android:layout_below="@+id/ip2_text" 
      android:layout_marginBottom="10dp" 
      android:layout_marginLeft="40dp" 
      android:layout_marginTop="10dp" 
      android:layout_toRightOf="@+id/ip3_text" 
      android:background="@color/cardview_light_background" 
      android:inputType="number" 
      android:maxLength="1" /> 
    </RelativeLayout> 
</android.support.v7.widget.CardView> 
</LinearLayout> 

Added to card one

Automatically it also appears in card 10

+0

記得回收視圖回收現有的視圖(這意味着如果一個視圖被創建,它將重新使用現有的一次),所以更好的寫入條件來清除文本\ – Killer

回答

1

發生這種情況是因爲RecyclerView中的視圖正在回收。在onBindViewHolder內部,您需要將特定文本設置爲特定位置處的EditTexts。

  1. 初始化ViewHolder內您的意見和存在添加TextWatchers作爲RecyclerView的整個哲學是重用的意見:

    public static class ViewHolder extends RecyclerView.ViewHolder { 
    
        EditText txt; 
        EditText txt2; 
        EditText txt3; 
        EditText txt4; 
    
        TextView serNoTxt; 
    
        public ViewHolder(LinearLayout mCardView) { 
         super(mCardView); 
         txt = (EditText) mCardView.findViewById(R.id.ip1_text); 
         txt2 = (EditText) mCardView.findViewById(R.id.ip2_text); 
         txt3 = (EditText) mCardView.findViewById(R.id.ip3_text); 
         txt4 = (EditText) mCardView.findViewById(R.id.ip4_text); 
    
         setNoTxt = (TextView) mCardView.findViewById(R.id.serNo); 
    
         txt.addTextChangedListener(new TextWatcher() { 
          @Override 
          public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
    
          } 
    
          @Override 
          public void onTextChanged(CharSequence s, int start, int before, int count) { 
    
          } 
    
          @Override 
          public void afterTextChanged(Editable s) { 
           data[getAdapterPosition()][0] = s.toString(); 
           Log.d("DATA" + getAdapterPosition() + "0", s.toString()); 
          } 
          }); 
         txt2.addTextChangedListener(new TextWatcher() { 
          @Override 
          public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
    
          } 
    
          @Override 
          public void onTextChanged(CharSequence s, int start, int before, int count) { 
    
          } 
    
          @Override 
          public void afterTextChanged(Editable s) { 
           data[getAdapterPosition()][1] = s.toString(); 
           Log.d("DATA" + getAdapterPosition() + "1", s.toString()); 
          } 
         }); 
         txt3.addTextChangedListener(new TextWatcher() { 
          @Override 
          public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
    
          } 
    
          @Override 
          public void onTextChanged(CharSequence s, int start, int before, int count) { 
    
          } 
    
          @Override 
          public void afterTextChanged(Editable s) { 
           data[getAdapterPosition()][2] = s.toString(); 
           Log.d("DATA" + getAdapterPosition() + "2", s.toString()); 
          } 
         }); 
         txt4.addTextChangedListener(new TextWatcher() { 
          @Override 
          public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
    
          } 
    
          @Override 
          public void onTextChanged(CharSequence s, int start, int before, int count) { 
    
          } 
    
          @Override 
          public void afterTextChanged(Editable s) { 
           data[getAdapterPosition()][3] = s.toString(); 
           Log.d("DATA" + getAdapterPosition() + "3", s.toString()); 
          } 
         }); 
        } 
    } 
    
  2. 設置相應的文本到EditTexts在onBindViewHolder

    @Override 
    public void onBindViewHolder(ViewHolder holder, final int position) { 
        holder.txt1.setText(data[position][0]); 
        holder.txt2.setText(data[position][1]); 
        holder.txt3.setText(data[position][2]); 
        holder.txt4.setText(data[position][3]); 
    
        holder.setNoTxt.setText(mDataset[position]); 
    } 
    
+0

非常感謝你的工作! –

0

您更新了數據而不通知適配器。這可能會造成不一致。所以在數據更改後,您應該致電notifyItemChanged()

例如:

@Override 
    public void afterTextChanged(Editable s) { 
     data[position][3] = s.toString(); 
     Log.d("DATA" + position + "3", s.toString()); 
     notifyItemChanged(position); 
    } 

還有一個建議,我已經是重用代碼,這樣你就不必複製粘貼這麼多,在很多地方修復(例如,所有的TextWatcher對象都是基本相同)。

+0

我試過'notifyItemChanged(position)',但現在它不起作用,它只是讓我的值消失並跳躍EditText –

相關問題