Android ListView数据不重复

Android ListView数据不重复

问题描述:

我想在listview中显示一些数据,但它只显示了上一个位置的数据。这里是我的代码:Android ListView数据不重复

public class ListDetailsActivity extends Activity { 
    /** Called when the activity is first created. */ 

    private Context con; 
    private String pos = ""; 
    private ImageView cPhoto; 
    private TextView cName, aName, aText; 
    private CacheImageDownloader downloader; 
    private Bitmap defaultBit; 
    private ProgressDialog pDialog; 
    private CityDetailsList CD; 
    private ListView cList; 
    private HotelMenuAdapter mAdapter; 
    private String response = ""; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.detailslayout); 
     con = this; 

     initUI(); 
    } 

    private void initUI() { 
     // TODO Auto-generated method stub 

     cName = (TextView) findViewById(R.id.cName); 

     cList = (ListView) findViewById(R.id.cDetailsView); 

     aName = (TextView) findViewById(R.id.authorName); 
     aText = (TextView) findViewById(R.id.authorText); 

     cPhoto = (ImageView) findViewById(R.id.cPhoto); 
     downloader = new CacheImageDownloader(); 
     defaultBit = BitmapFactory.decodeResource(getResources(), 
       R.drawable.attractionpng); 
     // pos = getIntent().getStringExtra("ID"); 
     PrintLog.myLog("CityDetails :", pos + "main"); 
     updateUI(); 

    } 

    private void updateUI() { 
     if (!SharedPreferencesHelper.isOnline(con)) { 
      AlertMessage.showMessage(con, "Error", "No internet connection"); 
      return; 
     } 

     pDialog = ProgressDialog.show(this, "Please wait...", "Loading...", 
       false, false); 

     final Thread d = new Thread(new Runnable() { 

      public void run() { 
       // TODO Auto-generated method stub 
       try { 
        if (AllCityDetailsParser.connect(con, AllURL 
          .cityGuideDetailsURL())) { 

        } 

       } catch (final Exception e) { 
        // TODO: handle exception 
        e.printStackTrace(); 
       } 

       try { 
        if (CityDetailsParser.connect(con, AllURL 
          .cityGuideDetailsURL())) { 
        } 

       } catch (final Exception e) { 
        // TODO: handle exception 
        e.printStackTrace(); 
       } 

       runOnUiThread(new Runnable() { 

        public void run() { 
         // TODO Auto-generated method stub 
         if (pDialog != null) { 
          pDialog.cancel(); 
         } 

         try { 

          CD = AllCityDetails.getAllCityDetails() 
            .elementAt(0); 

          cName.setText(CD.getName().trim()); 

          try { 
           if (CD.getIcon().length() != 0) { 

            downloader.download(CD.getIcon().trim(), 
              cPhoto); 

            AllConstants.cPhotoLink = CD.getIcon() 
              .replaceAll(" ", "%20"); 

           } 

           else { 
            cPhoto.setImageBitmap(defaultBit); 
            AllConstants.cPhotoLink = CD.getIcon(); 
           } 

          } catch (Exception e) { 
           // TODO: handle exception 
          } 

          if (AllCityDetails.getAllCityDetails().size() != 0) { 
           mAdapter = new HotelMenuAdapter(con); 
           cList.setAdapter(mAdapter); 

           PrintLog.myLog("I am in list adapter : ", 
             "true"); 

          } 

         } catch (Exception e) { 
          // TODO: handle exception 
         } 
        } 
       }); 

      } 
     }); 
     d.start(); 
    } 

    class HotelMenuAdapter extends ArrayAdapter<CityDetailsList> { 

     private final Context con; 

     public HotelMenuAdapter(Context context) { 
      super(context, R.layout.rowdetails, AllCityDetails 
        .getAllCityDetails()); 

      con = context; 
      // TODO Auto-generated constructor stub 

     } 

     @Override 
     public View getView(final int position, View convertView, 
       ViewGroup parent) { 
      View v = convertView; 
      if (v == null) { 
       final LayoutInflater vi = (LayoutInflater) con 
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       v = vi.inflate(R.layout.rowdetails, null); 
      } 

      if (position < AllCityDetails.getAllCityDetails().size()) { 

       final CityDetailsList r = AllCityDetails 
         .getCityDetailsList(position); 

       final TextView aname = (TextView) v 
         .findViewById(R.id.authorName); 
       aname.setText(r.getAuthor_name().trim()); 
       final TextView atext = (TextView) v 
         .findViewById(R.id.authorText); 
       atext.setText(r.getText().trim()); 

       PrintLog.myLog("authorName : ", r.getAuthor_name() + "!!!"); 

      } 

      // TODO Auto-generated method stub 
      return v; 
     } 

    } 

    } 

使用你的代码这样第一:

final MyProgressDialog progDailog = new MyProgressDialog(
          getActivity()); 

        final Handler handler = new Handler() { 
         @Override 
         public void handleMessage(Message msg) { 
          //set listview adapter here 
         } 

        }; 

        new Thread() { 

         public void run() { 

          try { 
             // put your all calling method here.. 
          } catch (Exception e) { 
           // TODO: handle exception 
           Log.e("error", "" 
             + e.getMessage().toString()); 
          } 

          handler.sendEmptyMessage(0); 
          progDailog.dismiss(); 
         } 
        }.start(); 

适配器存在许多问题。请尝试这种方式..

  1. clickhere
  2. clickhere