私は動的に追加する必要があるプロジェクトにも取り組んでいTextView
ますSpinner
。プログラムからこれら2つを動的に追加することができました。
スピナーでいくつかのアイテムを選択しようとすると、そのアイテムはエミュレーターに表示されませんが、選択したアイテムはトーストに表示されます。
そのアイテムをで選択させるために何かする必要がありますSpinner
か?
for (Map.Entry<String, String> entry : mapColumns.entrySet()) {
spinnerArray = new ArrayList<String>();
final TextView rowTextView = new TextView(cont);
final Spinner spinner = new Spinner(cont);
rowTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
spinner.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
for(String s: entry.getValue().split(",")) {
System.out.println(s);
s = s.replaceAll("[^a-zA-Z0-9]+","");
spinnerArray.add(s);
}
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(cont, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
rowTextView.setText(entry.getKey());
rowTextView.setTypeface(null, Typeface.BOLD);
spinner.setAdapter(spinnerArrayAdapter);
// add the listener
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
layout.addView(rowTextView);
layout.addView(spinner);
}
class CustomOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
Toast.makeText(
parent.getContext(),
"OnItemSelectedListener : "
+ parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
以下は私のXMLレイアウトです-
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="100px"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal|center"
android:gravity="center_vertical|center_horizontal"
android:text="Save" />
</LinearLayout>
</ScrollView>
ここでmapColumns
は、キーと値のペアを作成します。したがって、スピナーでは、すべてのアイテムがそのマップの値から表示されます。
問題文:-
ここで、スピナーで誰かがアイテムを選択している場合は、それが選択されて他の人に表示されるようにする必要があります。
以下は私がアイテムを選択した画像ですが、表示されてSpinner
おらず、TextView
色も非常に薄いです-