当我点击一个项目时,ListView ONItemClickListener选择几个项目

时间:2014-09-24 14:05:42

标签: android listview onitemclicklistener

例如,当我选择第一个项目时,也会选择第7个和第14个项目。但我只能让第一项的textview可见。可能是什么问题?

这是我的ItemClickListener:

theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {

                        if (myList.get(position).getstopId().toString()
                                .equals(tempid)) {
                            Log.e("Clicked", tempid);

                            busstopid = tempid;
                            stopName = myList.get(position).getstopName()
                                    .toString();
                            progress.setCancelable(false);
                            progress.setMessage(getString(R.string.pleasewait));
                            progress.setTitle(getString(R.string.loading));
                            progress.setIcon(R.drawable.ic_launcher);
                            progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                            progress.setProgress(0);
                            progress.setMax(100);
                            progress.show();
                            new GetClosestBusList().execute();
                            tempid = "0";
                        }

                        else {
                            tempid = myList.get(position).getstopId()
                                    .toString();
                            // Hide previously selected text view
                            if (selectedIndex > -1) {
                                int viewIndex = selectedIndex
                                        - parent.getFirstVisiblePosition();
                                // if previously selected list item is still
                                // visible, hide its text view
                                if (viewIndex > -1
                                        && viewIndex < parent.getChildCount()) {
                                    View itemView = parent
                                            .getChildAt(viewIndex);
                                    TextView textView = (TextView) itemView
                                            .findViewById(R.id.stationRouteTimeTable);
                                    TextView stationarrow = (TextView) itemView
                                            .findViewById(R.id.routedetailarrow);
                                    TextView iconbusonstop = (TextView) itemView
                                            .findViewById(R.id.iconbusonstop);
                                    LinearLayout stationlistitem = (LinearLayout) itemView
                                            .findViewById(R.id.stationlistitem);
                                    stationlistitem
                                            .setBackgroundColor(color.white);
                                    textView.setVisibility(View.GONE);
                                    if (myList.get(position).getbusOnStop()
                                            .toString().equalsIgnoreCase("1")) {
                                        iconbusonstop
                                                .setVisibility(View.VISIBLE);
                                    } else
                                        iconbusonstop.setVisibility(View.GONE);
                                    stationarrow.setVisibility(View.GONE);
                                }
                            }

                            // Now you make the newly selected text view visible
                            // and
                            // remember the selected item
                            selectedIndex = position;

                            int viewIndex = selectedIndex
                                    - parent.getFirstVisiblePosition();
                            View itemView = parent.getChildAt(viewIndex);
                            TextView itemView2 = (TextView) itemView
                                    .findViewById(R.id.stationRouteTimeTable);
                            TextView stationarrow = (TextView) view
                                    .findViewById(R.id.routedetailarrow);
                            TextView iconbusonstop = (TextView) view
                                    .findViewById(R.id.iconbusonstop);
                            stationarrow.setTypeface(Typeface.createFromAsset(
                                    getAssets(), "fontello.ttf"));

                            itemView2.setVisibility(View.VISIBLE);
                            stationarrow.setVisibility(View.VISIBLE);
                            if (myList.get(position).getbusOnStop().toString()
                                    .equalsIgnoreCase("1"))
                                iconbusonstop.setVisibility(View.VISIBLE);
                            else
                                iconbusonstop.setVisibility(View.GONE);
                            itemView2.setBackgroundColor(color.white);

                            Double lat = Double
                                    .parseDouble(((RouteDetailInfo) parent
                                            .getItemAtPosition(position))
                                            .getlat());
                            Double lng = Double
                                    .parseDouble(((RouteDetailInfo) parent
                                            .getItemAtPosition(position))
                                            .getlng());
                            LatLng itemLocation = new LatLng(lat, lng);

                            googleMap.animateCamera(CameraUpdateFactory
                                    .newLatLngZoom(itemLocation, 16));
                        }
                    }

                });

ListViewAdapter

public class RouteDetailInfoAdapter extends ArrayAdapter<RouteDetailInfo> {

    private Context context;

    public RouteDetailInfoAdapter(Context context, int textViewResourceId, ArrayList<RouteDetailInfo> items) {
        super(context, textViewResourceId, items);
        this.context = context;


    }


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



        View view = convertView;

        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.route_detail_item, null);
        }

        TextView iconbus = (TextView) view
                .findViewById(R.id.iconbus);
        TextView iconbusonstop = (TextView) view
                .findViewById(R.id.iconbusonstop);
        TextView routeArrow = (TextView) view
                .findViewById(R.id.routeArrow);
        Typeface font = Typeface.createFromAsset(context.getAssets(),
                "fontello.ttf");
        Typeface font2 = Typeface.createFromAsset(context.getAssets(),
                "opensansregular.ttf");
        iconbusonstop.setTypeface(font);
        iconbus.setTypeface(font);
        routeArrow.setTypeface(font);



        RouteDetailInfo item = getItem(position);
        if (item!= null) {

            TextView routeStopId = (TextView) view.findViewById(R.id.routeStopId);
            if (routeStopId != null) {
                // do whatever you want with your string and long
                routeStopId.setText(item.getstopId());

            }

            if (iconbusonstop != null) {
                // do whatever you want with your string and long
                if(item.getbusOnStop().equalsIgnoreCase("1"))
                iconbusonstop.setVisibility(View.VISIBLE);
                else
                    iconbusonstop.setVisibility(View.GONE);

            }

            // My layout has only one TextView
            TextView headsign = (TextView) view.findViewById(R.id.headsign);
            if (headsign != null) {
                // do whatever you want with your string and long
                headsign.setText(item.getstopName());
            }

         // My layout has only one TextView
            TextView stationRouteTimeTable = (TextView) view.findViewById(R.id.stationRouteTimeTable);
            if (stationRouteTimeTable != null) {
                // do whatever you want with your string and long
                stationRouteTimeTable.setText(item.gettimeTable());
            }


         }

        return view;
    }

}

1 个答案:

答案 0 :(得分:2)

Android会重复使用列表项(也称为flyweight模式),因此您需要在适配器的getView()方法中检查模型的选定状态和未选中状态。
您可以在以下内容中找到几个类似的帖子:

Android - Any change made to any of the ListView item is affected to the first item

Change a specific row in a ArrayAdapter ListView Android

相关问题