简体   繁体   English

你好,我正在获取 json 数据,但没有显示在列表视图中

[英]hello i am getting json data but not display in to list view

my code is-1] CustomerList.java我的代码是-1] CustomerList.java

public class CustomerList extends Activity {

    private String TAG = CustomerList.class.getSimpleName();

    private ProgressDialog progressDialog;

    private ListView listView;

    ListViewAdapter listViewAdapter;

    ArrayList<HashMap<String, String>> getAllCustomers;

    static  String CUSTOMER_ID= "customer_id";
    static  String CUSTOMER_CONTACT_NUMBER="customer_contact_number";
    static  String CUSTOMER_NAME="customer_name";
    static  String ZONE="zone";



    private static String url = "http://103.229.245.235/cb.selectnetworks.in/getAllCustomers";

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


        getAllCustomers = new ArrayList<>();

        listView = (ListView) findViewById(R.id.ListViewCustomerList);

        new GetCustomerData().execute();
    }


    public class GetCustomerData extends AsyncTask<URL, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog=new ProgressDialog(CustomerList.this);
            progressDialog.setMessage("please wait");
            progressDialog.setCancelable(false);
            progressDialog.show();
        }

        @Override
        protected String doInBackground(URL... urls) {

            HttpHandler httpHandler= new HttpHandler();
            String jsonStr=httpHandler.makeServiceCall(url);
            Log.e(TAG,"Response from url: " +jsonStr);

            if(jsonStr !=null){
                try {
                    JSONObject jsonObject= new JSONObject(new String(jsonStr));

                    JSONArray arr= jsonObject.getJSONArray("data");

                    for(int i=0;i<arr.length();i++){

                        JSONObject obj = arr.optJSONObject(i);// (JSONObject) arr.get(i);
                        String customer_id  = obj.getString("customer_id");
                        String customer_name = obj.getString("customer_name");
                        String customer_contact_number = obj.getString("customer_contact_number");
                        String zone = obj.getString("zone_id");

//                        Toast.makeText(CustomerList.this,customer_id,Toast.LENGTH_LONG).show();


                        HashMap<String,String> customData = new HashMap<>();
                        customData.put("customer_id",customer_id);
                        customData.put("customer_name",customer_name);
                        customData.put("customer_contact_number",customer_contact_number);
                        customData.put("zone",zone);

                        getAllCustomers.add(customData);

                    }

                } catch (final JSONException e) {
                    Log.e(TAG,"json parsing error: "  + e.getMessage());
                        runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),"json error : " + e.getMessage(),Toast.LENGTH_LONG).show();
                        }
                    });

                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            if(progressDialog.isShowing())
                progressDialog.dismiss();
            Toast.makeText(CustomerList.this,getAllCustomers.toString(),Toast.LENGTH_LONG).show();
           // ListAdapter listAdapter = new SimpleAdapter(
                   // CustomerList.this,getAllCustomers,R.layout.activity_listviewitem,new String[]{"customer_id","customer_name",
                    //"customer_contact_number", "zone"}, new int[] {R.id.customer_id,R.id.customer_name,R.id.customer_mobile,R.id.customer_zone});
                       // listView.setAdapter(listAdapter);
            listView = (ListView)findViewById(R.id.ListViewCustomerList);
            listViewAdapter = new ListViewAdapter(CustomerList.this,getAllCustomers);
            listView.setAdapter(listViewAdapter);
                    }


    }
}

2]ListViewAdapter.java 2]ListViewAdapter.java

public class ListViewAdapter extends BaseAdapter {

    Context context;
    LayoutInflater  layoutInflater;
    ArrayList<HashMap<String, String>> data;
    HashMap<String, String> result = new HashMap<String, String>();


    public ListViewAdapter(Context context,ArrayList<HashMap<String , String>> getAllCustomers){
        this.context=context;
        data=getAllCustomers;
    }
    @Override
    public int getCount() {
        data.size();
        return 0;
    }

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

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

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

        TextView customer_id;
        TextView customer_name;
        TextView customer_contact_no;
        TextView zone;

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

        View itemview = layoutInflater.inflate(R.layout.activity_listviewitem,viewGroup,false);

        // get the position

        result= data.get(i);

        // assign variables values

        customer_id= (TextView) itemview.findViewById(R.id.TextViewcustomer_id);
        customer_name=(TextView) itemview.findViewById(R.id.TextVIewcustomer_name);
        customer_contact_no =(TextView) itemview.findViewById(R.id.TextViewcustomer_mobile);
        zone=(TextView) itemview.findViewById(R.id.TextViewcustomer_zone);

        // set the result into text view

        customer_id.setText(result.get(CustomerList.CUSTOMER_ID));
        customer_name.setText(result.get(CustomerList.CUSTOMER_NAME));
        customer_contact_no.setText(result.get(CustomerList.CUSTOMER_CONTACT_NUMBER));
        zone.setText(result.get(CustomerList.ZONE));

        itemview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                result=data.get(i);

            }
        });
        return itemview;
    }
}

3]HttpHandler class 3]HttpHandler类

public class HttpHandler {

    public HttpHandler(){

    }

    public String makeServiceCall(String reqUrl){

        String response=null;
        try {
            URL url = new URL(reqUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            // read the response
            InputStream in = new BufferedInputStream(conn.getInputStream());
            response = convertStreamToString(in);
        } catch (MalformedURLException e) {
            Log.e(TAG, "MalformedURLException: " + e.getMessage());
        } catch (ProtocolException e) {
            Log.e(TAG, "ProtocolException: " + e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, "IOException: " + e.getMessage());
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
        return response;
    }

    private String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line).append('\n');
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

}


4]xml file-activity_list:-


    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/EditTextSearch"
            />

        <ListView
            android:id="@+id/ListViewCustomerList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </ListView>

    </LinearLayout>

    5]actvity_listviewitem:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/TextViewcustomer_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="2dip"
            android:paddingTop="6dip"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="16sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/TextVIewcustomer_name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="2dip"
            android:textColor="@color/colorAccent" />

        <TextView
            android:id="@+id/TextViewcustomer_mobile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#5d5d5d"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/TextViewcustomer_zone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#5d5d5d"
            android:textStyle="bold" />


    </LinearLayout>

in your adapter change this method在您的适配器中更改此方法

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

this should solve your problem the getCount() method should return size of the list.这应该可以解决您的问题getCount()方法应该返回列表的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将php json url解析为android list view,这是代码我没有显示 - how to parse php json url into android list view here is to code i am getting no display 您好,我正在尝试将 url 中的 json 数据显示到列表视图中。 虽然什么都没有显示 - Hello i am trying to display json data from url into listview. Nothing is displayed though 显示JSON数据到列表视图 - Display json data to list view 将Json数据放入列表视图 - Getting Json data into a list view 我正在尝试使用JSON格式显示来自MySQL数据库的数据,但我一直收到以下错误 - I am trying to display data from MySQL database using JSON Format but i keep getting the following error 我正在尝试将数据从服务器显示到 Recycler View 但出现错误 - I am trying to display data from server to Recycler View but getting error 您好,我收到一个 json 字符串,我需要通过 class 属性分隔数据 - Hello, I am receiving a json string, I need to separate data by class properties 如何在 Android JAVA 的列表视图中显示部分但不是全部 JSON 数据 - How do I display some but not all JSON data in a list view in Android JAVA 如何从JSON解析器加载数据并将其显示在自定义列表视图中? - How can I load Data from JSON parser and display it in my customized list view? 我正在使用凌空库在列表视图上崩溃。 - I am getting crash on the list view using volley library .
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM