簡體   English   中英

Android ListView數據不重復

[英]Android ListView data is not repeating

我正在嘗試在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. 點擊這里
  2. 點擊這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM