1

レイアウトの下部にある「次へ」ボタンでリストの選択した値を強調表示するコードを記述しようとしています。しかし、何らかの理由で、すべてのリスト項目の後に「次へ」ボタンも表示されます。誰かがこの問題を解決するのを手伝ってくれますか?

レイアウトファイルは次のとおりです。

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

よろしくお願いします。

選択したアダプタ クラス:

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

2 に答える 2

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"
  />
于 2012-11-28T04:26:52.807 に答える
0

しかし、何らかの理由で、すべてのリスト項目の後に「次へ」ボタンも表示されます。

ListView の行レイアウトは、インフレートするgetView()か、オーバーライドしていない場合はアダプタのスーパー クラスに渡すレイアウトによって決まりますgetView()。このレイアウトを再確認し、不要なコードを削除してください。


追加
ListView のアイテムのレイアウトは、それぞれにフレーズを表示したいだけなので、1 つの TextView だけで十分です。ただし、現在メインレイアウト全体を渡しています。これにより、ボタン、未使用のListView、およびすべての行に他のすべてが作成されます...

代わりにandroid.R.layout.simple_list_item_1ingetView()を使用します。もちろん、渡す ID も変更する必要がありますfindViewById()

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が効率的なアダプターを作成して高速化する方法について話しているのを見てください。

于 2012-11-28T04:34:42.180 に答える