0

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:

  1. 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>
    
  2. 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;
        }
    }
    
  3. 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;
    }
    }
    
  4. 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". -.-

Ry-
  • 218,210
  • 55
  • 464
  • 476
rikojir
  • 115
  • 2
  • 13
  • possible duplicate of [How can I use getSystemService in a non-activity class (LocationManager)?](http://stackoverflow.com/questions/4870667/how-can-i-use-getsystemservice-in-a-non-activity-class-locationmanager) – Apoorv Aug 09 '14 at 12:39
  • Pretty sure you are dealing with the same thing as this question: http://stackoverflow.com/questions/4321343/android-getsystemservice-inside-custom-arrayadapter I found this by searching "ArrayAdapter getSystemService". – Booger Aug 09 '14 at 12:43
  • You have to post your log stack to locate where's the error. you can get that by running this command in terminal:> adb logcat *:E – Sharjeel Oct 02 '14 at 23:56

1 Answers1

0

LayoutInflater is a systemService which is used to inflate layouts like the layout for your list item.In order to access any system service,you need some time of context.hence try this.

public class UserItemAdapter extends ArrayAdapter<UserRecord> {
private ArrayList<UserRecord> users;
private Context context

public UserItemAdapter(Context context, int textViewResourceId, ArrayList<UserRecord>             users) {
super(context, textViewResourceId, users);
this.context=context;
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)context.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;
}
}
Droidekas
  • 3,464
  • 2
  • 26
  • 40