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

Posted

技术标签:

【中文标题】如何将自定义适配器添加到活动中以使列表出现在活动中?【英文标题】:how to add custom adapter to the activity to make the list appear in the activity? 【发布时间】:2014-06-13 17:54:43 【问题描述】:

我有一个使用 sqlite 数据库和扩展 baseAdapter 的列表视图的 android 活动。

我需要将包含在sqlite中的数据显示在列表视图中。

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

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

在活动中??

谁能帮帮我??

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;
    


日志猫

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;
    


【问题讨论】:

这段代码有什么问题?你有什么错误吗? 代码看起来差不多,有什么问题? 这里没有问题...我的问题是,使用第三个活动 MatchScheduleList 的代码来显示此列表的最佳方法是什么 将您的适配器与列表视图绑定。这将满足您的需要。检查 SacreDeveloper 给出的答案。 【参考方案1】:

从光标获取数据然后将该数组列表传递给您的自定义适配器的方法。然后最后将适配器绑定到列表视图。

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

【讨论】:

【参考方案2】:

Simpley 在您的活动或片段中创建适配器实例并调用:

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

示例请参考:

link 1

link 2

link 3

【讨论】:

【参考方案3】:

简单地在你的活动中创建你的适配器实例

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

【讨论】:

【参考方案4】:

您需要为ItemDetails 类创建一个全局ArrayList。喜欢,

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

之后将此数组列表传递给您的自定义适配器。喜欢,

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

然后将该适配器设置为ListView

listview.setAdapter(adapter);

【讨论】:

没有你的数组列表。再次检查答案。 我将编辑我的问题并添加第三个活动,谁能告诉我错误在哪里?? 但是在您的活动中,您没有在列表中添加任何数据,因此它将为空 方法populateLitsFromDB() 不会将数据添加到列表中??怎么办?? 检查我的答案以添加从光标获取的数据。

以上是关于如何将自定义适配器添加到活动中以使列表出现在活动中?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用自定义适配器将整数添加到列表视图

将自定义小部件添加到 Flutter TabBar 的活动选项卡

如何使用来自另一个活动的自定义适配器更新列表视图?

如何向 SFSafariViewController 添加自定义活动?

将自定义ListView项目传递给Android中的其他活动

Android将自定义对象从服务传递给活动