I have a question about customizing android ListView items:
I was following this tutorial: http://codehenge.net/blog/2011/05/customizing-android-listview-item-layout/
The first part, with normal R.android.simple_text_item_1 worked fine for me, so I get my ListView with Strings in every row.
Then I moved on and tried to modify the items of my ListView like described:
I defined this XML data for my list items. Its a LinearLayout that holds only two TextViews
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/email" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
I defined a data class UserRecord, that simply gives me the abiltity to create objects which consist of a name and an email adress
public class UserRecord { public String username; public String email; public UserRecord(String username, String email) { this.username = username; this.email = email; } }
I copied the whole adapterclass, but I got one error, the line where I commented
public class UserItemAdapter extends ArrayAdapter<UserRecord> { private ArrayList<UserRecord> users; public UserItemAdapter(Context context, int textViewResourceId, ArrayList<UserRecord> users) { super(context, textViewResourceId, users); this.users = users; } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { //In the following line I get the error: "The method getSystemService is undefined //for type UserItemAdapter LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.list_item, null); } UserRecord user = users.get(position); if (user != null) { TextView username = (TextView) v.findViewById(R.id.username); TextView email = (TextView) v.findViewById(R.id.email); if (username != null) { username.setText(user.username); } if(email != null) { email.setText("Email: " + user.email ); } } return v; } }
I changed my onCreate method in the MainActivity like follows, maybe here's the fault.
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayList<UserRecord> users = new ArrayList<UserRecord>(); UserRecord a = new UserRecord("a","b"); users.add(a); ListView listView = (ListView) findViewById(R.id.listView1); //The following adapter worked fine when using a String array //listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items)); //This adapter doesn't work I guess :( listView.setAdapter(new UserItemAdapter(this, R.layout.list_item, users)); }
Can anyone tell me, where my fault is? If I try to launch my app, it doesn't even start and my emulator says "unfortunately closed blabla". -.-