如何将sqlite数据库中的网址改编为imageview

时间:2018-07-31 14:58:23

标签: android sqlite picasso android-glide

我在sqlite数据库中(预先配置)有一个图像URL列表(存储在“图片”列中),我想使用glide或Picasso库将其调整为child_list中的imageview。 我正在将一个simpletreecursoradapater用于一个expandablelistview,这就是在其他问题中调整答案的复杂性。 我是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"
    />

2 个答案:

答案 0 :(得分:0)

在适配器内部创建一个名为setData()的方法,该方法接受字符串数组(要在expandableListView中显示的所有图像url),并在要将图像设置为适配器时调用此方法,该方法如下所示< / p>

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);
    }

答案 1 :(得分:0)

让一个简单的类包含两个String并将其命名为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;
}
}

之后,您必须查询数据库并在主Activity上调用它之后返回一个游标,请使用此循环将游标转换为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));
}

现在您可以拥有文本和图像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上搜索,您会发现很多示例