2011-09-19 99 views
1

我的屏幕需求就像在顶部中间有一个标题栏,然后是列表视图,当我试图做的时候,我无法做到像我每个州都有州名列表item.I只要在顶部和一次。而且它的屏幕截图是 screen shot无法为Listview添加标题栏

<?xml version="1.0" encoding="utf-8"?> 
    <TableLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
     > 
     <TableRow> 
     <TextView 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:background="#000000" 
     android:text="Select State" 

     ></TextView> 
     </TableRow> 
     <TableRow> 
     <TextView android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 

      android:id="@+id/label" 
      android:textSize="30px"></TextView> 
    </TableRow> 
    </TableLayout> 

我已经尝试了很多方法,这是我目前的布局

和我的Java代码

import android.app.ListActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

public class new21 extends ListActivity { 
    /** Called when the activity is first created. */ 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     // Create an array of Strings, that will be put to our ListActivity 
     String[] names = new String[] { "Andhra Pradesh", "Kerala","Tamilnadu","Karnataka" }; 
     // Use your own layout and point the adapter to the UI elements which 
     // contains the label 
     this.setListAdapter(new ArrayAdapter<String>(this, R.layout.new21, 
       R.id.label, names)); 
    } 

    @Override 

    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 
     // Get the item that was clicked 
     Object o = this.getListAdapter().getItem(position); 
     String keyword = o.toString(); 
     if(keyword.equals("Andhra Pradesh")) 
     { 
      Intent ima45=new Intent(new21.this,new31.class); 
      startActivity(ima45); 

     } 
     Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG) 
       .show(); 

    } 

} 

任何一个可以帮助我。

+0

请更新您的new21.xml整个 –

回答

3

可以的AddHeader像这样ListView控件,

View header = getLayoutInflater().inflate(R.layout.header, null); 
ListView listView = getListView(); 
listView.addHeaderView(header); 
setListAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_single_choice, names)); 

哪里R.layout.header是包含有文本TextView布局"Select State"

更新:

public class ListViewProblemActivity extends ListActivity { 

    ListView listView; 

    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     // Create an array of Strings, that will be put to our ListActivity 
     String[] names = new String[] { "Andhra Pradesh", "Kerala","Tamilnadu","Karnataka" }; 
     // Use your own layout and point the adapter to the UI elements which 
     // contains the label 

     TextView tv = new TextView(getApplicationContext()); 
     tv.setText("Select State"); 

     listView = getListView(); 
     listView.addHeaderView(tv); 
     this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,names)); 
    } 

    @Override 

    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 
     // Get the item that was clicked 

     if(position != 0){ 
      System.out.println(position - 1); 
      Object o = this.getListAdapter().getItem(position - 1); 
      String keyword = o.toString(); 
      Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG) 
        .show(); 
     } 
    } 
} 
2

Usey只有一个ListView在您的活动中,删除标题回覆。然后使用ListView.addHeaderViwe();将标题添加到Listview。此标题显示在第一个项目之前。 查看documentaton了解更多信息。

0

你完全错了。只需要一个带有一个TextView(用于标题栏)和ListView(用于显示列表视图)的LinearLayout,无非就是这两个视图。

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

    <TextView 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:background="#000000" 
     android:text="Select State"> 
     </TextView> 

     <ListView 
     android:id="@+id/lviewState" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent"> 
     </ListView> 

</LinearLayout> 

现在,按照您的代码显示ListView。

0

你最好使用ListView.addHeaderViwe();

但不要忘记删除什么ü没有“扩展ListActivity”,没有必要的,

,并从你的“new21.xml” 在XML中删除标题,刚修好的文本项û想要显示在列表的每一行,