我有从 sqlite 检索数据并在 BaseAdapter 中显示数据的 android 应用程序..什么是最好的方法? [英] i have android app that retrieve data from sqlite and display the data in BaseAdapter .. what is the best way ??

查看:35
本文介绍了我有从 sqlite 检索数据并在 BaseAdapter 中显示数据的 android 应用程序..什么是最好的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 android 应用程序,它从 sqlite 数据库中检索数据并将这些数据显示在一个扩展 BaseAdapter 的 listView 中.

i have an android application that retrieve data from sqlite database and display these data in a listView that extend a BaseAdapter.

在这个应用程序中,我在 drawable 文件夹中有图像,而在 sqlite 中有一个包含这些图像名称的字段.

in this app i have images in the drawable folder and i have in the sqlite a field that contain name of these images.

我的问题是如何检索这些数据并将其显示在 listView 中??

my question is how to retrieve these data and display it in a listView ??

我阅读了本教程:(http://www.androidhub4you.com/2012/09/hello-friends-today-i-am-going-to-share.html).

i read this tutorial :(http://www.androidhub4you.com/2012/09/hello-friends-today-i-am-going-to-share.html).

我的问题是有没有其他方法可以做到这一点??

my question is there any other way to do this ??

我将不胜感激.

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

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的每个行都包含很多items.在包中,不到 10 行代码.就我而言,这是最好的方法.别担心,这也是我用的.

This is the most appropriate way,especially because each listview row contains a lot of items.If you want to get the items on a clicked row it becomes a walk in the pack,less than 10 lines of code.As far as am concerned,its the best way. Don't get worried,its also what i use.

这篇关于我有从 sqlite 检索数据并在 BaseAdapter 中显示数据的 android 应用程序..什么是最好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆