how to add custom adapter to the activity to make

2019-09-01 09:39发布

i have an android activity that use sqlite database and listview that extend baseAdapter.

what i need is to display the data included in the sqlite to be displayed in the list view.

the sqlite contain name of images that exist in the drawable folder.

so my question how to use this listView that extend Baseadapter after getting data

in the activity??

can anyone help me ??

ItemDetails.java

package com.devleb.expandablelistdemo3;

public class ItemDetails {

    String Name;
    int image;
    String stad_name;
    String team1;
    String team2;
    String flag1;
    String flag2;
    String Date_match;
    String group;



    public String getGroup() {
        return group;
    }
    public void setGroup(String group) {
        this.group = 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 getFlag1() {
        return flag1;
    }
    public void setFlag1(String flag1) {
        this.flag1 = flag1;
    }
    public String getFlag2() {
        return flag2;
    }
    public void setFlag2(String flag2) {
        this.flag2 = flag2;
    }
    public String getDate_match() {
        return Date_match;
    }
    public void setDate_match(String date_match) {
        Date_match = date_match;
    }
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    public int getImage() {
        return image;
    }
    public void setImage(int image) {
        this.image = image;
    }


}

CustomAdapter.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.ImageView;
import android.widget.TextView;

public class CustomAdapterMatchSchedule extends BaseAdapter {
    ArrayList<ItemDetails> itemdetailsList;
    Context context;

    public CustomAdapterMatchSchedule(Context context,
            ArrayList<ItemDetails> list) {

        this.context = context;
        itemdetailsList = list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return itemdetailsList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return itemdetailsList.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 itemdetail = itemdetailsList.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(itemdetail.getStad_name());
        // team1
        TextView txtTeam1 = (TextView) convertView.findViewById(R.id.textName1);
        txtTeam1.setText(itemdetail.getTeam1());
        // team2
        TextView txtTeam2 = (TextView) convertView.findViewById(R.id.textName2);
        txtTeam2.setText(itemdetail.getTeam2());

        // flag1
        int imageid = context.getResources().getIdentifier("brazil_flag",
                "drawable", context.getPackageName());
        ImageView imagenow = (ImageView) convertView
                .findViewById(R.id.imageView1);
        imagenow.setImageResource(imageid);

        // flag2
        int imageid2 = context.getResources().getIdentifier("croatian_flag",
                "drawable", context.getPackageName());
        ImageView imagenow2 = (ImageView) convertView
                .findViewById(R.id.imageView2);
        imagenow.setImageResource(imageid2);

        // match_date
        TextView txtmatch_date = (TextView) convertView
                .findViewById(R.id.txtDate);
        txtmatch_date.setText(itemdetail.getDate_match());

        // group
        TextView txtGroup = (TextView) convertView.findViewById(R.id.textGroup);
        txtGroup.setText(itemdetail.getGroup());

        return convertView;
    }

}

logCat

04-28 09:36:43.109: E/SQLiteLog(3465): (14) cannot open file at line 30191 of [00bb9c9ce4]
04-28 09:36:43.109: E/SQLiteLog(3465): (14) os_unix.c:30191: (2) open(//data//data//com.devleb.expandablelistdemo3//databases//world_cup.db) - 
04-28 09:36:43.117: E/SQLiteDatabase(3465): Failed to open database '//data//data//com.devleb.expandablelistdemo3//databases//world_cup.db'.
04-28 09:36:43.117: E/SQLiteDatabase(3465): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at com.devleb.expandablelistdemo3.ExternalDbOpenHelper.checkDataBase(ExternalDbOpenHelper.java:71)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at com.devleb.expandablelistdemo3.ExternalDbOpenHelper.createDataBase(ExternalDbOpenHelper.java:53)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at com.devleb.expandablelistdemo3.ExternalDbOpenHelper.openDataBase(ExternalDbOpenHelper.java:104)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at com.devleb.expandablelistdemo3.ExternalDbOpenHelper.<init>(ExternalDbOpenHelper.java:49)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at com.devleb.expandablelistdemo3.MatchScheduleList.onCreate(MatchScheduleList.java:71)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at android.app.Activity.performCreate(Activity.java:5133)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at android.os.Looper.loop(Looper.java:137)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at android.app.ActivityThread.main(ActivityThread.java:5103)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at java.lang.reflect.Method.invokeNative(Native Method)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at java.lang.reflect.Method.invoke(Method.java:525)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-28 09:36:43.117: E/SQLiteDatabase(3465):     at dalvik.system.NativeStart.main(Native Method)
04-28 09:36:43.117: E/class com.devleb.expandablelistdemo3.ExternalDbOpenHelper(3465): Error while checking db

MatchScheduleList.java

this activity must extend Activity here what i must write code to make it work

package com.devleb.expandablelistdemo3;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MatchScheduleList extends Activity {

    private static final String DB_NAME = "world_cup.db";
    // *****Tables name**************************//
    private static final String TABLE_NAME = "match_list";

    // *****Tbale name******************************//

    public static final String PLACE_ID = "_id";
    public static final String STAD_NAME = "stad_name";
    public static final String TEAM1 = "team1";
    public static final String TEAM2 = "team2";
    public static final String STAGE = "stage";
    public static final String MATCH_DATE = "match_date";
    public static final String GROUP = "group_team";
    public static final String FLAG1 = "flags1";
    public static final String FLAG2 = "flags2";

    public static final String[] ALL_KEYS = new String[] { PLACE_ID, STAD_NAME,
            TEAM1, TEAM2, STAGE, MATCH_DATE, FLAG1, FLAG2, GROUP };
    // *****Tbale name******************************//

    private SQLiteDatabase database;
    private ListView myList;
    private ArrayList<ItemDetails> list = new ArrayList<ItemDetails>();

    private ExternalDbOpenHelper extDB;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_match_schedule_list);

        ExternalDbOpenHelper extDB = new ExternalDbOpenHelper(this, DB_NAME);
        database = extDB.openDataBase();


        populateLitsFromDB();
    }

    public Cursor getAllRows() {
        String where = null;

        Cursor c = database.query(true, TABLE_NAME, ALL_KEYS, where, null,
                null, null, null, null);
        if (c != null) {
            c.moveToFirst();

        }

        return c;

    }

    private void populateLitsFromDB() {
        // TODO Auto-generated method stub

        Cursor cursor = getAllRows();

        // allo0w activity to manage life cicle of the cursor

        startManagingCursor(cursor);

        CustomAdapterMatchSchedule customAdapter = new CustomAdapterMatchSchedule(
                this, list);

        Log.d("in the getAllRows", customAdapter.toString());

        // set the adapter for te listView
        myList = (ListView) findViewById(R.id.list);
        myList.setAdapter(customAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.match_schedule_list, menu);
        return true;
    }

}

4条回答
太酷不给撩
2楼-- · 2019-09-01 10:04

You need to make one global ArrayList for ItemDetails class. Like,

ArrayList<ItemDetails> itemdetailsList = new ArrayList<ItemDetails>();

after that pass this arraylist to your custom adapter. Like,

CustomAdapterMatchSchedule adapter = new CustomAdapterMatchSchedule(getActivity(), itemdetailsList);

and after that set that adapter to ListView.

listview.setAdapter(adapter);
查看更多
Bombasti
3楼-- · 2019-09-01 10:05

Simpley create an instance of your adapter in your activity or your fragment and call to:

 YourAdapter adapter = new YourAdapter(getActivity(), yourItems);
 listView.setAdapter(yourAdapter)

for examples please refer to:

link 1

link 2

link 3

查看更多
甜甜的少女心
4楼-- · 2019-09-01 10:07

Simpley create an instance of your adapter in your activity

MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(YourActvity.this,
                items);
listview.setAdapter(adapter);
查看更多
叼着烟拽天下
5楼-- · 2019-09-01 10:19

Way to fetch data from cursor and then passing that arraylist to your custom adapter. then finally bind adapter to listview.

startManagingCursor(cursor);

ArrayList<ItemDetails> itemdetailsList = new ArrayList<ItemDetails>();
ItemDetails objItem;
cursor.moveToFirst();

while (!cursor.isAfterLast()) {
    objItem = new ItemDetails();

    stad_name = cursor.getString(cursor.getColumnIndex("stad_name_FIELDNAME"));
    team1 = cursor.getString(cursor.getColumnIndex("team1_FIELDNAME"));
    . . .

    itemdetailsList.add(objItem);

    cursor.moveToNext();
}

MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(YourActvity.this,
                itemdetailsList);
listview.setAdapter(adapter);
查看更多
登录 后发表回答