-1

I am building an android app in which when the user create its account as a technician his name id display in a listview in which other users which don't have firebase outh.the problem is that I am unable to show data of technicians in the listview.

This is my firebase database

image

This is my code that return 0 items or blank listview. I tried several things, but unable to display the information of technicians. Anyone please help me with this I am new in firebase.

    public class Technician extends Activity {

    ListView listView;
    ArrayList<String> tecnames = new ArrayList<>();

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

        ImageButton Back_Btn = (ImageButton) findViewById(R.id.back);
        Back_Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(Technician.this, Technician_main.class);
                startActivity(i);
                finish();
            }
        });

        listView = (ListView) findViewById(R.id.list_item);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


                Intent intent = new Intent(Technician.this, Book_Now.class);
                startActivity(intent);
                finish();
            }
        });

        Technician.CostumAdapter costumAdapter = new Technician.CostumAdapter();
        listView.setAdapter(costumAdapter);
        GetItems();
    }

    private class CostumAdapter extends BaseAdapter {


        @Override
        public int getCount() {
            return tecnames.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            view = getLayoutInflater().inflate(R.layout.cuctom_listview, null);

            TextView txt_one = (TextView) view.findViewById(R.id.skill_txt);
            txt_one.setText(tecnames.get(i));
            return view;
        }

    }

    private void GetItems() {
        DatabaseReference database = FirebaseDatabase.getInstance().getReference();
        DatabaseReference ref = database.child("user");
        final Query itemsQuery = ref.orderByChild("name");

        itemsQuery.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
                        Pojo item = singleSnapshot.getValue(Pojo.class);
                        tecnames.add(String.valueOf(item));
                        Log.e(TAG,"value of name"+itemsQuery);
                    }
                }else {
                    Toast.makeText(getApplicationContext(),"error",Toast.LENGTH_LONG).show();
                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        Log.e(TAG, "Returning " + tecnames.size() + " items");

    }
}
KENdi
  • 7,576
  • 2
  • 16
  • 31
Ehtisham habib
  • 63
  • 1
  • 2
  • 9

2 Answers2

0

instead of this:

  Technician.CostumAdapter costumAdapter = new Technician.CostumAdapter();

Write this:

 Technician.CostumAdapter costumAdapter = new Technician.CostumAdapter(getApplicationContext(),tecnames);

Then add this constructor inside of the class:

private ArrayList<String> datas;
Context mContext;
public CustomAdapter(Context context, ArrayList<String> datas) {
    super(context, R.layout.row_item, datas);
    this.mContext=context;
    this.datas = datas;

}

change addListenerforSingleValueEvent to addValueEventListener

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
0

Ok, there are few things that you need to fix:

1.Your array list tecnames should be of your POJO class

ArrayList<POJO> tecnames = new ArrayList<>();

Now change tecnames.add(String.valueOf(item)); to tecnames.add(item);

2.As @Peter Haddad suggested, you need to give this list to your adapter as a dataset.

3.Firebase DB listeners are asynchronous so you need to notify your adapter after you get data in your dataset(list). Add the following code inside onDataChange after your foreach loop:

costumAdapter.notifyDataSetChanged();

You can refer to this link for the 3rd point.

Shruti
  • 803
  • 9
  • 26