从数据库(Android)搜索后显示空白屏幕

时间:2017-04-12 16:30:43

标签: java php android json

当我点击搜索捐赠者然后它在列表视图中显示空白屏幕我不知道为什么这是我从服务器返回的响应

enter image description here这是我的自定义适配器代码:

package com.example.kamran.ebloodbank;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Kamran on 4/12/2017.
 */

public class DonorAdapter extends BaseAdapter {

    private Activity activity;
    private LayoutInflater inflater;
    private List<DataSet> donorItems;

    public DonorAdapter(Activity activity,List<DataSet> donorItems) {
        this.activity = activity;
        this.donorItems = donorItems;

    }

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

    @Override
    public Object getItem(int location) {
        return donorItems.get(location);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.donor_search_list_theme, null);

        TextView nameTextView = (TextView) convertView.findViewById(R.id.donor_name);
        TextView areaTextView = (TextView) convertView.findViewById(R.id.donor_area);

        DataSet d = donorItems.get(position);

        nameTextView.setText(d.getName());
        areaTextView.setText(d.getArea());
        return convertView;
    }
}

这是我的型号代码:

package com.example.kamran.ebloodbank;

import android.widget.ArrayAdapter;

import java.util.ArrayList;

/**
 * Created by Kamran on 4/12/2017.
 */

public class DataSet {
    private String name;
    private int area;


    public void setArea(int area) {
        this.area = area;
    }

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

    public int getArea() {
        return area;
    }

    public String getName() {
        return name;
    }


}

这是我的第一个活动代码:

package com.example.kamran.ebloodbank;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.kamran.ebloodbank.data.BloodContract.BloodEntry;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FindDonor extends AppCompatActivity {

    Spinner bloodgroup;
    Spinner area;
    String findDonorUrl = "https://wwwkamranalitk.000webhostapp.com/findDonor.php";
    int mBlood = BloodEntry.BLOOD_GROUP_A_POS;
    int mArea = BloodEntry.DONOR_AREA_MALIR;
    Button searchButton;
    RequestQueue requestQueue;
    StringRequest request;
    JSONObject jsonObject;
    JSONArray result;
    private List<DataSet> donorList = new ArrayList<DataSet>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_donor);


        bloodgroup = (Spinner) findViewById(R.id.bloodspinnerfinddonor);
        area = (Spinner) findViewById(R.id.areaSpinner);
        searchButton = (Button) findViewById(R.id.searchDonor);
        setupSpinnerBlood();
        setupSpinnerArea();
        requestQueue = Volley.newRequestQueue(getApplicationContext());

        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                request = new StringRequest(Request.Method.POST, findDonorUrl, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                            try {
                                jsonObject = new JSONObject(response);
                                result = jsonObject.getJSONArray("result");
                                JSONObject donorData = result.getJSONObject(0);
                                DataSet donorSet = new DataSet();

                                donorSet.setName(donorData.getString("name"));
                                donorSet.setArea(donorData.getInt("area"));
                                donorList.add(donorSet);
                                Intent intent = new Intent(FindDonor.this, DonorSearchList.class);

                                startActivity(intent);
                                finish();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }){
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        HashMap<String,String> parameters = new HashMap<String, String>();

                        parameters.put("bloodgroup", String.valueOf(mBlood));
                        parameters.put("area", String.valueOf(mArea));

                        return parameters;
                    }
                };

                requestQueue.add(request);
            }
        });

    }




    private void setupSpinnerBlood() {
        ArrayAdapter bloodGroupSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.blood_group, android.R.layout.simple_spinner_item);
        bloodGroupSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        bloodgroup.setAdapter(bloodGroupSpinnerAdapter);

        bloodgroup.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selection = (String) parent.getItemAtPosition(position);
                if (!TextUtils.isEmpty(selection)) {
                    if (selection.equals(getString(R.string.blood_apos))) {
                        mBlood = BloodEntry.BLOOD_GROUP_A_POS;
                    } else if (selection.equals((getString(R.string.blood_bpos)))) {
                        mBlood = BloodEntry.BLOOD_GROUP_B_POS;
                    } else if (selection.equals(getString(R.string.blood_opos))) {
                        mBlood = BloodEntry.BLOOD_GROUP_O_POS;
                    } else if (selection.equals((getString(R.string.blood_oneg)))) {
                        mBlood = BloodEntry.BLOOD_GROUP_O_NEG;
                    }
                }
            }


            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                mBlood = BloodEntry.BLOOD_GROUP_A_POS;
            }
        });
    }

    private void setupSpinnerArea() {
        ArrayAdapter areaSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.area, android.R.layout.simple_spinner_item);
        areaSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        area.setAdapter(areaSpinnerAdapter);

        area.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selection = (String) parent.getItemAtPosition(position);
                if (!TextUtils.isEmpty(selection)) {
                    if (selection.equals(getString(R.string.donor_area_malir))) {
                        mArea = BloodEntry.DONOR_AREA_MALIR;
                    } else if (selection.equals(getString(R.string.donor_area_defence))) {
                        mArea = BloodEntry.DONOR_AREA_DEFENCE;
                    } else if (selection.equals(getString(R.string.donor_area_korangi))) {
                        mArea = BloodEntry.DONOR_AREA_KORANGI;
                    } else if (selection.equals(getString(R.string.donor_area_johar))) {
                        mArea = BloodEntry.DONOR_AREA_JOHAR;
                    }
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                mArea = BloodEntry.DONOR_AREA_MALIR;
            }
        });

    }


}

,这是显示数据的代码:

package com.example.kamran.ebloodbank;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DonorSearchList extends AppCompatActivity {

    private String mArea;
    private List<DataSet> donorList = new ArrayList<DataSet>();
    private ListView listView;
    private DonorAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_donor_search_list);

        listView = (ListView) findViewById(R.id.donorListView);
        adapter = new DonorAdapter(this, donorList);

        listView.setAdapter(adapter);



    }
}

1 个答案:

答案 0 :(得分:0)

您需要将数据从搜索传输到列表活动。 尝试这样的事情:

public class bridge {
   public static List<DataSet> donorList = new ArrayList<DataSet>();
}

然后,在FindDonor类中:

donorSet.setName(donorData.getString("name"));
donorSet.setArea(donorData.getInt("area"));
donorList.add(donorSet);
//here put this line
bridge.donarList = donarList;
//rest code
Intent intent = new Intent(FindDonor.this, DonorSearchList.class);

然后,在DonorSearchList类中:

adapter = new DonorAdapter(this, bridge.donorList);