循环数据不正确

时间:2018-12-10 03:31:44

标签: android for-loop

我的循环有问题。

这是我的java中的函数类,我想查看此函数中的数据

class appointment extends AsyncTask<String, String, String>
    {
        private String val3;
        public appointment(String medicalno) {
            this.val3 = medicalno;
        }
        @Override
        protected String doInBackground(String... strings) {

            CallSoap cs = new CallSoap();
            String medno = cs.InfoUser(val3);
            String[] temp = medno.split(",");
            String data = cs.HistoryTomorrow(temp[0]);
            return data;
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            //Toast.makeText(Appointment.this,s.toString(),Toast.LENGTH_SHORT).show();
            poli.clear();
            nama.clear();
            tanggal.clear();
            if(s.equals("No Data"))
            {
                poli.add("");
                nama.add(s);
                tanggal.add("");
            }
            else {
                String data = s.replaceAll("[a-zA-Z0-9/:#., ]*","");
                if(data.length()<1)
                {
                    String data2[] = s.split("#");
                    poli.add(data2[0]);
                    nama.add(data2[1]);
                    tanggal.add(data2[2]);
                }
                else {
                    String data1[] = s.split("%");
                    Toast.makeText(Appointment.this,data,Toast.LENGTH_SHORT).show();
                    for (int i = 0; i < data.length()+1; i++) {
                        poli.add(data1[i] + "_____"+i);
                        nama.add("");
                        tanggal.add("");
                    }
                }
            }
            list.setAdapter(new HistoryTodayTomorrowAdapter(Appointment.this, poli,nama,tanggal));
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(Appointment.this,poli.get(position),Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

这是我的HistoryTodayTomorrowAdapter

package info.androidhive.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

import info.androidhive.recyclerviewsearch.R;

public class HistoryTodayTomorrowAdapter extends BaseAdapter {
    private Context context;
    private final ArrayList<String> Poli,Nama,Tanggal;

    public HistoryTodayTomorrowAdapter(Context context, ArrayList<String> poli,ArrayList<String> nama,ArrayList<String> tanggal) {
        this.context = context;
        this.Poli = poli;
        this.Nama = nama;
        this.Tanggal = tanggal;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null) {

            gridView = new View(context);

            // get layout from mobile.xml
            gridView = inflater.inflate(R.layout.user_row_appoint, null);

            // set value into textview
            TextView textView = (TextView) gridView.findViewById(R.id.NamaPoli);
            TextView textView1 = (TextView) gridView.findViewById(R.id.Tanggal);
            TextView textView2 = (TextView) gridView.findViewById(R.id.Nama);

            textView.setText(Nama.get(position));
            textView1.setText(Poli.get(position));
            textView2.setText(Tanggal.get(position));

        } else {
            gridView = (View) convertView;
        }

        return gridView;
    }


    @Override
    public int getCount() {
        return Poli.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
}

这是我从HistoryTomorrow返回的数据(示例)

data1#qwe1#a123%data2#qwe2#b123%data3#qwe3#c123%data4#qwe4#d123%data5#qwe5#e123

但是我得到这样的视图数据(联系人列表):

data1 - qwe1 - a123  
data2 - qwe2 - b123  
data3 - qwe3 - c123  
data1 - qwe1 - a123  
data2 - qwe2 - b123  

代码将像该行中的5个数据一样向右循环5次。但是数据视图不正确。它只是循环访问3个数据而不是5个/所有数据。

1 个答案:

答案 0 :(得分:0)

在批评循环之前,您应该检查约会的返回结果是否正确,然后解析结果有5个项目符合您的预期。希望对您有帮助。