2013-12-23 80 views
1

我昨天開始爲Android開發,所以我是一個完整的新手。 我正在使用適用於Eclipse的ADT包。 我想要做的是我有一串字符串。我想在滾動支持的視圖中顯示它們。我發現ListView可以做到這一點,但我發現的所有教程都非常複雜,並且解釋了嵌套視圖。在Android中顯示字符串列表

我試過使用ListView但我什至不能看到虛擬視圖。 這是我曾嘗試: activity_main.xml中:

<ListView 
     android:id="@+id/wifiScanResList" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/wifiScanButton" > 
</ListView> 
在MainActivity類別

private ListView wifiListView; 

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

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 

    getMenuInflater().inflate(R.menu.main, menu); 
    wifiListView = (ListView) findViewById(R.id.wifiScanResList); 
    wifiListView.setEnabled(true); 

    return true; 
} 

我有問候兩個問題我的問題了。

第一個是,我是否正確地使用了我的需求?如果不是,我應該使用什麼?

第二個是,虛擬視圖沒有出現的原因是什麼?我怎樣才能簡單地將字符串添加到ListView?

+0

爲什麼會有一個虛擬目錄? – njzk2

回答

5

首先你需要創建一個ArrayList來存儲所有的字符串。

String[] values = new String[] { "Android", "iPhone", "WindowsMobile", 
    "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", 
    "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", 
    "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2", 
    "Android", "iPhone", "WindowsMobile" }; 

final ArrayList<String> list = new ArrayList<String>(); 
for (int i = 0; i < values.length; ++i) { 
    list.add(values[i]); 
} 

那麼你就需要從這個數組列表創建一個適配器

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

那麼你就需要設置列表視圖此適配器

listview.setAdapter(adapter); 

請訪問以下鏈接爲一個簡單的列表視圖執行:

http://androidexample.com/Create_A_Simple_Listview_-_Android_Example/index.php?view=article_discription&aid=65&aaid=90

+0

Err爲什麼你在使用CursorAdapter時顯然需要ArrayAdapter? – dymmeh

+0

@對不起。編輯 – gaurav5430

+0

上面的答案應該有所幫助。您沒有使用任何適配器,因此該列表不會顯示。以這個鏈接爲例,它非常簡單,它與你所問的內容相同。用字符串簡單的列表視圖。 www.ezzylearning.com/tutorial.aspx?tid=1659127 –

2

這是我顯示列表的簡單方法。

1)創建一個名爲MySimpleArrayAdapter

public class MySimpleArrayAdapter extends ArrayAdapter<String> { 
    private final Context context; 
    private final ArrayList<String> values; 

    public MySimpleArrayAdapter(Context context, ArrayList<String> values) { 
     super(context, R.layout.rowlayout, values); 
     this.context = context; 
     this.values = values; 
    } 

    @Override 
    public void notifyDataSetChanged() { 
     // TODO Auto-generated method stub 
     super.notifyDataSetChanged(); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View rowView = null; 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     rowView = inflater.inflate(R.layout.rowlayout, parent, false); 

     // Displaying a textview 
     TextView textView = (TextView) rowView.findViewById(R.id.label); 
     textView.setText(values.get(position)); 


     return rowView; 
    } 

2)類中創建名爲rowlayout.xml佈局的XML文件。我們有textview,因爲 我們想顯示項目,但你可以顯示一個圖像的項目。使用ImageView的標籤

<?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="wrap_content" > 

    <TextView 
     android:id="@+id/label" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@+id/label" 
     android:textSize="30sp" > 
    </TextView> 


</LinearLayout> 

3)返回到主活動的xml文件(activity_main)。我們有你的wifiscan列表

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearlayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <ListView 
     android:id="@+id/wifiScanResList" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/wifiScanButton" > 

</ListView> 
</LinearLayout> 

4)最後,在主要活動,我們適配器設置爲您的列表

public class MainActivity extends Activity { 

    // The adapter that we gonna use 
    MySimpleArrayAdapter adapter; 

    // List of wifi results 
    ArrayList<String> wifi_results= new ArrayList<String>(); 

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

     adapter = new MySimpleArrayAdapter(this, wifi_results); 
     setListAdapter(adapter); 

     getListView().setOnItemClickListener(new OnItemClickListener() { 
     @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) { 

      // DO something if the user clicked on the item 
     } 
      }); 
    } 

} 
相關問題