Lo_Zio Lo_Zio - 4 months ago 12
Android Question

constructor of class ArrayAdapter

i have this into fragment in onCreateView():

ListView listView = (ListView) rootView.findViewById(R.id.listview);
ListAdapter listAdapter = new MyListAdapter();
listView.setAdapter(listAdapter);


MyListAdapter:

public class MyListAdapter extends ArrayAdapter {

/*public MyListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}*/


/* public MyListAdapter(Context context, int resource, List<ClipData.Item> items) {
super(context, resource, items);
}*/




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

Spinner spinner = (Spinner) convertView.findViewById(R.id.simpleSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getContext(),
R.array.country_arrays,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

return row;
}

}


ARRAY ADAPTER:

public class MyListAdapter extends ArrayAdapter implements AdapterView.OnItemSelectedListener{
Spinner spinner;
ListView listView;

/*public MyListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}*/

public MyListAdapter(Context context) {
super(context, R.layout.single_listview_item);
}




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

spinner = (Spinner) convertView.findViewById(R.id.simpleSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getContext(),
R.array.country_arrays,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

return row;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Spinner spinner = null;
System.out.println("ciao");
listView.setSelection(position);
String selState = (String) listView.getSelectedItem();
Toast.makeText(
getContext(),
"Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
}


Now my question is: what i must write into
constructor
of class
MyListAdapter
?

Thanks!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Answer

Every extending class must have minimum one constructor matching parent class. So Your class must have constructor with one line and calling super on it, no own code is required.

public MyListAdapter(Context context, int resource ) {

    super(context, resource);
    //your code 

}

But when You create your Adapter you are using constructor so You can create any constructor, important is to use super in it.

In constructor You can for example implement code to set data to list from database.

You can for example create constructor like that:

public MyListAdapter(Context context) {

    super(context, R.layout.row_view);

    //your code 


}

and

    ListView listView = (ListView) rootView.findViewById(R.id.listview);
    ListAdapter listAdapter = new MyListAdapter(getContext());
    listView.setAdapter(listAdapter);