Android:MySQL的ListView仅显示最后一个元素

Fadwa_lmh

我试图从MySql数据库中检索数据并将其放在上ListView,一切正常,我什至将该数据动态地放入textviews中,并且工作正常。但是,当我使用a时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);
                }
            });

        }
}

希望这个帮助

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章