我在SQLite数据库中使用路径作为字符串,并且正在使用Viewbinder来采用视图。.我正在尝试使用Glide库中的Glide库将SQLite数据库中的路径转换为图片。 (this)“ this”保持红色突出显示。.我的代码有什么问题?
编辑1 这是我的代码
MAINACTIVTY.JAVA
public class MainActivity extends AppCompatActivity {
ExpandableListView expandableListView;
Database mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] childFrom = new String[]{Database.DATABASE_CHILD_1,Database.DATABASE_CHILD_2,Database.DATABASE_CHILD_3};
String[] groupFrom = new String[]{Database.DATABASE_GROUP_1};
int[] groupTo = {R.id.group1};
int[] childTo = {R.id.child1,R.id.child2,R.id.child3};
simplecursortreeAdapter.setViewBinder(new MyViewBinder());
expandableListView = findViewById(R.id.expandableListview);
expandableListView.setAdapter(simplecursortreeAdapter);
}
public class MyViewBinder implements SimpleCursorTreeAdapter.ViewBinder {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int viewID = view.getId();
switch(viewID){
case R.id.group1 :
TextView groupName = (TextView) view;
String groupname;
groupname = cursor.getString(cursor.getColumnIndex(Database.DATABASE_GROUP_1));
groupName.setText(groupname);
break;
case R.id.child1 :
TextView friendName = (TextView) view;
String friend_name;
friend_name = cursor.getString(cursor.getColumnIndex(Database.DATABASE_CHILD_1));
friendName.setText(friend_name);
break;
case R.id.child2 :
ImageView contactProfile = (ImageView) view;
byte[] imageBytes = cursor.getBlob(cursor.getColumnIndex(Database.DATABASE_CHILD_2));
if(imageBytes != null ){
// Pic image from database
contactProfile.setImageBitmap(BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length));
}else {
// If image not found in database , assign a default image
contactProfile.setBackgroundResource(0);
}
break;
case R.id.child3 :
String urls;
urls = cursor.getString(cursor.getColumnIndex(Database.DATABASE_CHILD_3));
ImageView url=findViewById(R.id.child3);
Glide.with(MainActivity.this).load(urls).into(url);
break;
}
return true;
答案 0 :(得分:0)
尝试
Glide.with(YourActivityName.this).load(urls).into(url);
将YourActivityName
替换为当前活动。
答案 1 :(得分:0)
这是简单的Glide.with(this),您需要检查gradle是否具有正确的依赖项。
第一个参数是上下文
如果您处于Context的封闭类(内部匿名类,如侦听器)中,则需要在该上下文之外引用该类,这样该语句将是:
Glide.with(OuterClassActivity.this).load(urls).into(url);
如果您是片段,您:
Glide.with(getActivity()).load(urls).into(url);
或者如果您使用的是没有上下文可用的辅助方法:
ImageView url=findViewById(R.id.child3);
Glide.with(url.getContext()).load(urls).into(url);