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

时间:2012-11-28 04:17:01

标签: android listview

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

这是布局文件:

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

Java代码:

public class updateList extends Activity {

private SelectedAdapter selectedAdapter;
private ArrayList<String> list;
int correct_answer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list = new ArrayList<String>();
    list.add("Choice One");
    list.add("Choice Two");
    list.add("Choice Three");

    selectedAdapter = new SelectedAdapter(this,0,list);
    selectedAdapter.setNotifyOnChange(true);

    ListView listview = (ListView) findViewById(R.id.listExample);
    listview.setAdapter(selectedAdapter);

    listview.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view,
                                       int position, long id) {
            // user clicked a list item, make it "selected"
            selectedAdapter.setSelectedPosition(position);
        }
    });
    }
}

提前致谢 SSP

选定的适配器类:

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

2 个答案:

答案 0 :(得分:0)

在xml中更改列表视图,如下所示

<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"
  />

答案 1 :(得分:0)

  

但由于某种原因,在每个列表项之后,“下一步”按钮也会显示出来。

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


<强>加成
ListView项目的布局只需要是一个TextView,因为您只想在每个项目中显示一个短语。但是,您当前正在传递整个主布局,这将创建按钮,一个未使用的ListView,并在每个行中覆盖其他...

而是在android.R.layout.simple_list_item_1中使用getView(),当然您还需要更改传递给findViewById()的ID:

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

请观看Android的Romain Guy讨论writing an efficient adapter以加快速度。