gpt4 book ai didi

android - 无法为 ListView 添加标题栏

转载 作者:搜寻专家 更新时间:2023-11-01 07:38:23 25 4
gpt4 key购买 nike

我的屏幕要求就像在顶部中间有一个标题栏,然后是 ListView ,当我试图这样做时我无法做到,就像我在每个列表项中都有州名一样。我只希望在顶部和一次。它的屏幕截图是 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>

我试过很多方法这是我现在的布局

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

}

}

谁能帮帮我。

最佳答案

你可以像这样将Header添加到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();
}
}
}

关于android - 无法为 ListView 添加标题栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7468287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com