Android:按钮在列表视图中多次显示

Android:按钮在列表视图中多次显示,android,listview,Android,Listview,我试图编写代码,用布局底部的“下一步”按钮突出显示列表的选定值。但由于某些原因,在每个列表项之后,下一步按钮也会出现。有人能帮我解决这个问题吗 以下是布局文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

我试图编写代码,用布局底部的“下一步”按钮突出显示列表的选定值。但由于某些原因,在每个列表项之后,下一步按钮也会出现。有人能帮我解决这个问题吗

以下是布局文件:

<LinearLayout 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:orientation="vertical"
    android:id="@+id/questionLayout"
    >

    <TextView android:id="@+id/txtExample"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:textColor="#000000"
        android:background="#FF0000"
        />

    <ListView
        android:id="@+id/listExample"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#CCCCCC"
        android:choiceMode="singleChoice"
        />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal">

        <Button
             android:id = "@+id/next"
             android:text="Next"
             android:layout_width = "wrap_content"
             android:layout_height = "wrap_content"
             android:layout_gravity="center"
             android:layout_weight="50"
        />
        <Button
             android:id = "@+id/submit"
             android:text="Submit"
             android:layout_width = "0dp"
             android:layout_height = "wrap_content"
             android:layout_weight="50"
             android:layout_gravity="center"
        />

    </LinearLayout>

</LinearLayout>

在xml中更改listview,如下所示

<ListView
android:id="@+id/listExample"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"//===== set maximum heighthere
android:layout_marginBottom="50dp"//  === give some space at bottom so that buttons will appear
android:background="#CCCCCC"
android:choiceMode="singleChoice"
  />
但由于某些原因,在每个列表项之后,下一步按钮也会出现

ListView的行布局由您在getView中膨胀的布局决定,如果您没有覆盖getView,则传递给适配器的超类。仔细检查此布局并删除不需要的代码

加成 ListView项目的布局只需要一个TextView,因为您只想在每个文本视图中显示一个短语。无论您当前传递的是整个主布局,这都会在每一行中创建按钮、未使用的ListView和其他所有内容

取而代之的是在getView中使用android.R.layout.simple_list_item_1,当然您还需要更改传递给findViewById的id:


请观看Android的Romain Guy讨论如何加快速度。

我想知道您是否将listView高度设置为Android:layout\u height=fill\u parent。然后将如何显示下面添加的其他布局。请确认您的代码。将其更改为wrap\u content,但仍然可以看到相同的结果。@sathyapriya使用BaseAdapter而不是ArrayAdapter,并使用自定义listView在你的应用程序中。当我尝试你的建议时,没有一个选项出现,但按钮只显示了一次。删除布局将其转换为以前的格式Hi Sam,我已将getView代码添加到原始帖子中。有没有办法在getView中以编程方式添加按钮,我在谷歌上搜索了一下,但找不到任何代码。我更新了答案。这将阻止按钮在每一行中显示。祝你好运你好,山姆,谢谢你的帮助,我明天会试用,并会更新帖子
public class SelectedAdapter extends ArrayAdapter{

// used to keep selected position in ListView
private int selectedPos = -1;   // init value for not-selected

public SelectedAdapter(Context context, int textViewResourceId,
                   List objects) {
    super(context, textViewResourceId, objects);
}

public void setSelectedPosition(int pos){
    selectedPos = pos;
    // inform the view of this change
    notifyDataSetChanged();
}

public int getSelectedPosition(){
    return selectedPos;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    // only inflate the view if it's null
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.activity_main, null);
    }

    // get text view
    TextView label = (TextView)v.findViewById(R.id.txtExample);

    // change the row color based on selected state
    if(selectedPos == position){
        label.setBackgroundColor(Color.CYAN);
    }else{
        label.setBackgroundColor(Color.WHITE);
    }

    label.setText(this.getItem(position).toString());

    /*
    // to use something other than .toString()
    MyClass myobj = (MyClass)this.getItem(position);
    label.setText(myobj.myReturnsString());
    */
    return(v);
}
}
<ListView
android:id="@+id/listExample"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"//===== set maximum heighthere
android:layout_marginBottom="50dp"//  === give some space at bottom so that buttons will appear
android:background="#CCCCCC"
android:choiceMode="singleChoice"
  />
if (v == null) {
    LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = vi.inflate(android.R.layout.simple_list_item_1, null);
}

// get text view
TextView label = (TextView)v.findViewById(android.R.id.text1);