i have android app that retrieve data from sqlite

2019-08-31 03:58发布

问题:

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

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

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

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

i will appreciate any help .

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;
    }


}

this is what i wrote until now in the 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;
    }

}

回答1:

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.