0

For example, when I choose first item, 7th and 14th items are also selected. But I only make first item's textview's visible. What might be the problem?

Here is my 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;
    }

}
Figen Güngör
  • 12,169
  • 14
  • 66
  • 108

1 Answers1

2

Android reuses the list items (also known as the flyweight pattern) so you need to check both the selected and not selected state of the model in getView() method of the adapter.
You can find several similar posts in the following:

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

Community
  • 1
  • 1
Murat
  • 352
  • 1
  • 7