如何将 sqlite 数据库中的 url 适配到 imageview
how to adapt an url in sqlite database to imageview
我的 sqlite 数据库(预填充)中有一个图像 url 列表存储在 "pictures" 列中,我想使用 glide 或 picasso 库将它们适应我在 child_list 中的图像视图。
我正在为可扩展列表视图使用 simpletreecursoradater,这使其他问题的适应性答案变得复杂。
我是 android 的新手。所以请告诉我在哪里添加建议的代码..谢谢
这是我的Database.java
public class Database {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "URTd.db";
private static final String DATABASE_TABLE = "OrganAnatomy";
public static final String DATABASE_ID = "_id";
public static final String DATABASE_GROUP_1 = "Larynx_features";
public static final String DATABASE_CHILD_1 = "Larynx";
public static final String DATABASE_CHILD_2 = "pictures";
public void open() {
mDatabaseHelper = new DatabaseHelper(mContext, DATABASE_NAME, null, DATABASE_VERSION);
mDB = mDatabaseHelper.getWritableDatabase();
}
public Cursor getDatabase() {
String whereclause = DATABASE_CHILD_1 + " IS NOT NULL";
return mDB.query(DATABASE_TABLE, null, whereclause, null, null, null, DATABASE_ID);
}
这是我的 Main.activity.java
public class MainActivity extends AppCompatActivity {
ExpandableListView expandableListView;
Database mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDatabase = new Database(this);
mDatabase.open();
Cursor cursor = mDatabase.getDatabase();
startManagingCursor(cursor);
String[] childFrom = new String[]{Database.DATABASE_CHILD_1,Database.DATABASE_CHILD_2};
String[] groupFrom = new String[]{Database.DATABASE_GROUP_1};
int[] groupTo = {R.id.group1};
int[] childTo = {R.id.child1,R.id.child2};
SimpleCursorTreeAdapter simplecursortreeAdapter = new ExpandableListViewAdapter(
this,
cursor,
R.layout.list_group,
groupFrom,
groupTo,
R.layout.list_child,
childFrom,
childTo
);
expandableListView = findViewById(R.id.expandableListview);
expandableListView.setAdapter(simplecursortreeAdapter);
}
protected void onDestroy() {
super.onDestroy();
mDatabase.close();
}
private class ExpandableListViewAdapter extends SimpleCursorTreeAdapter {
private ExpandableListViewAdapter(
Context context,
Cursor cursor,
int groupLayout,
String[] groupFrom,
int[] groupTo,
int childLayout,
String[] childFrom,
int[] childTo) {
super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childFrom, childTo);
}
protected Cursor getChildrenCursor(Cursor groupCursor) {
return mDatabase.getID(groupCursor.getInt(groupCursor.getColumnIndex(Database.DATABASE_ID)));
Main.activity.xml
<ExpandableListView
android:id="@+id/expandableListview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
child_list.xml
<TextView
android:id="@+id/child1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="16sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/child2"
/>
在你的适配器中创建一个名为 setData() 的方法,它接受字符串数组(你想要在你的 expandableListView 中显示的所有图像 url)并在你想要将图像设置到你的适配器时调用这个方法,它看起来像
void setData(List<String> imagesUrls){
listItems.addAll(imagesUrls);
notifyDataSetChanged();
}
其中 listItems 是适配器内部的局部变量,当您想将每个图像设置到特定位置时,您可以这样做
String image;
image = listItems.get(position);
try {
Picasso.with(context).load(image).into(holder.imageView);
Log.e("Images", " PIC " + image);
} catch (Exception o) {
Log.e("Images", "NOOOOOOOOO PIC " + position);
}
让我们制作一个简单的 class 包含两个字符串并将其命名为 FirstClass
public class FirstClass {
String text;
String Image;
public FirstClass(String text, String image) {
this.text = text;
Image = image;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
}
之后你必须查询你的数据库和 return 游标然后在你的主 Activity 上调用它 Activity 使用这个循环将你的游标转换为列表
ArrayList<FirstClass> mArrayList = new ArrayList<FirstClass>();
for(mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) {
mArrayList.add(mCursor.getString(Text_COLUMN_INDEX),
mCursor.getString(Image_COLUMN_INDEX));
}
现在您可以拥有文本和图像列表 URL 现在是时候将此数据添加到我们的适配器中了,我们将其命名为 FirstAdapter
FirstAdapter adapter = new FirstAdapter (getActivity(),mArrayList );
并将适配器设置到您的列表中,对于您的适配器,它将是这样的
public class FirstAdapter extends ArrayAdapter<FirstClass> {
/**
* Create a new {@link FirstAdapter} object.
*
* @param context is the current context (i.e. Activity) that the adapter is being created in.
* @param firstClass is the list of {@link firstClass}s to be displayed.
*/
public WordAdapter(Context context, ArrayList<FirstClass> firstClass) {
super(context, 0, firstClass);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if an existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.child_list, parent, false);
}
// Get the {@link Word} object located at this position in the list
FirstClass currentWord = getItem(position);
// Find the TextView in the child_list.xml layout
TextView textView = (TextView) listItemView.findViewById(R.id.child1);
textView.setText(currentWord.getText());
// Find the ImageView in the child_list.xml layout with the ID image.
ImageView imageView = (ImageView) listItemView.findViewById(R.id.child2);
try {
Picasso.with(context).load(currentWord.getImage()).into(holder.imageView);
Log.e("Images", " PIC " + image);
} catch (Exception o) {
Log.e("Images", "NOOOOOOOOO PIC " + position);
}
// Return the whole list item layout so that it can be shown in
// the ListView.
return listItemView;
}
}
请随意接受答案,如果我没有帮助您尝试在 Internet 中搜索您会找到很多示例
我的 sqlite 数据库(预填充)中有一个图像 url 列表存储在 "pictures" 列中,我想使用 glide 或 picasso 库将它们适应我在 child_list 中的图像视图。 我正在为可扩展列表视图使用 simpletreecursoradater,这使其他问题的适应性答案变得复杂。 我是 android 的新手。所以请告诉我在哪里添加建议的代码..谢谢
这是我的Database.java
public class Database {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "URTd.db";
private static final String DATABASE_TABLE = "OrganAnatomy";
public static final String DATABASE_ID = "_id";
public static final String DATABASE_GROUP_1 = "Larynx_features";
public static final String DATABASE_CHILD_1 = "Larynx";
public static final String DATABASE_CHILD_2 = "pictures";
public void open() {
mDatabaseHelper = new DatabaseHelper(mContext, DATABASE_NAME, null, DATABASE_VERSION);
mDB = mDatabaseHelper.getWritableDatabase();
}
public Cursor getDatabase() {
String whereclause = DATABASE_CHILD_1 + " IS NOT NULL";
return mDB.query(DATABASE_TABLE, null, whereclause, null, null, null, DATABASE_ID);
}
这是我的 Main.activity.java
public class MainActivity extends AppCompatActivity {
ExpandableListView expandableListView;
Database mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDatabase = new Database(this);
mDatabase.open();
Cursor cursor = mDatabase.getDatabase();
startManagingCursor(cursor);
String[] childFrom = new String[]{Database.DATABASE_CHILD_1,Database.DATABASE_CHILD_2};
String[] groupFrom = new String[]{Database.DATABASE_GROUP_1};
int[] groupTo = {R.id.group1};
int[] childTo = {R.id.child1,R.id.child2};
SimpleCursorTreeAdapter simplecursortreeAdapter = new ExpandableListViewAdapter(
this,
cursor,
R.layout.list_group,
groupFrom,
groupTo,
R.layout.list_child,
childFrom,
childTo
);
expandableListView = findViewById(R.id.expandableListview);
expandableListView.setAdapter(simplecursortreeAdapter);
}
protected void onDestroy() {
super.onDestroy();
mDatabase.close();
}
private class ExpandableListViewAdapter extends SimpleCursorTreeAdapter {
private ExpandableListViewAdapter(
Context context,
Cursor cursor,
int groupLayout,
String[] groupFrom,
int[] groupTo,
int childLayout,
String[] childFrom,
int[] childTo) {
super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childFrom, childTo);
}
protected Cursor getChildrenCursor(Cursor groupCursor) {
return mDatabase.getID(groupCursor.getInt(groupCursor.getColumnIndex(Database.DATABASE_ID)));
Main.activity.xml
<ExpandableListView
android:id="@+id/expandableListview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
child_list.xml
<TextView
android:id="@+id/child1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="16sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/child2"
/>
在你的适配器中创建一个名为 setData() 的方法,它接受字符串数组(你想要在你的 expandableListView 中显示的所有图像 url)并在你想要将图像设置到你的适配器时调用这个方法,它看起来像
void setData(List<String> imagesUrls){
listItems.addAll(imagesUrls);
notifyDataSetChanged();
}
其中 listItems 是适配器内部的局部变量,当您想将每个图像设置到特定位置时,您可以这样做
String image;
image = listItems.get(position);
try {
Picasso.with(context).load(image).into(holder.imageView);
Log.e("Images", " PIC " + image);
} catch (Exception o) {
Log.e("Images", "NOOOOOOOOO PIC " + position);
}
让我们制作一个简单的 class 包含两个字符串并将其命名为 FirstClass
public class FirstClass {
String text;
String Image;
public FirstClass(String text, String image) {
this.text = text;
Image = image;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
}
之后你必须查询你的数据库和 return 游标然后在你的主 Activity 上调用它 Activity 使用这个循环将你的游标转换为列表
ArrayList<FirstClass> mArrayList = new ArrayList<FirstClass>();
for(mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) {
mArrayList.add(mCursor.getString(Text_COLUMN_INDEX),
mCursor.getString(Image_COLUMN_INDEX));
}
现在您可以拥有文本和图像列表 URL 现在是时候将此数据添加到我们的适配器中了,我们将其命名为 FirstAdapter
FirstAdapter adapter = new FirstAdapter (getActivity(),mArrayList );
并将适配器设置到您的列表中,对于您的适配器,它将是这样的
public class FirstAdapter extends ArrayAdapter<FirstClass> {
/**
* Create a new {@link FirstAdapter} object.
*
* @param context is the current context (i.e. Activity) that the adapter is being created in.
* @param firstClass is the list of {@link firstClass}s to be displayed.
*/
public WordAdapter(Context context, ArrayList<FirstClass> firstClass) {
super(context, 0, firstClass);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if an existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.child_list, parent, false);
}
// Get the {@link Word} object located at this position in the list
FirstClass currentWord = getItem(position);
// Find the TextView in the child_list.xml layout
TextView textView = (TextView) listItemView.findViewById(R.id.child1);
textView.setText(currentWord.getText());
// Find the ImageView in the child_list.xml layout with the ID image.
ImageView imageView = (ImageView) listItemView.findViewById(R.id.child2);
try {
Picasso.with(context).load(currentWord.getImage()).into(holder.imageView);
Log.e("Images", " PIC " + image);
} catch (Exception o) {
Log.e("Images", "NOOOOOOOOO PIC " + position);
}
// Return the whole list item layout so that it can be shown in
// the ListView.
return listItemView;
}
}
请随意接受答案,如果我没有帮助您尝试在 Internet 中搜索您会找到很多示例