2017-10-07 7 views
1

ListViewの行を取り込むために使用されるカスタムSimpleCursorAdapterクラスを定義しようとしています。下位互換性のためにv4.widget.SimpleCursorAdapterライブラリを使用しています。私はコードの次の2つの部分に問題があります。コンストラクタでListViewの行を取り込むためのカスタムSimpleCursorAdapter

は、私が

super(context,layout,c,from,to); 

を使用しています。一方、これは廃止され、私はそれを修正する方法がわからないです。

はまた、

alarm_activated = (ToggleButton)row.findViewById(R.id.alarm_activated); 

に「行」は解決できない、と私は質問の行を参照する方法を確認していません。 (この構文はArrayAdapterで動作していましたが、SimpleCursorAdapterでも動作することを期待していました...)。

おそらく、私のコードの他の部分(以下に見られる)が間違っている可能性があります。

ありがとうございました。

マイク・M.(上記のコメントを参照してください)によって行われた提案に続いてJ

+0

で使用する必要があります。 'bindView()'では 'row'ではなく' view'を使います。次に、推奨されていないコンストラクタを使用してください:https://developer.android.com/reference/android/support/v4/widget/SimpleCursorAdapter.html#SimpleCursorAdapter(android.content.Context,%20int,%20android.database.Cursor、 %20java.lang.String []、%20int []、%20int)。 –

+0

マイクありがとうございました。私は行の代わりにビューを使用しましたが、これは問題を解決しました。一方、コンストラクタに関しては、(コードに見られるように)既に推奨されていないものを使用しています。それにもかかわらず、私は "super(context、layout、c、from、to);" (これはAndroid Studioに下線を引くように "super"キーワードを表示しますが、非推奨となっています)。その部分を削除すると、使用されているサポートライブラリで使用可能なデフォルトのコンストラクタがないことを示すエラーが表示されます。 J – JF0001

+0

"私は既に(コードに見られるように)廃止されていないものを使用しています。" - 私はどこにも見ない。あなたは 'flags'引数を欠いています。 –

答えて

1

、ここでは他の誰かがそれが役立つだろう場合の作業コードです。また、最初のコードを開発するときにこのlinkにある例を使用していたことに注意してください(そのコードのBobbake4に感謝します)。

class AlarmRowAdapter extends SimpleCursorAdapter { 

    private Context mContext; 
    private Context appContext; 
    private int layout; 
    private Cursor cr; 
    private final LayoutInflater inflater; 


    public AlarmRowAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) { 
     super(context,layout,c,from,to, flags); 
     this.layout=layout; 
     this.mContext = context; 
     this.inflater=LayoutInflater.from(context); 
     this.cr=c; 
    } 

    @Override 
    public View newView (Context context, Cursor cursor, ViewGroup parent) { 
     return inflater.inflate(layout, null); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     super.bindView(view, context, cursor); 

     alarm_activated = (ToggleButton)view.findViewById(R.id.alarm_activated); 

     if (activationInt == 1) { 
      alarm_activated.setChecked(true); 
      alarm_activated.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY); 
     } else { 
      alarm_activated.setChecked(false); 
     } 

     alarm_activated.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if (isChecked) { 
        buttonView.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY); 
       } else { 
        buttonView.getBackground().setColorFilter(Color.LTGRAY, PorterDuff.Mode.MULTIPLY); 
       } 
      } 
     }); 

    } 

} 

J

1

標準コンストラクタのフラグパラメータを追加:CursorAdapter doc.

super (context,layout,c, from,to, 0); 

をそして、あなたはrow.findViewByIdを使用しようとしている。しかし、変数の行は存在しません。パラメータはviewなので、view.findViewById

+0

ありがとうNabin。 J – JF0001

+0

ようこそ。 :) –

関連する問題