如何在列表视图中显示外部数据库的数据? [英] How to display the data of an external database in the listview?

查看:60
本文介绍了如何在列表视图中显示外部数据库的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目和带有简单适配器的Listview中使用了外部SQlite数据库,实际上它工作正常,但我想使用glide库在Listview中显示图像.我将图像url存储在数据库中,但我不知道如何将其传递给滑行,这可能与否? 预先感谢.

I am using an external SQlite database in my project and a Listview with simple adapter, in fact it works fine but i wanna show images in Listview using glide library. i stored images url in database but i don't know how can I pass it to glide , it's possible or not? thanks in advance.

public class DatabaseHandler extends SQLiteOpenHelper {
private Context main_context;

private static String DB_PATH;
private static String DB_NAME = "ebook_db.db";
private static int DB_VERSION = 1;
private static String DB_TBL_BOOKS = "books";
private static String DB_TBL_SETTINGS = "settings";

private SQLiteDatabase db;


public DatabaseHandler(Context con) {
    super(con, DB_NAME, null, DB_VERSION);

    main_context = con;

    DB_PATH = con.getCacheDir().getPath() + "/" + DB_NAME;
}

@Override
public void onCreate(SQLiteDatabase db) {
    /* do nothing */
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    /* do nothing */
}

private boolean dbExists() {
    File f = new File(DB_PATH);
    if (f.exists())
        return true;
    else
        return false;
}

private boolean copyDB() {
    try {
        FileOutputStream out = new FileOutputStream(DB_PATH);

        InputStream in = main_context.getAssets().open(DB_NAME);

        byte[] buffer = new byte[1024];
        int ch;

        while ((ch = in.read(buffer)) > 0) {
            out.write(buffer, 0, ch);
        }

        out.flush();
        out.close();
        in.close();

        return true;
    } catch (Exception e) {
        /* do nothing */
        return false;
    }
}

public void open() {
    if (dbExists()) {
        try {
            File temp = new File(DB_PATH);

            db = SQLiteDatabase.openDatabase(
                    temp.getAbsolutePath(), null, SQLiteDatabase.OPEN_READWRITE
            );
        } catch (Exception e) {
            /* do nothing */
        }
    } else {
        if (copyDB())
            open();
    }
}

@Override
public synchronized void close() {
    db.close();
}
public List<HashMap<String,Object>> getTableOfContent(String id) {

    Cursor result = db.rawQuery("SELECT * FROM " + DB_TBL_BOOKS + " WHERE author = '" + id + "'", null);

    List<HashMap<String, Object>> all_data = new ArrayList<>();

    while (result.moveToNext()) {
        HashMap<String,Object> temp = new HashMap<>();

        temp.put("id", result.getString(0));
        temp.put("title", result.getString(1));
        temp.put("author", result.getString(3));
        temp.put("image", result.getString(8));

        if (result.getString(9).equals("1")) {
            temp.put("fav_flag", R.drawable.is_favorite);
        } else {
            temp.put("fav_flag", R.drawable.not_favorite);
        }

        if (result.getString(10).equals("1")) {
            temp.put("see_flag", R.drawable.see);
        } else {
            temp.put("see_flag", R.drawable.not_see);
        }

        all_data.add(temp);
    }

    return all_data;
}

tblOfContent.java

tblOfContent.java

public class tblOfContent extends AppCompatActivity {
private ListView contentListView;

private List<HashMap<String, Object>> books_list;

private DatabaseHandler db;

public String my_key_number;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tbl_of_content);

    getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        my_key_number = extras.getString("number_of_keys");

        Toast.makeText(this, my_key_number, Toast.LENGTH_LONG).show();
    }

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);
    //actionBar.setTitle(R.string.doctors_title);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
        }
    });

    contentListView = findViewById(R.id.tblOfContentListView);
    db = new DatabaseHandler(getBaseContext());

    db.open();
    books_list = db.getTableOfContent(my_key_number);
    String[] from = {"title", "image", "fav_flag", "see_flag"};

    int[] to = {R.id.txtTitle, R.id.url, R.id.setFav, R.id.setSee};

    SimpleAdapter adb = new SimpleAdapter(
            getBaseContext(), books_list, R.layout.foods_row_item, from, to
    );


    contentListView.setAdapter(adb);


    db.close();

    contentListView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Intent i = new Intent(getBaseContext(), book_content.class);

                    String my_id = books_list.get(position).get("id").toString();

                    i.putExtra("id", my_id);

                    startActivity(i);

                }
            }
    );

}

}

推荐答案

尝试一下:

    String[] from = {"title", "fav_flag", "see_flag"};
    int[] to = {R.id.txtTitle, R.id.setFav, R.id.setSee};
    SimpleAdapter adb = new SimpleAdapter(getBaseContext(), books_list, R.layout.foods_row_item, from, to){
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View itemView = super.getView(position, convertView, parent);

            String url = (String)((HashMap<String, Object>)getItem(position)).get("image");
            ImageView imageView = (ImageView)itemView.findViewById(R.id.url);
            Glide.with(this)
                    .load(url)
                    .into(imageView);

            return itemView;
        }
    };

希望有帮助!

这篇关于如何在列表视图中显示外部数据库的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆