package
com.example.button_spinnertest;
import
android.app.Activity;
import
android.app.AlertDialog;
import
android.content.Context;
import
android.os.Bundle;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.view.ViewGroup;
import
android.widget.AdapterView;
import
android.widget.AdapterView.OnItemClickListener;
import
android.widget.BaseAdapter;
import
android.widget.ListView;
import
android.widget.TextView;
import
android.widget.Toast;
public
class
MainActivity
extends
Activity {
private
TextView spinnerBtn;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinnerBtn=(TextView) findViewById(R.id.spinnerBtn);
spinnerBtn.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View v) {
actionAlertDialog();
}
});
}
protected
void
actionAlertDialog(){
AlertDialog.Builder builder;
final
AlertDialog alertDialog;
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.listview, (ViewGroup)findViewById(R.id.layout_myview));
ListView myListView = (ListView) layout.findViewById(R.id.mylistview);
MyAdapter adapter =
new
MyAdapter(MainActivity.
this
);
myListView.setAdapter(adapter);
builder =
new
AlertDialog.Builder(MainActivity.
this
);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
myListView.setOnItemClickListener(
new
OnItemClickListener() {
@Override
public
void
onItemClick(AdapterView<?> arg0, View view,
int
position,
long
arg3) {
int
temp=(Integer) arg0.getItemAtPosition(position);
System.out.println(temp+
"=====================temp"
);
Toast.makeText(getApplicationContext(),
"SB "
+temp+
" 號"
, Toast.LENGTH_SHORT).show();
alertDialog.dismiss();
}
});
}
class
MyAdapter
extends
BaseAdapter {
int
sum[]={
1
,
2
,
3
,
4
,
5
};
Context context;
private
LayoutInflater inflater;
public
MyAdapter(Context context){
this
.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public
Object getItem(
int
position) {
return
sum[position];
}
@Override
public
int
getCount() {
return
sum.length;
}
@Override
public
long
getItemId(
int
position) {
return
position;
}
@Override
public
View getView(
final
int
position, View convertView, ViewGroup parent) {
Holder holder;
if
(convertView==
null
){
holder =
new
Holder();
convertView = inflater.inflate(R.layout.item,
null
);
holder.name = (TextView) convertView.findViewById(R.id.item_name);
convertView.setTag(holder);
}
else
{
holder = (Holder) convertView.getTag();
}
holder.name.setText(sum[position]+
""
);
return
convertView;
}
public
class
Holder{
TextView name;
}
}
}