2015-06-27 70 views
1

我很新入android。我需要填充一个ListView。现在,我只是将其添加到数组中,然后尝试填充视图。它将完全从现有的数据库完成。如何从数组中填充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); 
    } 

} 

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

+0

请解释一下你所看到的,当你运行你的应用程序,它从你想要什么不同之处。另外,请显示你的'AndroidManifest.xml'文件。 –

回答

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>