Android ListView数据不重复

时间:2013-02-11 05:45:54

标签: android android-listview position

我试图在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;
        }

    }

    }

1 个答案:

答案 0 :(得分:1)

首先以这种方式使用您的代码:

    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