你好,我需要一点帮助。我正在做一个项目,根据总体、类别和城市推荐大学。我知道如何在运行时以简单的列表视图显示学院列表,但我想使用子文本视图系统地显示它,以便显示列数据,这样可以方便地阅读学院列表。
下面是我的代码来显示简单生成的学院列表。
显示列表.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条答案
按热度按时间gywdnpxw1#
将cardview与recyclerview一起使用。为此,您必须创建一个模型类,一个适配器,用于将数据提取到卡中。基本上,它将是一个for循环,将记录从sqlite提取到卡上所需的textview中。
hfwmuf9z2#
如果要为listview项使用自定义视图,则需要创建自定义适配器。
举个例子:
然后在活动中的代码列表中,可以执行以下操作:
在我使用的适配器中:
这样,我膨胀了我的自定义布局命名为
item_brands
,我之前在res>layouts
看起来是这样的:然后在
getView()
在CustomAdapter
您可以使用它来获取视图和填充数据:使用
brand_image
或任何其他变量。要获得cardview外观,您可以使用以下内容:所以你得到这个: