0

I'm trying to implement a listview within a tab. There i'm using a custom arrayadapter to create a custom listview. Code is as follows.

Creating the tab activity :

TabHost th = getTabHost();
TabSpec specGroups = th.newTabSpec("Groups");
    specGroups.setIndicator("Groups");
    Intent intentGroups = new Intent(this, GroupsList.class);
    specGroups.setContent(intentGroups);

Grouplist activity :

public class GroupsList extends Activity {

public String[] ROSTER_LIST = {"Sam", "Bob", "Tabg", "Toushi", "john"};
private ListView ROSTER_LISTVIEW;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.groups_list);

    Log.d("STARTED", "0");

    ROSTER_LISTVIEW = (ListView) findViewById(R.id.listViewFriends);
    Log.d("STARTED", "1");

    ArrayAdapter<String> adapter = new MyAdapter(this,
            android.R.layout.simple_list_item_1, R.id.textViewRosterRow,
            ROSTER_LIST);
    Log.d("STARTED", "2");

    ROSTER_LISTVIEW.setAdapter(adapter);
    Log.d("STARTED", "3");

}

Custom Adapter Class (inner class) :

private class MyAdapter extends ArrayAdapter<String> {

    public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] ROSTER_LIST) {
        super(context, resource, textViewResourceId, ROSTER_LIST);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Log.d("ADAPTER", "0");
        View row = inflater
                .inflate(R.layout.friends_list_row, parent, false);
        Log.d("ADAPTER", "1");

        TextView text = (TextView) row.findViewById(R.id.textViewRosterRow);
        Log.d("ADAPTER", "2");
        text.setText(ROSTER_LIST[position]);

        Log.d("ADAPTER", "OK");
        return row;
    }

}

inside the oncreate method Log.d("STARTED", "2"); line logs in the logcat and then pops sthe nullpointerexception. no Log logs in the logcat which is inside the MyAdapter inner class.

This code executes fine without the tabs.

what is the mistake i have made here? How can I solve this? thanks in advance :)

Chamath
  • 249
  • 4
  • 12

1 Answers1

0

Change Adapter getView as :

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;

        if(row==null){
          LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          Log.d("ADAPTER", "0");
          row = inflater
                .inflate(R.layout.friends_list_row, parent, false);
          Log.d("ADAPTER", "1");
      }
        TextView text = (TextView) row.findViewById(R.id.textViewRosterRow);
        Log.d("ADAPTER", "2");
        text.setText(ROSTER_LIST[position]);

        Log.d("ADAPTER", "OK");
        return row;
    }
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213