2017-10-07 48 views
1

我想定义一个自定义SimpleCursorAdapter类,它将用于填充ListView的行。为了向后兼容,我使用了v4.widget.SimpleCursorAdapter库。我在代码的以下两部分中遇到问题。用于填充ListView行的自定义SimpleCursorAdapter

在构造函数中,我使用

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

同时,不赞成这种方式,我不知道如何修改它。

此外,在

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

“行”解决不了的,我不知道如何来引用问题的行。 (这个语法在ArrayAdapter中工作,我希望它也可以在SimpleCursorAdapter中工作...)。

也许,我的代码的一些其他部分(以下发现)是不正确的。

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) { 
     super(context,layout,c,from,to); 
     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)row.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); 
       } 
      } 
     }); 

    } 

} 

谢谢你的帮忙。 (请参见上述评论)由Mike M.做出

Ĵ

+0

在'bindView()'中,使用'view'而不是'row'。然后使用不被弃用的构造函数: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

谢谢Mike。按照建议,我用视图而不是行,并且这已解决了问题。同时,关于构造函数,我正在使用尚未弃用的构造函数(如代码中所示)。但是,看起来我必须添加“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); 
       } 
      } 
     }); 

    } 

} 

Ĵ

1

对于标准的构造函数添加一个标志参数:CursorAdapter doc.

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

而且你要使用row.findViewById但变量行不存在。由于参数是view,所以应该使用view.findViewById

+0

谢谢Nabin。 J – JF0001

+0

不客气。 :) –

相关问题