egente
egente

Reputation: 3

CustomListAdapter with custom ArrayList in Android

CustomListAdapter Class:

its like example for problem;

public class CustomList extends ArrayAdapter<String>{

private final Activity context;
private final List<FixtureData> list;



public CustomList(Activity context,List<FixtureData>list) {

super(context, R.layout.list_single);
this.context = context;
this.list = list;



}
@Override
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);

TextView txt_date = (TextView) rowView.findViewById(R.id.txt_date);
TextView txt_team1 = (TextView) rowView.findViewById(R.id.txt_team2);
TextView txt_team2 = (TextView) rowView.findViewById(R.id.txt_team1);



txt_date.setText(list.get(position).date.toString());
txt_team1.setText(list.get(position).team1.toString());
txt_team2.setText(list.get(position).team2.toString());

return rowView;
}
}

MainActivity Class:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


public List<FixtureData> fixtureArray = new ArrayList<FixtureData>();

    class FixtureData extends Object{

            public String date=null;
            public String team1=null;
            public String team2=null;

}


        FixtureData fixture = new FixtureData();
      fixture.date="1990";
      fixture.team1="Manchester";
      fixture.team2="Barcelona";

          fixtureArray.add(fixture);

    final CustomList adapter2 = new CustomList(MainActivity.this, fixtureArray);


        liste=(ListView)findViewById(R.id.list1));
        liste.setAdapter(adapter2);
        liste.setItemsCanFocus(true);
        liste.setFocusable(false);
        liste.setFocusableInTouchMode(false);
        liste.setClickable(false);
}}

its just example for problem, dont check intention.

When i use single ArrayList my Codes Working like;

final List<String> date = new ArrayList<String>();
final List<Integer> team1= new ArrayList<String>();
final List<Integer> team2= new ArrayList<String>();

but when i tried custom ArrayList like this, its not working

public List<FixtureData> fixtureArray = new ArrayList<FixtureData>();
class FixtureData extends Object{

            public String date=null;
            public String team1=null;
            public String team2=null;           
}

      FixtureData fixture = new FixtureData();
      fixture.date="1990";
      fixture.team1="Manchester";
      fixture.team2="Barcelona";

      fixtureArray.add(fixture);

with this codes giving me null ListView, How can i solve my problem?

Upvotes: 0

Views: 316

Answers (3)

Laura
Laura

Reputation: 284

It looks like there's a lot going on in your code, but in addition to what Brijesh mentioned about changing ArrayAdapter to ArrayAdapter, one other thing that seems to be happening is that you never set your list to your adapter. You send in your list, and call super(context, R.layout.list_single), but you didn't include your list in that call and don't separately set it later. In addition, although this is not the only way to deal with this issue, since you are using a custom list item view with multiple text views, it might be easier to just send a 0 as a placeholder for the layout resource - that's okay bc you define it later in getView(). The super constructor for ArrayAdapter expects a layout with one text view. You probably want to check out the android developer reference page for ArrayAdapter http://developer.android.com/reference/android/widget/ArrayAdapter.html so you can see what your options are and what is expected, but one option would be to just call super(context,0,list). BTW - if you want to use this.list, make sure to define it before you use it in the call to super, instead of after as you currently have it.

Upvotes: 0

Apoorv
Apoorv

Reputation: 13520

Override the getCount method in your CustomList

@Override
public int getCount()
{
  return list.size();
}

OR

change

super(context, R.layout.list_single);

to

super(context, R.layout.list_single,list);

Upvotes: 1

Brijesh Thakur
Brijesh Thakur

Reputation: 6788

Your first line of Program shows

public class CustomList extends ArrayAdapter< String >{

It should be :

public class CustomList extends ArrayAdapter< FixtureData >{

Your ListAdapter class should know about what type of Object its going to handle

Upvotes: 0

Related Questions