如何向活动添加自定义适配器以使列表出现在活动中?

时间:2014-04-28 09:02:33

标签: android android-listview android-sqlite baseadapter

我有一个使用sqlite数据库和listview扩展baseAdapter的android活动。

我需要的是显示sqlite中包含的数据,以便在列表视图中显示。

sqlite包含可绘制文件夹中存在的图像名称。

所以我的问题是如何使用这个listView在获取数据后扩展Baseadapter

活动中的

??

谁能帮助我?

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

此活动必须扩展活动 这里我必须编写代码才能使其正常工作

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 个答案:

答案 0 :(得分:0)

Simpley在您的活动或片段中创建适配器的实例,并致电:

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

有关示例,请参阅:

link 1

link 2

link 3

答案 1 :(得分:0)

您需要为ArrayList课程制作一个全局ItemDetails。像,

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

之后将此arraylist传递给您的自定义适配器。像,

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

然后将该适配器设置为ListView

listview.setAdapter(adapter);

答案 2 :(得分:0)

Simpley在您的活动中创建适配器的实例

MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(YourActvity.this,
                items);
listview.setAdapter(adapter);

答案 3 :(得分:0)

从光标获取数据然后将该arraylist传递给自定义适配器的方法。然后最终将适配器绑定到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);