如何在运行时生成card视图来显示来自sqlite的数据?

vql8enpb  于 2021-06-26  发布在  Java
关注(0)|答案(2)|浏览(421)

你好,我需要一点帮助。我正在做一个项目,根据总体、类别和城市推荐大学。我知道如何在运行时以简单的列表视图显示学院列表,但我想使用子文本视图系统地显示它,以便显示列数据,这样可以方便地阅读学院列表。
下面是我的代码来显示简单生成的学院列表。
显示列表.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代码,大学名称,地点,百分比就像下面的图片链接。
列表

gywdnpxw

gywdnpxw1#

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

hfwmuf9z

hfwmuf9z2#

如果要为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;
    }
}

然后在活动中的代码列表中,可以执行以下操作:

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

这样,我膨胀了我的自定义布局命名为 item_brands ,我之前在 res>layouts 看起来是这样的:

<?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>

然后在 getView()CustomAdapter 您可以使用它来获取视图和填充数据:

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

使用 brand_image 或任何其他变量。要获得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>

所以你得到这个:

相关问题