尝试用数据库图像填充列表视图

时间:2018-07-21 02:16:54

标签: android image listview

我正在尝试使用名称和电话,ID和图像填充ID(未显示ID,仅当我单击所需的客户端时才提供该ID,仅播放通过ID请求找到信息的其他活动),我试图增加与我的问题相关的其他帖子的代码,但没有成功,有人可以帮助我吗?我不是程序员,我只是想作为一种业余爱好来学习更多,到目前为止,我所拥有的...

Datasource.java(用于在数据库中搜索信息的代码)

    public List<String> getAllClients(){
    List<String> list = new ArrayList<String>();

    String selectQuery = "SELECT * FROM userDB ORDER BY nome ASC";

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            list.add(cursor.getString(2));
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return list;
}

MainActivity.java(具有适配器)

private void ClientList() {

    DataSource db = new DataSource(getApplicationContext());
    List<String> clientlist = db.getAllClients();
    clientAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, clientlist);
    clientList.setAdapter(clientAdapter);

}

content_main.xml(包含一个搜索视图和一个列表视图)

<SearchView
    android:id="@+id/searchView"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="5dp"
    android:queryHint="@string/search"
    android:iconifiedByDefault="false"
    android:background="#FFFFFF"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/searchView" />

我发现,要想做我想做的事,我必须有一个自定义列表视图,所以就是这样

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:padding="5dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="5dp"
    android:layout_marginTop="5dp"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/perfil_image_cliente"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginEnd="20dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="20dp"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/perfil_image" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/nome_lista_client"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="20dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="20dp"
            android:layout_marginStart="0dp"
            android:layout_marginTop="5dp"
            android:text="Nome: "
            android:textColor="#008080"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/fone_lista_client"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="20dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="20dp"
            android:layout_marginStart="0dp"
            android:layout_marginTop="5dp"
            android:text="Fone: "
            android:textColor="@color/common_google_signin_btn_text_dark_focused"
            android:textSize="18sp" />

    </LinearLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

使用调试器检查getAllClients()是否返回了所有内容。

如果不检查数据库中是否有内容,以及查询是否正确。

如果getAllClients()返回某项,则您的列表实现存在问题,请检查http://www.vogella.com/tutorials/AndroidListView/article.html

相关问题