如何从数组中填充ListView?

时间:2015-06-27 22:33:31

标签: android arraylist android-listview

我是非常新的android。我需要填充ListView。就像现在一样,我只是添加到一个数组中,然后尝试填充视图。它将从现有数据库中完成。

列表不会显示在listView小部件中。

Main.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:textColor="#0000CC"
        android:background="#E8E8F0"
        android:text="@string/textview1"
        android:id="@+id/textview1"
        android:autoText="false"/>

    <ListView
        android:layout_width="match_parent"
        android:background="#3399FF"
        android:layout_height="461dp"
        android:id="@+id/metroList"
        />
</LinearLayout> 

我的myActivity文件:

package com.bkane56.metrolink;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class MyActivity extends Activity
{
    Button button;
    TextView textView;
    ListView listView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
//        button.setOnClickListener(new MyOnClickListener(this));
        textView = (TextView) findViewById(R.id.textview1);
        listView = (ListView) findViewById(R.id.metroList);
    }
}

和我使用arrayAdapter填写的文件:

package com.bkane56.metrolink;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;

public class MetroStopList extends ListActivity {


    public void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        setContentView(R.layout.main);

        ListView metroList = (ListView) findViewById(R.id.metroList);

        ArrayList<String> metroStop = new ArrayList<String>();
            metroStop.add("LAMBERT MAIN TRML METROLINK STATION");
            metroStop.add("LAMBERT EAST TRML METROLINK STATION");
            metroStop.add("RICHMOND HEIGHTS METROLINK STATION");
            metroStop.add("CLAYTON METROLINK STATION");
            metroStop.add("BRENTWOOD METROLINK STATION");
            metroStop.add("MAPLEWOOD METROLINK STATION");
            metroStop.add("SUNNEN METROLINK STATION");
            metroStop.add("FORSYTH METROLINK STATION");
            metroStop.add("SHREWSBURY METROLINK STATION");
            metroStop.add("NORTH HANLEY METROLINK STATION");
            metroStop.add("UMSL NORTH METROLINK STATION"); 

//more of the same...the list is a total of 36


    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, metroStop);

    metroList.setAdapter(arrayAdapter);
    }

}

任何帮助将不胜感激。我相信当我走得更远时,我会有更多的问题。

1 个答案:

答案 0 :(得分:0)

我不确定这个答案对你有帮助吗?如果您认为我不理解您的问题,请告诉我,我会添加更多。

基本上,我不确定你为什么要使用两个.java类。您只能使用一个来填充ListView。此外,使用ListActivity有利于此目的,但在使用时需要注意一些事情。 Here您可以找到关于如何使用ListActivity的解释的好例子。

这是我对您的代码的实现:

<强> MainActivity.java

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ListActivity {

    private List<String> metroStop;

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

        metroStop = new ArrayList<String>();
        metroStop.add("LAMBERT MAIN TRML METROLINK STATION");
        metroStop.add("LAMBERT EAST TRML METROLINK STATION");
        metroStop.add("RICHMOND HEIGHTS METROLINK STATION");
        metroStop.add("CLAYTON METROLINK STATION");
        metroStop.add("BRENTWOOD METROLINK STATION");
        metroStop.add("MAPLEWOOD METROLINK STATION");
        metroStop.add("SUNNEN METROLINK STATION");
        metroStop.add("FORSYTH METROLINK STATION");
        metroStop.add("SHREWSBURY METROLINK STATION");
        metroStop.add("NORTH HANLEY METROLINK STATION");
        metroStop.add("UMSL NORTH METROLINK STATION");

        ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, R.layout.row_layout, R.id.listText, metroStop);
        setListAdapter(myAdapter);
    }
}

<强> activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/mainText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My list" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mainText"
        android:background="#aaaaaa" />
</RelativeLayout>

<强> row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/listText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="18sp"
        android:textStyle="bold"/>

</LinearLayout>

<强>的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.populatefromarray" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>

        </activity>

    </application>

</manifest>