无法从arraylist中显示listview中的数据

时间:2017-05-15 07:01:04

标签: java android

我的应用程序从服务器获取数据,我在listview中显示它我想在其中添加过滤器/搜索功能。所以我在baseadapter中使用了filterable。我在控制台中收到服务器的响应,但listview中没有显示任何内容。请在我的代码中指导我什么不对。

这是适配器类

public class Patient_Adapter extends BaseAdapter implements Filterable{

Activity context;
String[] name, age, number, dip_rate;

//for filtering use arraylists
LayoutInflater inflater;
private ValueFilter valueFilter;
ArrayList<ArrayList_get_set> arrayList;
private ArrayList<ArrayList_get_set> StringFilterList;

public Patient_Adapter(Activity applicationContext, 
ArrayList<ArrayList_get_set> arrayList) {

    this.context     = applicationContext;
    /////
    this.arrayList   = arrayList;
    StringFilterList = arrayList;
    this.inflater    = (LayoutInflater) 
    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    getFilter();

}

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

@Override
public Object getItem(int i) {
    return arrayList.get(i).getName();
}

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


private class Viewholder{

    TextView name_
            ,age_
            ,number_
            ,dip_rate_
            ,tv_request_number;

}


@Override
public View getView(final int position, View view, ViewGroup viewGroup) {


    Viewholder viewholder;

    LayoutInflater inflater = context.getLayoutInflater();

    if (view == null){

        viewholder = new Viewholder();
        view = inflater.inflate(R.layout.patient_item_list,null);

        viewholder.tv_request_number = (TextView) 
        view.findViewById(R.id.s_req_num);
        viewholder.name_     = (TextView) view.findViewById(R.id.name);
        viewholder.age_      = (TextView) view.findViewById(R.id.age);
        viewholder.number_   = (TextView) view.findViewById(R.id.number);
        viewholder.dip_rate_ = (TextView) view.findViewById(R.id.dip_rate);

        view.setTag(viewholder);

    }
    else {

        viewholder = (Viewholder) view.getTag();
    }


    // set the value in TextView
    viewholder.tv_request_number.setText((name.length-position)+".");
    viewholder.name_.setText(name[position]);
    viewholder.age_.setText(age[position]);
    viewholder.number_.setText(number[position]);
    viewholder.dip_rate_.setText(dip_rate[position]);

    return view;
}


//Filter Results
@Override
public Filter getFilter() {

    if(valueFilter==null) {

        valueFilter=new ValueFilter();
    }

    return valueFilter;
}


private class ValueFilter extends Filter {

    //Invoked in a worker thread to filter the data according to the 
    constraint.
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();
        if(constraint != null && constraint.length()>0){
            ArrayList<ArrayList_get_set> filterList = new ArrayList<>();
            for(int i=0;i<StringFilterList.size();i++){
                if((StringFilterList.get(i).getName().toUpperCase())
                        .contains(constraint.toString().toUpperCase())) {
                    ArrayList_get_set contacts = new ArrayList_get_set();
                    contacts.setName(StringFilterList.get(i).getName());
                        filterList.add(contacts);
                }
            }
            results.count=filterList.size();
            results.values=filterList;
        }else{
            results.count=StringFilterList.size();
            results.values=StringFilterList;
        }
        return results;
    }


    //Invoked in the UI thread to publish the filtering results in the user 
    interface.
    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint,
                                  FilterResults results) {
        arrayList = (ArrayList<ArrayList_get_set>) results.values;
        notifyDataSetChanged();
    }
}


}

这是getset类

public class ArrayList_get_set {

String name
        ,age
        ,number
        ,dip_rate;

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

public String getAge() {
    return age;
}
public void setAge(String age) {
    this.age = age;
}

public String getNumber() {
    return number;
}
public void setNumber(String number) {
    this.number = number;
}

public String getDip_rate() {
    return dip_rate;
}
public void setDip_rate(String dip_rate) {
    this.dip_rate = dip_rate;
}

}

这是我的主要活动

private ArrayList<ArrayList_get_set> arrayList = new 
ArrayList<ArrayList_get_set>();
boolean server_check=false;
Patient_Adapter patient_adapter;

ListView simListView;
EditText search;

String[] name
        ,age
        ,number
        ,dip_rate;

String server_response="0"
        ,server_response_text
        ,d_number;

JSONObject jp_obj;
JSONArray jar_array;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, @Nullable Bundle savedInstanceState) {

    View v       = inflater.inflate(R.layout.patient_history, container, 
false  );

    simListView  = (ListView) v.findViewById(R.id.list);
    search       = (EditText) v.findViewById(R.id.et_search);


    //Getting Client Number from SharedPreferences
    SharedPreferences preferences = 
this.getActivity().getSharedPreferences("DataStore" , Context.MODE_PRIVATE);
    d_number = preferences.getString("number", "Ni Mila Kuch");

    if(new Check_internet_connection(getActivity()).isNetworkAvailable()){

        new GetPatientsList().execute();

    }
    else {

        Toast.makeText(getActivity(),
                "Check your Internet Connection & Try again", 
Toast.LENGTH_LONG).show();
    }


    return v;
}


//load questions
public class GetPatientsList extends AsyncTask<String, Void, String> {

    ProgressDialog progressDialog;

    @Override
    protected void onPreExecute() {

        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setTitle("Loading! Be Patient!");
        progressDialog.setCancelable(false);
        progressDialog.show();

    }

    @Override
    protected String doInBackground(String... params) {

        try {

            JSONObject obj = new JSONObject();

            obj.put("operation", "patients");
            obj.put("d_number", d_number);

            JsonParser jp = new JsonParser();

            String str_req = jp.multipartFormRequestForFindFriends(Url.ulr, 
"UTF-8", obj, null);

            jp_obj = new JSONObject(str_req);
            jar_array = jp_obj.getJSONArray("JsonData");

            JSONObject c;

            name      = new String[(jar_array.length()-1)];
            age       = new String[(jar_array.length()-1)];
            number    = new String[(jar_array.length()-1)];
            dip_rate  = new String[(jar_array.length()-1)];

            c = jar_array.getJSONObject(0);

            if (c.length() > 0) {

                server_response = c.getString("response");

                if (server_response.equals("0")) {
                    server_response_text = c.getString("response-text");

                }
            }

            int j = 1;

            if (server_response.equals("1")) {
                for (int i = 0; j < jar_array.length(); i++) {

                    c = jar_array.getJSONObject(j);

                    if (c.length() > 0) {

                        name[i]     = c.getString("name");
                        age[i]      = c.getString("age");
                        number[i]   = c.getString("number");
                        dip_rate[i] = c.getString("dip_rate");

                    }

                    j++;
                }
            }


            server_check = true;


        } catch (Exception e) {
            e.printStackTrace();

        }


        return null;
    }


    @Override
    protected void onPostExecute(String s) {

        progressDialog.dismiss();

        if (server_check) {

            if (server_response.equals("1")) {

                if (name.length > 0) {

                    Toast.makeText(getActivity(), arrayList.size(), 
Toast.LENGTH_SHORT).show();
                    patient_adapter = new Patient_Adapter(getActivity(),
                            arrayList);
                    simListView.setAdapter(patient_adapter);



                    //filtering data
                    search.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence 
charSequence, int i, int i1, int i2) {


patient_adapter.getFilter().filter(charSequence);
                        }

                        @Override
                        public void onTextChanged(CharSequence charSequence, 
                   int i, int i1, int i2) {

                        }

                        @Override
                        public void afterTextChanged(Editable editable) {

                        }
                    });


                } else {
                    Toast.makeText(getActivity(), server_response_text, 
Toast.LENGTH_SHORT).show();

                }


            } else {

                Toast.makeText(getActivity(), "Error while loading data", 
Toast.LENGTH_SHORT).show();
            }

        }
    }

}


@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    //you can set the title for your toolbar here for different fragments 
different titles
    getActivity().setTitle("Patient History");
}

}

2 个答案:

答案 0 :(得分:0)

viewholder.name_.setText(arrayList.get(position).getName());

从ArrayList获取。

答案 1 :(得分:0)

试试这个。

getView()中的

使用此

                   viewholder.tv_request_number.setText((arrayList.size()-position)+".");
                    viewholder.name_.setText(arrayList.get(position).getName());
                    viewholder.age_.setText(arrayList.get(position).getAge());
                    viewholder.number_.setText(arrayList.get(position).getNumber());
                    viewholder.dip_rate_.setText(arrayList.get(position).getDip_rate());

然后更新

@Override
public Object getItem(int i) {
    return arrayList.get(i);
}