简体   繁体   中英

Custom Adapter not working Properly in android

I hakve made a custom adaptor for binding parsed data into a listView,I have tried as below,But it only gives me 1st value .please help me.I want to bind all the values to listView but currently i only able to bind only 1\\one value in it.. Registration.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.WholesaleProductAdapter;
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 EditText contact;
    private EditText password;
    private ListView countrylist;
    private ListView statelist;

    private TextView tv_country;

    private TextView state;
    JSONArray countries = null;

    // Fetching List

    JSONObject jsonObj;
    JSONArray contries = null;
    ArrayList<HashMap<String, String>> countryList;
    private CountryAdapter countryContent;

    // Hashmap for ListView

    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);
        tv_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 = (ListView) findViewById(R.id.country_list);
        countrylist.setVisibility(View.GONE);
        statelist = (ListView) findViewById(R.id.state_list);
        countryList = new ArrayList<HashMap<String, String>>();

        regMenu.setSelectedTab(1);
        new GetCountries().execute();
        countrylist.setVisibility(View.GONE);

        // 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();

                tv_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 ((!tv_country.getText().toString().equals("") && tv_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));
                            tv_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...!!!!
        tv_country.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                countrylist.setVisibility(View.VISIBLE);

            }
        });

    }

    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() {

        }

        @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);
            try {
                if (jsonStr != null) {

                    jsonObj = new JSONObject(jsonStr);
                    if (jsonObj.has(Const.TAG_COUNTY_LIST)) {
                        contries = jsonObj.getJSONArray(Const.TAG_COUNTY_LIST);
                        System.out.println(":::::::::PRODUCT lENGTH:::::::::::" + contries.length());
                        if (contries != null && contries.length() != 0) {
                            // looping through All Contacts

                            for (int i = 0; i < contries.length(); i++) {
                                JSONObject c = contries.getJSONObject(i);

                                String Con_id = c.getString(Const.TAG_COUNTRY_ID);
                                System.out.println("::::::::::::::::::PARSING PRODUCT ID:::::::::::::" + Con_id);
                                String CountryName = c.getString(Const.TAG_COUNTRY_NAME);

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

                                // adding each child node to HashMap key =>
                                // value
                                country.put(Const.TAG_COUNTRY_ID, Con_id);
                                country.put(Const.TAG_COUNTRY_NAME, CountryName);
                                countryList.add(country);

                            }
                        }
                    }

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

            } catch (JSONException e) {
                e.printStackTrace();
                System.out.println("::::::::::::::::::got an error::::::::::::");
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog

            countryContent = new CountryAdapter(RegistrationActivity.this, countryList);
            countrylist.setAdapter(countryContent);

        }
    }

}

Adapter.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.ImageView;
import android.widget.TextView;

import com.epe.yehki.util.Const;
import com.example.yehki.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

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

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

    }

    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.con_id = ((TextView) paramView.findViewById(R.id.cat_id));
            localViewholder.con_name = ((TextView) paramView.findViewById(R.id.name));

            paramView.setTag(localViewholder);

        } else {
            localViewholder = new Viewholder();
            localViewholder = (Viewholder) paramView.getTag();
        }

        localViewholder.con_name.setText(countryArray.get(paramInt).get(Const.TAG_COUNTRY_NAME));
        System.out.println("::::::::::::::::::;PRDUCT name" + countryArray.get(paramInt).get(Const.TAG_COUNTRY_NAME));

        return paramView;

    }

    static class Viewholder {
        TextView con_id;
        TextView con_name;

    }
}

remove localViewholder = new Viewholder(); from getView else condition. Here you are creating a new holder object every time.

It should be

else {
        localViewholder = (Viewholder) paramView.getTag();
     }

Please update the line

LayoutInflater localLayoutInflater = (LayoutInflater) this.mContext.getSystemService("layout_inflater");

to

LayoutInflater localLayoutInflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Please try the following class:

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

    public a(Context paramContext, ArrayList<HashMap<String, String>> countryList) {
        this.mContext = paramContext;
        this.countryArray = countryList;
    }

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

    public Object getItem(int paramInt) {
        return countryArray.get(paramInt);
    }

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


    @SuppressWarnings("static-access")
    public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
        Viewholder localViewholder;
        LayoutInflater localLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (paramView == null) {
            paramView = localLayoutInflater.inflate(R.layout.list_item, paramViewGroup, false);
            localViewholder = new Viewholder();
            localViewholder.con_id = ((TextView) paramView.findViewById(R.id.cat_id));
            localViewholder.con_name = ((TextView) paramView.findViewById(R.id.name));
            paramView.setTag(localViewholder);
        } else {
            localViewholder = (Viewholder) paramView.getTag();
        }

        HashMap<String, String> data = (HashMap<String, String>) getItem(paramInt);
        localViewholder.con_name.setText(data.get(Const.TAG_COUNTRY_NAME));
        return paramView;
    }

    static class Viewholder {
        TextView con_id;
        TextView con_name;

    }
}

Please note you can't use System.out.println on Android , please use Log class instead: Log.d("myTag", "message") .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM