Skip to content

Commit ecd687b

Browse files
committed
Using LayoutInflator to change the icons for different things.
1 parent 999d653 commit ecd687b

3 files changed

Lines changed: 41 additions & 3 deletions

File tree

637 Bytes
Loading
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package de.vogella.android.listactivity;
2+
3+
import android.app.Activity;
4+
import android.view.LayoutInflater;
5+
import android.view.View;
6+
import android.view.ViewGroup;
7+
import android.widget.ArrayAdapter;
8+
import android.widget.ImageView;
9+
import android.widget.TextView;
10+
11+
public class MyArrayAdapter extends ArrayAdapter<String> {
12+
private final Activity context;
13+
private final String[] names;
14+
15+
public MyArrayAdapter(Activity context, String[] names) {
16+
super(context, R.layout.rowlayout, names);
17+
this.context = context;
18+
this.names = names;
19+
}
20+
21+
@Override
22+
public View getView(int position, View convertView, ViewGroup parent) {
23+
LayoutInflater inflater = context.getLayoutInflater();
24+
View rowView = inflater.inflate(R.layout.rowlayout, null, true);
25+
26+
TextView label = (TextView) rowView.findViewById(R.id.label);
27+
label.setText(names[position]);
28+
System.out.println(names[position]);
29+
// Change the icon for Windows and iPhone
30+
if (names[position].startsWith("Windows7") || names[position].startsWith("iPhone") ) {
31+
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
32+
imageView.setImageResource(R.drawable.alt_window_16);
33+
}
34+
35+
return rowView;
36+
}
37+
38+
}

‎de.vogella.android.listactivity/src/de/vogella/android/listactivity/MyList.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import android.app.ListActivity;
44
import android.os.Bundle;
55
import android.view.View;
6-
import android.widget.ArrayAdapter;
76
import android.widget.ListView;
87
import android.widget.Toast;
98

@@ -16,8 +15,7 @@ public void onCreate(Bundle icicle) {
1615
String[] names = new String[] { "Linux", "Windows7", "Eclipse", "Suse", "Ubuntu", "Solaris", "Android", "iPhone"};
1716
// Create an ArrayAdapter, that will actually make the Strings above
1817
// appear in the ListView
19-
this.setListAdapter(new ArrayAdapter<String>(this,
20-
R.layout.rowlayout, R.id.label, names));
18+
this.setListAdapter(new MyArrayAdapter(this, names));
2119
}
2220

2321
@Override
@@ -30,4 +28,6 @@ protected void onListItemClick(ListView l, View v, int position, long id) {
3028
.show();
3129

3230
}
31+
32+
3333
}

0 commit comments

Comments
 (0)