0

I have made a very simple, form with certain fields,In that i have put a ListView for country List,I am fetching all country names from a webservice,I got all data,But i am unable to set it in ListView,My ListView not visible,Please help me for it.thank you my code is as below:

RegistrationActivity.java

    package com.epe.yehki.ui;

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

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

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.epe.yehki.adapter.CountryAdapter;
import com.epe.yehki.adapter.ProductAdapter;
import com.epe.yehki.backend.BackendAPIService;
import com.epe.yehki.backend.RegisterationAPI;
import com.epe.yehki.backend.ResponseListener;
import com.epe.yehki.uc.Menu;
import com.epe.yehki.util.Const;
import com.epe.yehki.util.Const.API_RESULT;
import com.example.yehki.R;

public class RegistrationActivity extends Activity {
    public RegisterationAPI registartionAPI;
    private EditText fName;
    private EditText lName;
    private EditText eMail;
    private Button register;
    private CountryAdapter countryContent;

    private EditText contact;
    private EditText password;
    private ListView countrylist;
    private ListView statelist;

    public static ArrayList<String> countryArray;
    private TextView country;

    private TextView state;
    JSONArray countries = null;

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> countryList;
    ArrayList<HashMap<String, String>> stateList;

    public com.epe.yehki.uc.Menu regMenu;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_registration);

        // FINDING IDS...
        fName = (EditText) findViewById(R.id.et_fname);
        lName = (EditText) findViewById(R.id.et_lname);
        eMail = (EditText) findViewById(R.id.et_email);
        contact = (EditText) findViewById(R.id.et_contact);
        password = (EditText) findViewById(R.id.et_pwd);
        country = (TextView) findViewById(R.id.et_country);
        state = (TextView) findViewById(R.id.et_state);
        regMenu = (Menu) findViewById(R.id.menuReg);
        register = (Button) findViewById(R.id.btn_register);
        countryList = new ArrayList<HashMap<String, String>>();
        countrylist = (ListView) findViewById(R.id.country_list);
        statelist = (ListView) findViewById(R.id.state_list);

        countryArray = new ArrayList<String>();

        regMenu.setSelectedTab(1);

        // COUNTRY LIST CLICK EVENT...!!!
        countrylist.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // getting values from selected ListItem

                String countryname = ((TextView) view.findViewById(R.id.name)).getText().toString();

                country.setText(countryname);
                // countrylist.setVisibility(view.GONE);
            }
        });

        // REGISTER BUTTOM CLICK EVENT.....!

        register.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (!fName.getText().toString().equals("") && fName.getText().toString() != null) {
                    if ((!lName.getText().toString().equals("") && lName.getText().toString() != null)) {
                        if ((!country.getText().toString().equals("") && country.getText().toString() != null)) {
                            if ((!state.getText().toString().equals("") && state.getText().toString() != null)) {
                                if ((!eMail.getText().toString().equals("") && eMail.getText().toString() != null)) {
                                    if ((!password.getText().toString().equals("") && password.getText().toString() != null)) {
                                        if ((!contact.getText().toString().equals("") && contact.getText().toString() != null)) {

                                        } else {// CONTACT NUMBER
                                            validation(getResources().getString(R.string.valid_number));
                                            password.requestFocus();
                                        }

                                    } else {// password...
                                        validation(getResources().getString(R.string.valid_pwd));
                                        password.requestFocus();
                                    }

                                } else {// EMAIL
                                    validation(getResources().getString(R.string.valid_mail));
                                    eMail.requestFocus();
                                }

                            } else {// STATE
                                validation(getResources().getString(R.string.valid_state));
                                state.requestFocus();

                            }
                        } else {// COUNTRY
                            validation(getResources().getString(R.string.valid_country));
                            country.requestFocus();

                        }

                    } else {// LAST NAME
                        validation(getResources().getString(R.string.valid_lname));
                        lName.requestFocus();

                    }
                } else {// FIRST NAME
                    validation(getResources().getString(R.string.valid_fname));
                    fName.requestFocus();
                }
            }
        });
        // COUINTRY LIST CLICK EVENT...!!!!
        country.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                countrylist.setVisibility(View.VISIBLE);
                new GetCountries().execute();
            }
        });

    }

    void validation(String msg) {

        Toast.makeText(RegistrationActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    ResponseListener responseListener = new ResponseListener() {

        @Override
        public void onResponce(String api, API_RESULT result, Object obj) {
            System.out.println("::::::::::::::inside response Listener::::::::");

            if (progressDialog != null && progressDialog.isShowing())
                progressDialog.dismiss();

            if (api.equals(Const.API_LOGIN)) {
                System.out.println("::::::::::::::inside response Listener::::::::1");

                if (result == Const.API_RESULT.SUCCESS) {

                    registartionAPI = null;

                    // as
                    // doctor
                    System.out.println("success Login::::::::::>>>>>>>>>>>>>>>>>>" + result);
                    startActivity(new Intent(RegistrationActivity.this, HomeActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));

                } else {
                    System.out.println("failed Login::::::::::>>>>>>>>>>>>>>>>>>" + result);
                }
            }

        }
    };

    // ASYNC TASK FOR GETTING COUNTRY LIST...!!!

    private class GetCountries extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            /*
             * pDialog = new ProgressDialog(CategoryActivity.this);
             * pDialog.setMessage("Please wait...");
             * pDialog.setCancelable(false); pDialog.show();
             */
            System.out.println("==========inside preexecute===================");

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            BackendAPIService sh = new BackendAPIService();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(Const.API_COUTRY, BackendAPIService.GET);

            Log.d("Response: ", "> " + jsonStr);
            System.out.println("=============MY RESPONSE========== FOR COUNTRYlIST" + jsonStr);

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

                    // Getting JSON Array node
                    if (jsonObj.has(Const.TAG_COUNTY_LIST)) {

                        // looping through All Contacts

                        countries = jsonObj.getJSONArray(Const.TAG_COUNTY_LIST);
                        for (int i = 0; i < countries.length(); i++) {
                            JSONObject c = countries.getJSONObject(i);

                            String cId = c.getString(Const.TAG_COUNTRY_ID);
                            String countryName = c.getString(Const.TAG_COUNTRY_NAME);
                            System.out.println("::::::::::::CountryId:::::::::::::" + countryName);

                            HashMap<String, String> country = new HashMap<String, String>();

                            country.put(Const.TAG_COUNTRY_ID, cId);
                            country.put(Const.TAG_COUNTRY_NAME, countryName);

                            countryList.add(country);

                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            countryContent = new CountryAdapter(RegistrationActivity.this, countryList);
            countrylist.setAdapter(countryContent);

        }

    }
}

CountryAdapter.java

package com.epe.yehki.adapter;

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

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 com.epe.yehki.util.Const;
import com.example.yehki.R;

public class CountryAdapter extends BaseAdapter {
    public ArrayList<HashMap<String, String>> countryArray;
    private Context mContext;

    public CountryAdapter(Context paramContext, ArrayList<HashMap<String, String>> cuntryList) {
        this.mContext = paramContext;
        this.countryArray = cuntryList;

    }

    public int getCount() {
        return this.countryArray.size();
    }

    public Object getItem(int paramInt) {
        return Integer.valueOf(paramInt);
    }

    public long getItemId(int paramInt) {
        return paramInt;
    }

    @SuppressWarnings("static-access")
    public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
        LayoutInflater localLayoutInflater = (LayoutInflater) this.mContext.getSystemService("layout_inflater");
        Viewholder localViewholder = null;
        if (paramView == null) {
            paramView = localLayoutInflater.inflate(R.layout.list_item, paramViewGroup, false);
            localViewholder = new Viewholder();
            localViewholder.countryid = ((TextView) paramView.findViewById(R.id.cat_id));
            localViewholder.countryName = ((TextView) paramView.findViewById(R.id.name));

            paramView.setTag(localViewholder);

        } else {
            localViewholder = new Viewholder();
            localViewholder = (Viewholder) paramView.getTag();
        }
        System.out.println("::::::::::::::array indexes::::::::::::" + countryArray.get(paramInt));

        localViewholder.countryName.setText(countryArray.get(paramInt).get(Const.TAG_COUNTRY_NAME));
        localViewholder.countryid.setText(countryArray.get(paramInt).get(Const.TAG_COUNTRY_ID));

        return paramView;

    }

    static class Viewholder {
        TextView countryName;
        TextView countryid;

    }
}
Jigar jims
  • 157
  • 3
  • 15

1 Answers1

0

It is display nothing because you do not fill up the dataset you want to display. You create the Adapter in onCreate but at the time the dataset is empty. Imo you should change the Adapter constructor from

public CountryAdapter(Context paramContext, ArrayList<String> paramArrayList) {

to

public CountryAdapter(Context paramContext, ArrayList<HashMap<String, String>> paramArrayList) {

and when onPostExecuted is called, you create a new CountryAdapter and submit it to the ListView. Of course you have to adjust your Adapter in order to reflect the changes of the Adapter's constructor

Blackbelt
  • 156,034
  • 29
  • 297
  • 305