Kalai Selvan
Kalai Selvan

Reputation: 3

Listview populated from Firebase onitemclicklistener

I have successfully populated a ListView from Firebase. I tried to implement OnItemClicklistener for the items in the ListView and push all the values of items to the next activity. As I am new to these i couldn't figure out how to do it.

Here is the code for viewing data from ListView:

package com.example.kselvan.mediacc;

import android.app.ProgressDialog;
import android.content.ClipData;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

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;
import java.util.List;

public class ViewDataActivity extends AppCompatActivity {
    ListView listView;
    List<Doctor> list;
    ProgressDialog progressDialog;
    private DatabaseReference databaseReference;
    MyAdapter myAdapter;

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

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

        list = new ArrayList<>();
        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("Fetching, please wait!");
        progressDialog.show();

        databaseReference = FirebaseDatabase.getInstance().getReference(Appointment.DATABASE_PATH);

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                progressDialog.dismiss();
                list.clear();

                for (DataSnapshot snap : dataSnapshot.getChildren()) {
                    Doctor doctor = snap.getValue(Doctor.class);
                    list.add(doctor);

                    myAdapter = new MyAdapter(ViewDataActivity.this,R.layout.data_items,list);
                    listView.setAdapter(myAdapter);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {}
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(ViewDataActivity.this,ViewProfile.class);
                intent.putExtra("doctor",listView.getItemAtPosition(position).toString());
            }
        });
    }
}

Here is the adapter:

package com.example.kselvan.mediacc;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by kalaiselvan on 2/20/2018.
 */

public class MyAdapter extends ArrayAdapter<Doctor>{

    Activity activity;
    int resource;
    List<Doctor> list;





    public MyAdapter(Activity activity, int resource, List<Doctor> list) {
        super(activity, resource,list);
        this.activity = activity;
        this.resource = resource;
        this.list = list;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        LayoutInflater layoutInflater = activity.getLayoutInflater();

        View view = layoutInflater.inflate(resource,null);

        ImageView imageView = (ImageView) view.findViewById(R.id.getImages);
        TextView name = (TextView) view.findViewById(R.id.getName);
        TextView email = (TextView) view.findViewById(R.id.getEmail);


        name.setText(list.get(position).getName());
        email.setText(list.get(position).getEmail());
        Glide.with(activity).load(list.get(position).getImageUri()).into(imageView);


        return view;
    }
}

Here is the class for getter and setter:


        enter code herepackage com.example.kselvan.mediacc;

    /**
     * Created by kalaiselvan on 2/19/2018.
     */

    public class Doctor {
        String name;
        String email;
        String imageUri;

        public Doctor() {
        }

        String mobile;

        public Doctor(String name, String email,String mobile, String imageUri) {
            this.name = name;
            this.email = email;
            this.imageUri = imageUri;
            this.mobile = mobile;
        }





        public String getMobile() {
            return mobile;
        }

        public void setMobile(String mobile) {
            this.mobile = mobile;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getImageUri() {
            return imageUri;
        }

        public void setImageUri(String imageUri) {
            this.imageUri = imageUri;
        }
    }

Here is data items xml

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

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/getImages"
        android:src="@mipmap/ic_launcher"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/getName"
        android:gravity="center"
        android:padding="10dp"
        android:text="name here"
        android:textSize="22sp"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/getEmail"
        android:gravity="center"
        android:padding="10dp"
        android:text="email here"
        android:textSize="22sp" />

</LinearLayout>

I tried so many ways, but none helped. Glad if someone could help me out.

Upvotes: 0

Views: 1109

Answers (1)

mbob
mbob

Reputation: 630

I assume that the item click works well. In this case, the issue is in your code here: intent.putExtra("doctor",listView.getItemAtPosition(position).toString());

You have to match the position of CLICKED ITEM with your actual list of doctors which is in your adapter.

You're doing this by finding the data bound to your listview adapter:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Intent intent = new Intent(ViewDataActivity.this,ViewProfile.class);

    Doctor doctor = (Doctor) parent.getAdapter().getItem(position);

    //here you have to serialize the object. 
    //A common way of doing this is sending the object as json. 
    //You can do it with GSON -> https://github.com/google/gson.
    intent.putExtra("doctor", new Gson().toJson(doctor));

   //you have also forgot to start the new activity :-)
   startActivity(intent);
   }
});

In the second activity, you have to deserialize it from json:

if(getIntent().hasExtra("data")) {
 String  data = getIntent().getStringExtra("data");
 Doctor doctor = new Gson().fromJson(data, Doctor.class);
}

IMPORTANT NOTE: make sure you search for the subject before posting a question. At least this one was discussed thousands of times over the internet :)

Upvotes: 1

Related Questions