MainActivity.java
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
Spinner java_spinner;
Spinner java_spinner2;
String mytext;
String mytext2;
TextView choosen_text;
TextView choosen_text2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
java_spinner = (Spinner)findViewById(R.id. spinner);
java_spinner.setOnItemSelectedListener(this);
java_spinner2 = (Spinner)findViewById(R.id. spinner_number_of_rounds);
java_spinner2.setOnItemSelectedListener(this);
ListView lv = (ListView) findViewById(R.id.listView);
generateListContent();
lv.setAdapter(new MyListAdaper(this, R.layout.custom_layout, data));
}
private ArrayList<String> data = new ArrayList<String>();
private void generateListContent() {
for(int i = 0; i < 55; i++) {
data.add("This is row number " + i);
}
}
private class MyListAdaper extends ArrayAdapter<String> {
private int layout;
private List<String> mObjects;
private MyListAdaper(Context context, int resource, List<String> objects) {
super(context, resource, objects);
mObjects = objects;
layout = resource;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainViewholder = null;
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.itemImage);
viewHolder.title = (TextView) convertView.findViewById(R.id.listView);
viewHolder.button = (Button) convertView.findViewById(R.id.playbutton);
viewHolder.button.setTag(position);
viewHolder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Button of the following item was clicked at: " + position, Toast.LENGTH_LONG).show();
}
});
convertView.setTag(viewHolder);
}
mainViewholder = (ViewHolder) convertView.getTag();
mainViewholder.title.setText(getItem(position));
return convertView;
}
}
public class ViewHolder {
ImageView thumbnail;
TextView title;
Button button;
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
View test1 = java_spinner.getSelectedView();
choosen_text = (TextView)test1 ;
mytext = choosen_text.getText().toString();
//Toast.makeText(this, "you selected" + mytext, Toast.LENGTH_SHORT).show();
View test2 = java_spinner2.getSelectedView();
choosen_text2 = (TextView)test2 ;
mytext2 = choosen_text2.getText().toString();
//Toast.makeText(this, "you selected" + mytext2, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
@Override
public void onBackPressed() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Keinen Bock mehr...?");
alertDialogBuilder
.setMessage("Ja und tschüss...!")
.setCancelable(false)
.setPositiveButton("Ja",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
})
.setNegativeButton("Nein", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context="com.example.dominic.spinner.MainActivity"
android:background="#c0b7b7">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#368260">
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginTop="12dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:entries="@array/days"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:spinnerMode="dropdown"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/linearLayout"
android:layout_alignEnd="@+id/linearLayout" />
<Spinner
android:entries="@array/pools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner2"
android:spinnerMode="dropdown"
android:layout_above="@+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner_number_of_rounds"
android:entries="@array/Auswahl_der_Rundenanzahl"
android:layout_above="@+id/spinner2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:id="@+id/listView" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="125dp"
android:layout_height="90dp"
android:id="@+id/itemImage"
android:src="@mipmap/party"
android:layout_margin="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/listView"
android:layout_weight="2"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PLAY"
android:id="@+id/playbutton" />
</LinearLayout>
</LinearLayout>
K Thx和Best, 星