簡體   English   中英

如何在運行時生成卡片視圖以顯示來自 Sqlite 的數據?

[英]How can I Generate Card view at run time to display data from Sqlite?

您好,我需要一點幫助。 我正在做一個項目,根據聚合、類別和城市推薦大學。 我知道如何在運行時在簡單列表視圖中顯示大學列表,但我想使用子文本視圖系統地顯示它以顯示列數據,以便用戶友好地閱讀大學列表。

下面是我顯示簡單生成的大學列表的代碼。

顯示列表.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);
    }

}

function getColleges() from DatabaseAccess.java 返回學院列表

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 代碼、大學名稱、位置、百分比,就像下面的圖片鏈接一樣。

列表

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

通過這種方式,我擴展了我之前在res>layouts中創建的名為item_brands的自定義布局,如下所示:

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

然后在CustomAdaptergetView()中,您可以使用它來獲取視圖並填充數據:

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>

所以你得到這個:

在此處輸入圖像描述

將 CardView 與 RecyclerView 一起使用。 為此,您必須創建 Model class,將數據提取到卡中的適配器。 基本上它將是一個 For 循環,它將從 sqlite 獲取記錄到卡上所需的 textview 中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM