2016-11-20 7 views
0

DeleteAdapter、listViewの各行はtextViewとcheckBoxで構成されます。 私はDeleteTaskを持っています。このボタンは、DeleteAdapterのチェックボックスの合計数をカウントするためのボタンで構成されています。listViewをスクロールダウンすると、チェックボックスのカウンタは減少しますが、チェックボックスの位置は変わりません。

DeleteTask

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.delete_task); 
     delete = (Button)findViewById(R.id.delete); 
     adapter = new DeleteAdapter(getApplication(), search, listview,delete); 
     } 

DeleteAdapter

public class DeleteAdapter extends BaseAdapter { 

    private static ArrayList<SearchList> search; 
    private LayoutInflater mInflater; 
    ListView listview; 
    Button delete; 
    private static int checkBoxCounter = 0; 

    public DeleteAdapter(Context context, ArrayList<SearchList> searchList, ListView listview,Button delete) { 
     search=searchList; 
     delete=delete; 
     this.listview=listview; 
     mInflater = LayoutInflater.from(context); 

    } 
    public int getCount() { 
     return search.size(); 
    } 

    public SearchList getItem(int position) { 
     return search.get(position); 
    } 

    public long getItemId(int position) { 
     return 0; 
    } 

    public void removeItem(int position) { 
     search.remove(position); 
     this.notifyDataSetChanged(); 
    } 

    public View getView(final int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 
     //delete =(Button)convertView.findViewById(R.id.delete); 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.delete_task_with_edittext, null); 
      holder = new ViewHolder(); 
      holder.text = (TextView) convertView.findViewById(R.id.task_title); 
      holder.time = (TextView) convertView.findViewById(R.id.time); 
      holder.date = (TextView) convertView.findViewById(R.id.date); 
      holder.ckbox = (CheckBox) convertView.findViewById(R.id.checkBox); 
      holder.ckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        if(buttonView.isChecked()) { 
         int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag. 
         search.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state. 
         checkBoxCounter ++; 
        } 
        else 
        { 
         checkBoxCounter--; 
        } 
        delete.setText(checkBoxCounter+""); 
       } 

      }); 
      convertView.setTag(holder); 

     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     holder.ckbox.setTag(position); 
     holder.text.setText(search.get(position).getTitle()); 
     holder.time.setText(search.get(position).getTime()); 
     holder.date.setText(search.get(position).getDate()); 
     holder.ckbox.setChecked(search.get(position).isSelected()); 
     return convertView; 

    } 

    static class ViewHolder { 
     TextView text,time,date; 
     CheckBox ckbox; 

    } 
} 

私は、チェックボックスをクリックすると、私はラインdelete.setText(checkBoxCounter+"");でNullPointerExceptionエラーを取得しますが、私はすでにDeleteTaskでそれを宣言する。誰でも私はそれを修正する方法を知っていますか?

全のStackTrace

11-21 01:12:24.005 5695-5695/com.example.seng.healthyapp E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    java.lang.NullPointerException 
      at com.example.seng.healthyapp.adapter.DeleteAdapter$1.onCheckedChanged(DeleteAdapter.java:90) 
      at android.widget.CompoundButton.setChecked(CompoundButton.java:126) 
      at android.widget.CompoundButton.toggle(CompoundButton.java:87) 
      at android.widget.CompoundButton.performClick(CompoundButton.java:99) 
      at android.view.View$PerformClick.run(View.java:17660) 
      at android.os.Handler.handleCallback(Handler.java:800) 
      at android.os.Handler.dispatchMessage(Handler.java:100) 
      at android.os.Looper.loop(Looper.java:194) 
      at android.app.ActivityThread.main(ActivityThread.java:5433) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:525) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:924) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691) 
      at dalvik.system.NativeStart.main(Native Method) 

私は別の問題が発生しました。最初の3つのチェックボックスをクリックすると、setTextボタンが3を表示します。しかし、下にスクロールすると、カウンタが減少します。あなたのアダプタのコンストラクタで

+3

であなたのコードでは、あなたは= this.deleteを使用 –

+0

@VictorVを削除する必要があります。ありがとう、あなたがなぜリストビューをスクロールすると、カウンターの数が減少するのか分かりますか? –

+0

あなたは、どのようにrecyclerview作業を理解する必要があります。一度にすべてのリストアイテムを作成するわけではなく、表示アイテムだけを作成して保持します。 @ VictorV。 –

答えて

0

public DeleteAdapter(Context context, ArrayList<SearchList> searchList, ListView listview,Button delete) { 
    search=searchList; 
    delete=delete; 
    this.listview=listview; 
    mInflater = LayoutInflater.from(context); 

} 

あなたのインスタンス変数が削除初期化しない、削除するために削除割り当てています。これにより、setText()メソッドが呼び出されたためにチェックボックスがクリックされたときにNullPointerExceptionが発生するデフォルト値nullが返されます。

1

更新コンストラクタでアダプタ

public DeleteAdapter(Context context, ArrayList<SearchList> searchList, ListView listview,Button delete) { 
    search=searchList; 
    this.delete=delete; 
    this.listview=listview; 
    mInflater = LayoutInflater.from(context); 

} 
関連する問題