有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在运行时生成卡片视图以显示来自Sqlite的数据?

你好,我需要一点帮助。我正在做一个项目,根据总体、类别和城市推荐大学。我知道如何在运行时以简单的列表视图显示大学列表,但我想使用子文本视图系统地显示大学列表,以便显示列数据,这样就可以方便地阅读大学列表

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

显示列表。java

package com.example.dsecollegerecommender;

import 安卓x.appcompat.app.AppCompatActivity;
import 安卓x.appcompat.app.AppCompatDelegate;

import 安卓.os.Bundle;

import 安卓.widget.ArrayAdapter;
import 安卓.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, 安卓.R.layout.simple_list_item_1, colleges);
        listView.setAdapter(adapter);
    }

}

函数getColleges(),从数据库访问。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代码,学院名称,位置,百分比,就像下面的图片链接一样

LIST


共 (2) 个答案

  1. # 1 楼答案

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

  2. # 2 楼答案

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

    所以你得到这个:

    enter image description here