setOnItemClickListener未选择列表视图项

时间:2018-07-03 01:52:57

标签: android

我已经创建了此适配器文件以创建列表视图。列表视图已创建,但我无法从列表视图和显示中选择项目。

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;
    }

}

这是我的课程文件。

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;
    }
}`

这是我的主要活动文件,在其中显示列表视图。

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();
            }
        };
    }

}

我正在尝试在烤面包片中获得一个选定的物品位置。但是它没有显示。我在不同的示例中尝试了相同的代码,从而体现了这一点。

我是android的新手,请引导我。

1 个答案:

答案 0 :(得分:1)

您的DepartureListViewAdapter有点奇怪。您应该按以下方式致电super

super(context, 0, objects);

这告诉ArrayAdapter您不希望它为您创建列表项。您将在getView()中增加自己的列表项,特别是在此行:

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