当前位置: 首页 > 知识库问答 >
问题:

如何在运行时生成Card视图以显示来自SQLite的数据?

戚鸿福
2023-03-14

你好,我想要个小帮手。我正致力于一个项目,以推荐学院的综合,类别和城市。我知道如何在简单的列表视图中显示学院列表在运行时,但我想系统地显示它,使用子文本视图显示列数据,这样它将是用户友好的阅读学院列表。

下面是我的代码来显示简单生成的学院列表。

DisplayList.java

package com.example.dsecollegerecommender;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;

import android.os.Bundle;

import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import static java.lang.Boolean.getBoolean;

public class DisplayList extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        setContentView(R.layout.activity_display_list);

        Bundle bundle = getIntent().getExtras();
        HashMap<String, String> cities = new HashMap<>();

        Boolean city1 = getIntent().getExtras().getBoolean("Pune");
        Boolean city2 = getIntent().getExtras().getBoolean("Amravati");
        Boolean city3 = getIntent().getExtras().getBoolean("Ahmednagar");
        Boolean city4 = getIntent().getExtras().getBoolean("Nashik");
        Boolean city5 = getIntent().getExtras().getBoolean("Nanded");
        Boolean city6 = getIntent().getExtras().getBoolean("Aurangabad");
        Boolean city7 = getIntent().getExtras().getBoolean("Nagpur");
        Boolean city8 = getIntent().getExtras().getBoolean("Mumbai");
        Boolean city9 = getIntent().getExtras().getBoolean("Sangli");

        if(city1) {
            cities.put("city1", "Pune");
        }
        if(city2) {
            cities.put("city2", "Amravati");
        }
        if(city3) {
            cities.put("city3", "Ahmednagar");
        }
        if(city4) {
            cities.put("city4", "Nashik");
        }
        if(city5) {
            cities.put("city5", "Nanded");
        }
        if (city6) {
            cities.put("city6", "Aurangabad");
        }
        if(city7) {
            cities.put("city7", "Nagpur");
        }
        if(city8) {
            cities.put("city8", "Mumbai");
        }
        if(city9) {
            cities.put("city9", "Sangli");
        }

        ListView listView = (ListView) findViewById(R.id.listview);
        DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
        databaseAccess.open();
        List<String> colleges = databaseAccess.getColleges((String)bundle.get("Percentage"),(String)bundle.get("Category"), cities);
        databaseAccess.close();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, colleges);
        listView.setAdapter(adapter);
    }

}

DatabaseAccess.java中的函数getColleges()返回学院列表

public List<String> getColleges(String per, String cat, HashMap<String,String> cities) {
        List<String> list = new ArrayList<>();
        String pune = cities.get("city1");
        String amra = cities.get("city2");
        String ahmed = cities.get("city3");
        String nashik = cities.get("city4");
        String nanded = cities.get("city5");
        String aurang = cities.get("city6");
        String nagpur = cities.get("city7");
        String mumbai = cities.get("city8");
        String sangli = cities.get("city9");
        String[] vals = new String[cities.size()];
        int index = 0;
        for (Map.Entry<String, String> entry : cities.entrySet()) {
            vals[index] = entry.getValue();
            index++;
        }
        if(pune == "Pune" || amra == "Amravati" || ahmed == "Ahmednagar" || nashik == "Nashik" || nanded == "Nanded" || aurang == "Aurangbad" || nagpur == "Nagpur" || mumbai == "Mumbai" || sangli == "Sangli") {
            Cursor cursor = database.rawQuery("SELECT * FROM Colleges WHERE ("+cat+" BETWEEN "+(Float.parseFloat(per)-5)+" AND " +(Float.parseFloat(per)+5)+") AND city IN (?, ?, ?, ?, ?, ?, ?, ?, ?) ORDER BY "+cat+" DESC", vals);
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                list.add(cursor.getString(1));
                list.add(cursor.getString(2));
                list.add(cursor.getString(3));
                list.add(cursor.getString(4));
                if(cat.equals("Open")) {
                    list.add(cursor.getString(5));
                }
                if(cat.equals("Obc")) {
                    list.add(cursor.getString(6));
                }
                if(cat.equals("Sc")) {
                    list.add(cursor.getString(7));
                }
                if(cat.equals("St")) {
                    list.add(cursor.getString(8));
                }
                if(cat.equals("Vjnt")) {
                    list.add(cursor.getString(9));
                }
                if(cat.equals("Ews")) {
                    list.add(cursor.getString(10));
                }
                cursor.moveToNext();
            }
            cursor.close();
        }
        return list;
    }

我想显示Dte代码,学院名称,位置,百分比就像下面的图像链接。

列表

共有2个答案

闻人昕
2023-03-14

将CardView与RecyclerView一起使用。为此,您将必须创建模型类,适配器,以获取数据到卡。基本上,它将是一个For循环,它将从sqlite中提取记录到卡上所需的textview中。

万承志
2023-03-14

如果希望对ListView项使用自定义视图,则需要创建自定义适配器。

下面是一个例子:

public class BrandsListAdapter extends BaseAdapter {

    private ArrayNode _data_set;
    private Context _context;

    public BrandsListAdapter(ArrayNode dataSet, @NonNull Context context) {
        super();
        this._context = context;
        this._data_set = dataSet;
    }

    public int getCount()
    {
        return _data_set.size();
    }

    public Object getItem(int position)
    {
        return position;
    }
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        try {
            JsonNode list_model = _data_set.get(position);

            if(convertView == null) {
                LayoutInflater layout_inflater = LayoutInflater.from(_context);
                convertView = layout_inflater.inflate(R.layout.item_brands, parent, false);
            }

            
        return convertView;
    }
}

那么在activity的代码中,您可以这样做:

BrandListAdapter adapter = new BrandListAdapter(dataset, getBaseContext());

在这个适配器中,我使用了:

if(convertView == null) {
            LayoutInflater layout_inflater = LayoutInflater.from(_context);
            convertView = layout_inflater.inflate(R.layout.item_brands, parent, false);
        }

通过这种方式,我膨胀了名为 的自定义布局,它是我先前在 layouts/code>中创建的,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:padding="15dp"
    android:clickable="true"
    android:focusable="true"
    android:foreground="?android:attr/selectableItemBackground"
    android:foregroundTint="@color/colorPrimary"
    android:layout_height="80dp"
    android:gravity="center_vertical"
    android:layout_marginEnd="@dimen/standard_layout_margin"
    android:layout_marginStart="@dimen/standard_layout_margin">

    <ImageView
       android:layout_width="@dimen/brand_image_width"
       android:layout_height="@dimen/brand_image_height"
       android:src="@drawable/ic_image"
       android:id="@+id/brand_photo"
       android:layout_centerInParent="true"
       android:scaleType="fitCenter"/>
            
</RelativeLayout>

null

ImageView brand_image = convertView.findViewById(R.id.brand_photo);

并像往常一样使用 或任何其他变量。要获得CardView外观,可以使用以下内容:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:clickable="true"
    android:layout_gravity="center"
    android:focusable="true"
    app:cardCornerRadius="10dp"
    app:cardElevation="20dp"
    android:layout_marginStart="5dp"
    android:layout_marginEnd="5dp"
    android:foreground="?android:attr/selectableItemBackground"
    android:foregroundTint="@color/colorPrimary"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:gravity="center_vertical"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAlignment="textStart"
            android:text="SOMETEXT"
            android:layout_marginStart="5dp"
            android:textSize="@dimen/subtitle1"
            style="@style/TextAppearance.MaterialComponents.Subtitle1"
            android:textColor="@color/colorSecondary"
            android:id="@+id/tv_brand_name"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAlignment="textStart"
            android:text="SOMETEXT"
            android:layout_marginStart="5dp"
            android:textSize="@dimen/subtitle1"
            style="@style/TextAppearance.MaterialComponents.Subtitle1"
            android:textColor="@color/colorSecondary"
           />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAlignment="textStart"
            android:text="SOMETEXT"
            android:layout_marginStart="5dp"
            android:textSize="@dimen/subtitle1"
            style="@style/TextAppearance.MaterialComponents.Subtitle1"
            android:textColor="@color/colorSecondary"
            />
    </LinearLayout>
</androidx.cardview.widget.CardView>

所以你得到了这个:

 类似资料:
  • 我想在JTable中显示MySQL中的数据,但只显示了表中的最后一行。请帮帮我。我知道我有一个问题,因为jt=newjtable(数据,列)每次都为每行创建一个新表(删除之前的),但我找不到正确的选项。

  • 我试着用特定的透明颜色在Android画布中显示一个jpg。它与png配合得很好,我也知道如何用java将jpg转换为png,所以最后我在文件系统上有了一个新的png文件。 现在我的问题是:有没有办法从文件系统读取jpg文件,在运行时设置透明颜色(转换为png)并在运行时显示图像? 附加注释:我尝试在自定义视图中使用ondraw方法和drawbitmap来实现这一点。我不能使用imageview:

  • 我有一个,如下所示,它可以从CMS/Database中提取数据。  反应代码: Z行打印以下内容: 问题说明:在上面的代码中,中的div没有打印任何内容,尽管我能够在中显示所有内容。

  • 我的一个客户上传了一些照片到他们的谷歌驱动器,并希望我显示他们的照片在他们的公司网站上。我查看了关于在web页面上显示Google Drive内容的文档,但似乎这只是在web页面上显示内容的说明,它已经这样做了。 我的问题是,如何直接在客户端的网页上显示内容?

  • 使用Android在中显示现有Firestore数据库中的数据的最佳方法是什么?

  • 问题内容: 此代码将图片test.gif放入图片位值中。 我的下一个目标是显示以字节为单位保存在数据库中的图片。如何才能做到这一点 ? 问题答案: 就像是: 可能为您工作。

  • 提前致谢 下面是我的plugin.xml