无法为列表视图添加标题栏

发布于 2024-12-05 04:35:52 字数 2234 浏览 1 评论 0原文

我的屏幕的要求就像在顶部中间有一个标题栏,然后是列表视图,当我尝试这样做时,我无法像我在每个列表项中都有状态名称一样。我只希望在顶部和一次。它的屏幕截图是 屏幕截图

 
    <表格布局
      xmlns:android="http://schemas.android.com/apk/res/android"
      安卓:layout_width =“fill_parent”
     安卓:layout_height =“fill_parent”
        >
      <表行>
      <文本视图 
         安卓:layout_height =“wrap_content”
         安卓:layout_width =“fill_parent”
         机器人:背景=“#000000”
         android:text="选择状态"
                
      >
      
      <表行> 
      
    
    

我尝试了很多方法,这是我现在的布局

,我的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();
        
    }

}

任何人都可以帮助我吗?

The requirement for my screen is like having a title bar at the top middle and followed by the list view when am trying to do that am not able to do like i got state name in every list item.I want that only in the top and once. And the screen shot of it is
screen shot

 <?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>

I have tried many ways this is my present layout

and my java code is

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();
        
    }

}

Can any one help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

み格子的夏天 2024-12-12 04:35:52

您可以像这样向 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 是包含带有文本 "Select State" TextView 的布局

<强>更新:

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();
        }
    }
}

You can addHeader to the ListView like this,

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));

Where R.layout.header is the layout that contains a TextView with text "Select State"

UPDATED:

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();
        }
    }
}
俏︾媚 2024-12-12 04:35:52

您的活动中仅使用 ListView,请删除那里的标题。然后使用ListView.addHeaderViwe();向Listview添加标题。该标题显示在第一项之前。
有关更多信息,请参阅文档信息。

Usey only a ListView in your activity, remove the heading there. Then use ListView.addHeaderViwe(); to add a header to the Listview. This Header is display before the first item.
See the documentaton for further information.

っ〆星空下的拥抱 2024-12-12 04:35:52

你最好使用 ListView.addHeaderViwe();

但不要忘记删除您所做的“扩展 ListActivity”,不需要这样做,

并从“new21.xml”中删除标题
在您的 xml 中,只需修复您想要在列表的每一行中显示的文本项,

you better use ListView.addHeaderViwe();

but don't forget to remove what u did "extends ListActivity", no need for that,

and also remove title from your "new21.xml"
in your xml, just fix the text item u want to show in every row of the list,

夏日落 2024-12-12 04:35:52

你做的完全错误。就拿一个 LinearLayout 加上一个 TextView(用于标题栏)和 ListView(用于显示列表视图),无非就是这 2 个视图。

<?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。

You are doing fully wrong. Just take a LinearLayout with one TextView (for Title bar) and ListView (To display listview), nothing more than these 2 views.

<?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>

Now, follow your code to display ListView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文