2014-09-24 73 views
0

例如,當我選擇第一個項目時,第7和第14項也被選中。但是我只能使第一個項目的textview可見。可能是什麼問題?ListView ItemClickListener選擇幾個項目時,我點擊一個

這裏是我的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; 
    } 

} 
+0

請發表您的列表適配器代碼。 – 2014-09-24 14:12:33

+0

我加了我的適配器。你能看看嗎? – 2014-09-24 14:55:57

回答

2

的Android重用列表項(也被稱爲輕量級模式),所以你需要檢查兩者的選擇,而不是選擇狀態該模型在適配器的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

+0

如何檢查未選擇的狀態? – 2014-09-24 14:40:01

+0

我添加了布爾變量選擇到我的項目模型,然後檢查它在我的適配器,它的工作。謝謝,繆拉。 – 2014-09-25 11:10:31

相關問題