setOnItemClickListener is not selecting listview item


setOnItemClickListener is not selecting listview item



I have created this adapter file to create a list view. List View is created but I am not able select item from list view and display.


public class DepartureListViewAdapter extends ArrayAdapter<Departure_City> {

private static final String TAG = "PersonListAdapter";
ArrayList<Departure_City> city = new ArrayList<>();


private Context mContext;
private int mResource;
private int lastPosition = -1;

/**
* Holds variables in a View
*/
private static class ViewHolder {
TextView name;
TextView nameInitial;
}

/**
* Default constructor for the PersonListAdapter
* @param context
* @param resource
* @param objects
*/
public DepartureListViewAdapter(Context context, int resource, ArrayList<Departure_City> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//get the persons information
String name = getItem(position).getName();
String nameInitial = getItem(position).getNameInitial();

//Create the person object with the information
//Person person = new Person(name,birthday,sex);
Departure_City dep = new Departure_City(name, nameInitial);
//create the view result for showing the animation
final View result;

//ViewHolder object
ViewHolder holder;


if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
holder= new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.cityName);
holder.nameInitial = (TextView) convertView.findViewById(R.id.cityInitial);

result = convertView;

convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
result = convertView;
}


Animation animation = AnimationUtils.loadAnimation(mContext,
(position > lastPosition) ? R.anim.load_down_anim : R.anim.load_up_anim);
result.startAnimation(animation);
lastPosition = position;

holder.name.setText(dep.getName());
holder.nameInitial.setText(dep.getNameInitial());


return convertView;
}

}



This is my class file.


public class Departure_City {
private String name;
private String nameInitial;

public Departure_City(String name, String nameInitial){
this.name = name;
this. nameInitial = nameInitial;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getNameInitial() {
return nameInitial;
}

public void setNameInitial(String nameInitial) {
this.nameInitial = nameInitial;
}
}`



This is my main activity file where list view displaying.


public class Departure extends AppCompatActivity {

private static final String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_departure);
Log.d(TAG, "onCreate: Started.");
ListView mListView = (ListView) findViewById(R.id.departureFromList);

//Create the Person objects
Departure_City city1 = new Departure_City("Wellington", "Wlg");
Departure_City city2 = new Departure_City("Nelson", "Nel");
Departure_City city3 = new Departure_City("Blenheim", "Ble");
Departure_City city4 = new Departure_City("Paraparumu", "Prm");
Departure_City city5 = new Departure_City("Taupo", "Tau");
Departure_City city6 = new Departure_City("Auckland", "akl");
Departure_City city7 = new Departure_City("Wellington", "Wlg");
Departure_City city8 = new Departure_City("Nelson", "Nel");
Departure_City city9 = new Departure_City("Blenheim", "Ble");
Departure_City city10 = new Departure_City("Paraparumu", "Prm");
Departure_City city11 = new Departure_City("Taupo", "Tau");
Departure_City city12 = new Departure_City("Auckland", "akl");

//Add the Person objects to an ArrayList
ArrayList<Departure_City> city = new ArrayList<>();
city.add(city1);
city.add(city2);
city.add(city3);
city.add(city4);
city.add(city5);
city.add(city6);
city.add(city7);
city.add(city8);
city.add(city9);
city.add(city10);
city.add(city11);
city.add(city12);

DepartureListViewAdapter adapter = new DepartureListViewAdapter(this, R.layout.departure_from_list, city);
mListView.setAdapter(adapter);

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(Departure.this, "Position: "+position, Toast.LENGTH_SHORT).show();
}
});
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(Departure.this, "are you clicked", Toast.LENGTH_SHORT).show();
}
};
}

}



I am trying to get a selected item position in a toast. But it is not displaying. I tried same code to different example it is reflecting there.



I am new in android so please guide me.





Please edit the last piece of the code too - public class Departure extends AppCompatActivity { Thanks!
– Rohit Sharma
Jul 3 at 2:24


public class Departure extends AppCompatActivity {




1 Answer
1



There is something weird with your DepartureListViewAdapter. You are supposed to call super as below


DepartureListViewAdapter


super


super(context, 0, objects);



This tells the ArrayAdapter that you don't want it to create the list item for you. You will inflate your own list item in getView(), specifically at this line:


ArrayAdapter


getView()


convertView = inflater.inflate(mResource, parent, false);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages