Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • ListView is not getting displayed on selecting an item from spinner. What to do?

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 1.21k
    Answer it

    I am creating two spinners: One for City and the other for Area in that particular city. As per the selection made by the user in the above spinners, a ListView will be displayed which will be different for different combinations of selections made by the user in spinners.I am fetching the data for the Listview from the database stored in the Firebase.The URL mentioned in the java code would change everytime.But the listview itself is not getting displayed.

    The layout which has the spinners and the listview is as follows:

     

     

    I am creating two spinners: One for City and the other for Area in that particular city. As per the selection made by the user in the above spinners, a ListView will be displayed which will be different for different combinations of selections made by the user in spinners.I am fetching the data for the Listview from the database stored in the Firebase.The URL mentioned in the java code would change everytime.But the listview itself is not getting displayed.

    The layout which has the spinners and the listview is as follows:

    <?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"
        android:paddingTop="100dp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Select a City"
            />
    
        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/selectCity"
            android:entries="@array/city_name"
            android:textSize="20sp"
            android:singleLine="false"
            android:prompt="@string/city_title"
            android:outlineProvider="background"
    
            android:background="@android:drawable/btn_dropdown"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Select Area"
            android:paddingTop="20sp"
            />
        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/select_area"
            android:background="@android:drawable/btn_dropdown"
            >
        </Spinner>
        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
    
            android:id="@+id/displayAllAstrologers"
    
            ></ListView>
    
    </LinearLayout>

     

    The java fragment code through which I am handling spinners and Listview are:

     

     package com.example.hsports.weddingplanner.Fragments;
    
    
        import android.content.Context;
        import android.content.Intent;
        import android.content.SharedPreferences;
        import android.os.Bundle;
        import android.support.annotation.Nullable;
        import android.support.v4.app.Fragment;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.AdapterView;
        import android.widget.ArrayAdapter;
        import android.widget.ListView;
        import android.widget.Spinner;
    
        import com.example.hsports.weddingplanner.Activities.FrontPage;
        import com.example.hsports.weddingplanner.Adapters.displayingListCategories;
        import com.example.hsports.weddingplanner.R;
        import com.google.firebase.database.DataSnapshot;
        import com.google.firebase.database.DatabaseError;
        import com.google.firebase.database.DatabaseReference;
        import com.google.firebase.database.FirebaseDatabase;
        import com.google.firebase.database.ValueEventListener;
    
        import java.util.ArrayList;
    
        /**
         * Created by I324671 on 12/5/2016.
         */
        public class Location_Selection extends Fragment {
            ArrayList<String> listCategories=new ArrayList<>();
    
            String categorySelected;
    
            String citySelected="";
            String areaSelected="";
            public  Location_Selection(String categorySelected)
            {
                this.categorySelected= this.categorySelected;
            }
            @Nullable
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                View view=inflater.inflate(R.layout.city_selection_page,container,false);
                final Spinner areaSelector=(Spinner)view.findViewById(R.id.select_area);
                Spinner selectCity=(Spinner)view.findViewById(R.id.selectCity);
                final ListView listView = (ListView) view.findViewById(R.id.displayAllAstrologers);
               selectCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                   @Override
                   public void onItemSelected(AdapterView<?> parent, final View view, int position, long id) {
    
                       final ArrayList<String> listAreas = new ArrayList<String>();
    
                       String s = parent.getItemAtPosition(position).toString();
                       FirebaseDatabase database = FirebaseDatabase.getInstance();
                       DatabaseReference reference = database.getReference(s);
    
                       reference.addValueEventListener(new ValueEventListener() {
                           @Override
                           public void onDataChange(DataSnapshot dataSnapshot) {
    
                               if (dataSnapshot.hasChildren()) {
                                   for (DataSnapshot child : dataSnapshot.getChildren()) {
                                       String areaId = child.getKey().toString();
                                       String areaName = child.getValue().toString();
                                       listAreas.add(areaName);
                                   }
                               }
                               ArrayAdapter<String> adapter = new ArrayAdapter(view.getContext(),
                                       android.R.layout.simple_spinner_item, listAreas);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                               areaSelector.setAdapter(adapter);
                           }
                           @Override
                           public void onCancelled(DatabaseError databaseError) {
                           }
                       });
    
                   }
                   @Override
                   public void onNothingSelected(AdapterView<?> parent) {
    
                   }
               });
                areaSelector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        FirebaseDatabase database1 = FirebaseDatabase.getInstance();
                        DatabaseReference reference1 = database1.getReference("Astrologer-Hoodi/email");
                        reference1.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
    
                                if (dataSnapshot.hasChildren()) {
                                    for (DataSnapshot child : dataSnapshot.getChildren()) {
                                        String getEmailNo = child.getKey().toString();
                                        String getEmailId = child.getValue().toString();
                                        listCategories.add(getEmailId);
                                    }
                                }
    listView.setAdapter(new displayingListCategories(getContext(), listCategories));
                            }
                            @Override
                            public void onCancelled(DatabaseError databaseError) {
                            }
    
                        });
    
    
    
                    }
    
                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {
    
                    }
                });
    
                return  view;
            }
               }

     

    I am using a baseAdapter to inflate the ListView in the xml. So the Adapter clas which extends BaseAdapter is as follows:

     

    package com.example.hsports.weddingplanner.Adapters;
    
    import android.content.Context;
    import android.database.Cursor;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.CursorAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import com.example.hsports.weddingplanner.R;
    import com.google.firebase.database.DataSnapshot;
    import com.google.firebase.database.DatabaseError;
    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;
    import com.google.firebase.database.ValueEventListener;
    
    import java.util.ArrayList;
    
    /**
     * Created by I324671 on 12/5/2016.
     */
    public class displayingListCategories extends BaseAdapter {
    
        private Context mcontext;
    
        public ArrayList<String> listCategories;
    
        public displayingListCategories(Context context,ArrayList<String> listCategories) {
            //super(context, c);
            mcontext=context;
            this.listCategories=listCategories;
        }
    
    
        @Override
        public int getCount() {
            return 0;
        }
    
        @Override
        public Object getItem(int position) {
            return null;
        }
    
        @Override
        public long getItemId(int position) {
            return 0;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            View view= LayoutInflater.from(convertView.getContext()).inflate(R.layout.format_of_list_of_categories, parent, false);
            ImageView imageView=(ImageView)view.findViewById(R.id.image_of_customer);
            imageView.setImageResource(R.drawable.customer_photo);
            TextView nameOfCustomer=(TextView)view.findViewById(R.id.name_of_customer);
            TextView emailIdOfCustomer=(TextView)view.findViewById(R.id.email_id_of_customer);
            TextView phoneOfCustomer=(TextView)view.findViewById(R.id.phone_no_of_customer);
            nameOfCustomer.setText(listCategories.get(position));
            emailIdOfCustomer.setText(listCategories.get(position));
            phoneOfCustomer.setText(listCategories.get(position));
                return view;
        }
    }

     

    I am not getting the listview displayed. And I want the listview to be different for different selections in the spinners.

     

    Look for actual issue posted here:

 1 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: