ListView没有在android中显示值

时间:2014-04-07 07:41:10

标签: android android-listview android-adapter android-webservice

我用一些字段制作了一个非常简单的表单,因为我已经为国家列表放了一个ListView,我从webservice获取所有国家名称,我得到了所有数据,但是我无法在ListView中设置它,我的ListView不可见,请帮帮我。谢谢你,我的代码如下:

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;

    }
}

1 个答案:

答案 0 :(得分:0)

它没有显示任何内容,因为您没有填满要显示的数据集。您可以在onCreate中创建适配器,但在数据集为空时。 Imo你应该从

更改Adapter构造函数
public CountryAdapter(Context paramContext, ArrayList<String> paramArrayList) {

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

当调用onPostExecuted时,您创建一个新的CountryAdapter并将其提交给ListView。当然,您必须调整适配器以反映适配器构造函数的更改