我有Android应用程序从sqlite检索数据并在BaseAdapter中显示数据..什么是最好的方法?
作者:互联网
我有一个Android应用程序,它从sqlite数据库中检索数据,并在扩展BaseAdapter的listView中显示这些数据.
在这个应用程序中我有可绘制文件夹中的图像,我在sqlite中有一个包含这些图像的名称的字段.
我的问题是如何检索这些数据并将其显示在listView中?
我读了这个教程:(http://www.androidhub4you.com/2012/09/hello-friends-today-i-am-going-to-share.html).
我的问题还有其他办法吗?
我将不胜感激任何帮助.
row_list_match_schedulle.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Group"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Date"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textName1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignParentLeft="true"
android:text="name1" />
<TextView
android:id="@+id/textName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView2"
android:layout_alignParentRight="true"
android:text="Name2" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textLocation"
android:layout_toRightOf="@+id/textName1"
android:adjustViewBounds="true"
android:maxHeight="40dp"
android:maxWidth="40dp"
android:scaleType="fitCenter"
android:src="@drawable/algeria_flag" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView1"
android:layout_toLeftOf="@+id/textName2"
android:adjustViewBounds="true"
android:maxHeight="40dp"
android:maxWidth="40dp"
android:scaleType="fitCenter"
android:src="@drawable/algeria_flag" />
<TextView
android:id="@+id/textLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Location" />
</RelativeLayout>
ItemDetails.java
package com.devleb.expandablelistdemo3;
public class ItemDetails {
String stad_name;
String team1;
String team2;
String match_date;
String flags1;
String flags2;
String group;
public String getStad_name() {
return stad_name;
}
public void setStad_name(String stad_name) {
this.stad_name = stad_name;
}
public String getTeam1() {
return team1;
}
public void setTeam1(String team1) {
this.team1 = team1;
}
public String getTeam2() {
return team2;
}
public void setTeam2(String team2) {
this.team2 = team2;
}
public String getMatch_date() {
return match_date;
}
public void setMatch_date(String match_date) {
this.match_date = match_date;
}
public String getFlags1() {
return flags1;
}
public void setFlags1(String flags1) {
this.flags1 = flags1;
}
public String getFlags2() {
return flags2;
}
public void setFlags2(String flags2) {
this.flags2 = flags2;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
}
这是我在BaseAdapter中直到现在所写的内容
CustomAdapterMatchSchedule.java
package com.devleb.expandablelistdemo3;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class CustomAdapterMatchSchedule extends BaseAdapter {
LayoutInflater layoutInflater;
ArrayList<ItemDetails> itemdetailList;
Context context;
public CustomAdapterMatchSchedule(Context c, ArrayList<ItemDetails> listOfItem){
this.context = c;
itemdetailList = listOfItem;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return itemdetailList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return itemdetailList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
ItemDetails itemListDetails = itemdetailList.get(position);
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_list_match_schedule, null);
}
//Stad_name
TextView txtStadName = (TextView)convertView.findViewById(R.id.textLocation);
txtStadName.setText(itemListDetails.getStad_name());
//team1
TextView txtTeam1 = (TextView)convertView.findViewById(R.id.textName1);
txtTeam1.setText(itemListDetails.getTeam1());
//team2
TextView txtTeam2 = (TextView)convertView.findViewById(R.id.textName2);
txtTeam2.setText(itemListDetails.getTeam2());
//flag1
TextView txtflag1 = (TextView)convertView.findViewById(R.id.imageView1);
txtflag1.setText(itemListDetails.getTeam1());
//flag2
return null;
}
}
解决方法:
这是最合适的方式,特别是因为每个listview行包含很多项目.如果你想在一个被点击的行上获取项目,它就变成了一个步骤,不到10行代码.就我而言,这是最好的方式.不要担心,它也是我用的.
标签:android,android-listview,android-sqlite,baseadapter 来源: https://codeday.me/bug/20190926/1820272.html