【问题标题】:Looping Data is not Right [duplicate]循环数据不正确[重复]
【发布时间】:2018-12-10 14:03:02
【问题描述】:

我的循环有问题。

这是我的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 个/所有数据。

【问题讨论】:

  • 您确定问题出在循环上吗?你是怎么确定的?您确定它不在您的HistoryTodayTomorrowAdapter 中吗?
  • 是的,它在您的Adapter 中。将getView()if// set value into textview 注释之后的所有TextView 行移动到else 块之后。

标签: android for-loop


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-27
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多