简体   繁体   English

Android ListView数据不重复

[英]Android ListView data is not repeating

I am trying to show some data in listview .But it is showing the only a data from last position. 我正在尝试在listview显示一些数据。但是它仅显示最后一个位置的数据。 Here is my code: 这是我的代码:

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;
        }

    }

    }

use your code this way first : 首先以这种方式使用您的代码:

    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();

in Adapter there is many problem.. please try this way.. 在适配器中有很多问题..请尝试这种方式..

  1. clickhere 点击这里
  2. clickhere 点击这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM