Instead of CharSequence[] items = { "String 1", "String 2", "String 3" };
you can use a Custom Adapter
in your Alert Dialog,
Something like,
AlertDialog.Builder builder = new AlertDialog.Builder(MyApp.this);
builder.setTitle("Select");
builder.setAdapter(adapter,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int item) {
Toast.makeText(MyApp.this, "You selected: " + items[item],Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
Your list_row.xml file
<?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:id="@+id/icon"
android:layout_width="48px"
android:layout_height="48px"
android:layout_gravity="left" />
<TextView
android:id="@+id/title"
android:textColor="#0000FF"
android:text=""
android:paddingLeft="10dip"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
And your ListAdapter something like,
String[] items = {"airplanes", "animals", "cars", "colors", "flowers", "letters", "monsters", "numbers", "shapes", "smileys", "sports", "stars" };
// Instead of String[] items, Here you can also use ArrayList for your custom object..
ListAdapter adapter = new ArrayAdapter<String>(
getApplicationContext(), R.layout.list_row, items) {
ViewHolder holder;
Drawable icon;
class ViewHolder {
ImageView icon;
TextView title;
}
public View getView(int position, View convertView,
ViewGroup parent) {
final LayoutInflater inflater = (LayoutInflater) getApplicationContext()
.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(
R.layout.list_row, null);
holder = new ViewHolder();
holder.icon = (ImageView) convertView
.findViewById(R.id.icon);
holder.title = (TextView) convertView
.findViewById(R.id.title);
convertView.setTag(holder);
} else {
// view already defined, retrieve view holder
holder = (ViewHolder) convertView.getTag();
}
Drawable drawable = getResources().getDrawable(R.drawable.list_icon); //this is an image from the drawables folder
holder.title.setText(items[position]);
holder.icon.setImageDrawable(drawable);
return convertView;
}
};
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…