I have a list of image urls in my sqlite database(prepoulated) stored in "pictures" column, and i want to adapt them to my imageview in child_list using a glide or picasso library. i'm using a simpletreecursoradapater for an expendablelistview and that's what complicate adapting answers in other questions. i'm new to android. so please show me where to add the suggested code..thanks
this is my 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);
}
and this is my 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"
/>
lets make a simple class contain two Strings and name it 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;
}
}
after that you have to query your database and return a cursor after that call it on your main Activity use this loop to convert your cursor to List
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));
}
now you can have list for both of your text and Image URL now it's time to add this data to our adapter we will name it FirstAdapter
FirstAdapter adapter = new FirstAdapter (getActivity(),mArrayList );
and set the adapter to your list, for your adapter it will be like that
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;
}
}
feel free to accept the answer and if I didn't help you try to search in the Internet you will find many examples