使用arrayadapter显示listview中数据库的数据

时间:2014-02-08 09:13:50

标签: android sqlite listview cursor android-arrayadapter

Hy guys!

我遇到了问题。我的应用应该在列表视图中显示所有路由。但是arrayadapter有问题。如果我像这样尝试我的arrayadapter:

ArrayAdapter<DefineRoute> adapter = new ArrayAdapter<DefineRoute>(
                this, android.R.layout.simple_list_item_1,verbindungen.getVerbindungen());

它可以工作,但它只显示DefineRoute的对象名,我想显示光标的输出。 我想我应该尝试:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_list_item_1,verbindungen.getVerbindungen());

但是出现了错误:无法解析构造函数ArrayAdapter

这是我的Acticity:

  @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated constructor stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.planausgabelayout);

            //Aufruf der TextViews
            TextView txtStart = (TextView)findViewById(R.id.txtAusgabeStart);
            TextView txtZiel = (TextView)findViewById(R.id.txtAusgabeZiel);
            TextView txtZeit = (TextView)findViewById(R.id.txtAusgabeZeit);


            intent = getIntent();

            txtStart.setText(intent.getStringExtra("StartHaltestelle"));
            txtZiel.setText(intent.getStringExtra("ZielHaltestelle"));
            txtZeit.setText(intent.getStringExtra("Zeit"));

            getRoute();
        }

        public void getRoute() {
            lvList = (ListView)findViewById(R.id.lvList);
            Verbindungen verbindungen = new Verbindungen(this);

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    this, android.R.layout.simple_list_item_1,verbindungen.getVerbindungen());

            lvList.setAdapter(adapter);
        }

这是我的活动定义路线:

public class DefineRoute {
    private String abfahrtszeit;
    private String ankunftszeit;
    private String dauer;
    private String umstieg;

    public DefineRoute(String abfahrtszeit, String ankunftszeit, String dauer, String umstieg)
    {
        this.abfahrtszeit = getAbfahrtszeit();
        this.ankunftszeit = getAnkunftszeit();
        this.dauer = getDauer();
        this.umstieg = getUmstieg();
    }

    public String getAbfahrtszeit() {
        return abfahrtszeit;
    }

    public String getAnkunftszeit() {
        return  ankunftszeit;
    }

    public String getDauer() {
        return dauer;
    }

    public String getUmstieg() {
        return umstieg;
    }
}

这是我的Activity Verbindungen:

public class Verbindungen {
    SQLiteDatabase db;
    LinkedList<DefineRoute> route;
    DefineRoute[] routeArray;
    Context context;

    DatabaseHelper myDbHelper = null;

    public Verbindungen(Context context) {
        route = new LinkedList<DefineRoute>();
        this.context = context;
        myDbHelper = new DatabaseHelper(context);
    }

    public DefineRoute[] getVerbindungen() {

        try {

            myDbHelper.createDataBase();

        } catch (IOException ioe) {

            throw new Error("Unable to create database");
        }
        try {

            myDbHelper.openDataBase();

        }catch(SQLException sqle){

            throw sqle;

        }

        db = myDbHelper.getReadableDatabase();
        // Alle Daten der Datenbank abrufen mithilfe eines Cursors
        Cursor cursor = db.rawQuery("SELECT strftime('%H:%M', f.abfahrt) AS Abfahrt," +
                "strftime('%H:%M', f.ankunft) AS Ankunft," +
                "strftime('%H:%M', strftime('%s',f.ankunft)- strftime('%s',f.abfahrt), 'unixepoch') AS Dauer," +
                "r.name AS Route," +
                "count(u.fahrt_id) AS Umstiege " +
                "FROM scotty_fahrt f " +
                "JOIN scotty_haltestelle start ON f.start_id = start.id " +
                "JOIN scotty_haltestelle ziel ON f.ziel_id = ziel.id " +
                "JOIN scotty_route r ON f.route_id = r.id " +
                "LEFT OUTER JOIN scotty_umstiegsstelle u ON f.id = u.fahrt_id " +
                "WHERE start.name = 'Linz/Donau Hbf (Busterminal)' " +
                "AND ziel.name = 'Neufelden Busterminal (Schulzentrum)' " +
                "GROUP BY u.fahrt_id",null);

        cursor.moveToFirst();
        int i=0;
        while (cursor.moveToNext()){
            //in this string we get the record for each row from the column "name"
            i++;
        }
        routeArray = new DefineRoute[i];
        cursor.moveToFirst();
        int k =0;
        while (cursor.moveToNext())
        {
            routeArray[k] = new DefineRoute(cursor.getString(0),cursor.getString(1),cursor.getString(2),
                    cursor.getString(3));
            k++;

        }
        //here we close the cursor because we do not longer need it
        //}
        cursor.close();
        myDbHelper.close();

        return routeArray;

    }

请帮帮我。

现在我正在创建一个ArrayAdapter类,我在列表视图中定义了我的输出:

public class RouteAdapter extends ArrayAdapter<DefineRoute>{
    Activity context;
    DefineRoute[] defineroute;

    public RouteAdapter(Activity context, DefineRoute[] defineroute){
        super(context, R.layout.layoutausgabe, defineroute);
        this.defineroute = defineroute;
        this.context = context;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View row = inflater.inflate(R.layout.layoutausgabe,null);
        TextView txZeit = (TextView)row.findViewById(R.id.txZeit);
        TextView txDauer = (TextView)row.findViewById(R.id.txDauer);
        TextView txUmstieg = (TextView)row.findViewById(R.id.txUmstieg);

        DefineRoute defineRoute = defineroute[position];

        txZeit.setText(defineRoute.getAbfahrtszeit() + " - " + defineRoute.getAnkunftszeit());
        txDauer.setText(defineRoute.getDauer());
        txUmstieg.setText(defineRoute.getUmstieg());
        return row;
    }
}

我该怎么办? 我的适配器应该是什么样的?

2 个答案:

答案 0 :(得分:0)

您的ArrayAdapter<String>类型为String,因此将String列表传递给它的构造函数而不是verbindungen.getVerbindungen()对象列表。

答案 1 :(得分:0)

ArrarAdapter&lt; T>是您可以使用的任何类型的课程

在您的情况下ArrayAdapter,因此您需要覆盖DefineRoute类

的toString方法

在你的情况下

 @Override
     public String toString() {
     return ankunftszeit+" "+ankunftszeit;
//or what ever you want to displat
     }

或者还有其他解决方案是通过BaseAdapter类创建自己的适配器。