2016-05-13 12 views
0

ListViewに保存するか、またはEditTextを含むデータを取得しようとしています。 画面サイズを超える行を持つListViewからデータを保存または取得しようとすると、エラーが発生します。私は理由を知っていますが、それを修正する方法はわかりません。理由は彼らが意見を再利用しているからです。Listviewのサイズが画面サイズを超えると、データを保存する方法はありますか?

ここに私がしたいことの例があります。画面に5行、画面外に1行がある場合、6行目への参照はどのように取得できますか?

public void saveData(){ 
     StringBuffer buffer = new StringBuffer(); 
     int count = listView.getCount(); 
     for(int i=0 ;i < count ;i++) { 
      View child = listView.getChildAt(i); 
      TextView setText1 = (TextView)child.findViewById(R.id.TextView1); 
      EditText text = (EditText) child.findViewById(R.id.setText1); 
      String content1 = setText1.getText().toString().trim(); 
      String content2 = text.getText().toString().trim(); 
      buffer.append(content1); 
      buffer.append(","); 
      buffer.append(content2); 
      buffer.append(",");    
    } 

Stringbufferにデータを格納します。

public class CustomAdapter extends ArrayAdapter<String> { 

    public CustomAdapter(Context context, List<String> num_set) { 
     super(context, R.layout.custom_row ,num_set); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = LayoutInflater.from(getContext()); 
     View CustomView = inflater.inflate(R.layout.custom_row ,parent , false); 


     String[] num = getItem(position).split(","); 
     TextView setText1 = (TextView)CustomView.findViewById(R.id.TextView1); 
     EditText setText2 =(EditText)CustomView.findViewById(R.id.setText2); 

     setText1.setText(num[0]); 
     setText2.setText(num[1]); 
     return CustomView; 
    } 

} 

ここはlogcatです。引き起こさ :ここ

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference 

custom_row.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:weightSum="1"> 

    <TextView 
     android:id="@+id/TextView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Text" /> 

    <EditText 
     android:id="@+id/setText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:enabled="true" 
     android:inputType="numberDecimal" 
     android:textSize="24sp" /> 
</LinearLayout> 
+0

あなたのアダプタコードを共有してください。 –

+0

はlogcatエラーを共有します。 –

+0

@jankigadhiya私はそれを実行しました –

答えて

0
editText.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) { 

       } 
      }); 

使用addtextのLISTNER、あなたが特定のデータを取得するので、あなたがのEditTextにデータを書き込む時はいつでも保存しています。

0

ビューからデータを分離する必要があります。 ListViewでは、画面上にない行は実際には存在しません!行に何かを入力すると、それを画面の外にスクロールし、もう一度テキストが消えたことに気づくでしょう。

Arjun's answerのようなものを使用して、どこかのonTextChangedにデータを保存することをお勧めします。このようにして、スクロールするときにテキストを再び復元することもできます。

また、(アダプタのconvertView引数を使用して)行を再利用することはありません。毎回新しい行を作成するため、ViewHolderパターンも読み込んでください。

関連する問題