Android:来自MySQL的ListView只显示最后一个元素

我正在尝试从
MySql数据库中检索数据并将其放在ListView上,一切正常,我甚至将这些数据放入textview(动态),它工作正常.但是当我使用ListView时,只显示了最后一个元素,我认为这意味着每个新元素都会被旧元素覆盖,对吧?

我该怎么做才能解决这个问题?这是我的代码告诉我哪里错了?

public class MakeAppointementActivity extends AppCompatActivity {

public List<AvailabilityList> customList;
public ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_make_appointement);
    lv=(ListView)findViewById(R.id.listView);

    Intent intent=getIntent();


    new RetrieveTask().execute();

}

private class RetrieveTask extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        String strUrl = "availableAppointments1.php";
        URL url;
        StringBuffer sb = new StringBuffer();
        try {
            url = new URL(strUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            InputStream iStream = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(iStream));
            String line;
            while( (line = reader.readLine()) != null){
                sb.append(line);
            }

            reader.close();
            iStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();

    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        new ParserTask().execute(result);
    }
}

// Background thread to parse the JSON data retrieved from MySQL server
private class ParserTask extends AsyncTask<String, Void, List<HashMap<String, String>>> {
    @Override
    protected List<HashMap<String, String>> doInBackground(String... params) {
        AppointementJSONParser appointementParser = new AppointementJSONParser();
        JSONObject json = null;
        try {
            json = new JSONObject(params[0]);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return appointementParser.parse(json);
    }

 @Override
 protected void onPostExecute(List<HashMap<String, String>> result) {
             customList=new ArrayList<>(); / move it to here
            for (int i = 0; i < result.size(); i++) {
                HashMap<String, String> appointement = result.get(i);
                String fromT = appointement.get("fromT");
                String toT = appointement.get("toT");
                String date = appointement.get("date");

                addAvailableAppoint(fromT,toT,date);
            }
            updateListView(); // update listview when you add all data to arraylist
        }
    }

    private void addAvailableAppoint(final String fromT, final String toT, final String date) {
        customList.add(new AvailabilityList(fromT));
    }

    // split new function for update listview
    private updateListView(){
        ArrayAdapter adapter=new    DoctorAvailabilityAdapter(MakeAppointementActivity.this,R.layout.list_items,customList);
        adapter.notifyDataSetChanged();
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MakeAppointementActivity.this, AppointementActivity.class);
                intent.putExtra("fromT", fromT);
                intent.putExtra("toT", toT);
                intent.putExtra("date", date);
                startActivity(intent);
            }
        });

    }

}

最佳答案 试试这个代码

     .....// your above code



     @Override
     protected void onPostExecute(List<HashMap<String, String>> result) {
                 customList=new ArrayList<>(); / move it to here
                for (int i = 0; i < result.size(); i++) {
                    HashMap<String, String> appointement = result.get(i);
                    String fromT = appointement.get("fromT");
                    String toT = appointement.get("toT");
                    String date = appointement.get("date");

                    addAvailableAppoint(fromT,toT,date);
                }
                updateListView(); // update listview when you add all data to arraylist
            }
        }

        private void addAvailableAppoint(final String fromT, final String toT, final String date) {
            customList.add(new AvailabilityList(fromT));
        }

        // split new function for update listview
        private updateListView(){
            ArrayAdapter adapter=new    DoctorAvailabilityAdapter(MakeAppointementActivity.this,R.layout.list_items,customList);
            adapter.notifyDataSetChanged();
            lv.setAdapter(adapter);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(MakeAppointementActivity.this, AppointementActivity.class);
                    // intent.putExtra("fromT", fromT); // change it to
                    intent.putExtra("fromT", customList.get(position).getFromT());  
                    intent.putExtra("toT", toT);
                    intent.putExtra("date", date);
                    startActivity(intent);
                }
            });

        }
}

希望这有帮助

点赞