自定义列表不显示

时间:2016-08-10 17:36:32

标签: android listview firebase custom-lists

我正在从firebase中检索数据以填充我的自定义列表。我的数据在onChildAdded事件中被检索,但它不会添加到我的自定义列表中。我的自定义列表为空

这是我设置列表适配器的主要活动

package com.example.manas.worknhire;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;

import com.google.firebase.database.ChildEventListener;
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 java.util.ArrayList;

public class work_available_list extends AppCompatActivity implements         ChildEventListener {
    ListView work_list;
    newJob temp=new newJob();
    ArrayList<newJob> job1=new ArrayList<newJob>();
    FirebaseDatabase db=FirebaseDatabase.getInstance();
    DatabaseReference myref=db.getReference();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work_available_list);
        work_list=(ListView)findViewById(R.id.work_list_items);
        myref=myref.child("jobsPosted");
        myref.addChildEventListener(this);
        CustomList list1=new CustomList(this,job1);
        work_list.setAdapter(list1);



    }

    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        temp=dataSnapshot.getValue(newJob.class);
        job1.add(temp);



    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {

    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
}

这是我的自定义列表代码

package com.example.manas.worknhire;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;

/**
* Created by Manas on 7/12/2016.
*/
public class CustomList extends ArrayAdapter<String>{
    private final ArrayList<newJob> job1;
    private final Activity context;

    public CustomList(Activity context, ArrayList<newJob> job1) {
        super(context,R.layout.list_row);
        this.job1=job1;
        this.context = context;
    }



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

        LayoutInflater inflater=context.getLayoutInflater();
        View rowView=inflater.inflate(R.layout.list_row, null, true);
        TextView title=(TextView)rowView.findViewById(R.id.title_job);
        TextView description=(TextView)rowView.findViewById(R.id.description_job);
        TextView postedBy=(TextView)rowView.findViewById(R.id.postedBy_job);
        Button contact=(Button)rowView.findViewById(R.id.contact_job);
        Log.w("title",job1.get(position).getJob_title());
        title.setText(job1.get(position).getJob_title());
        description.setText(job1.get(position).getJob_description());
        postedBy.setText(job1.get(position).getName());

        return rowView;
    }
}

这是listrow的快照

enter image description here

1 个答案:

答案 0 :(得分:0)

您的列表不是空的,适配器只是不知道您添加了任何内容。

@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    temp=dataSnapshot.getValue(newJob.class);
    job1.add(temp);
    // Need to update the ListView Adapter
}

您需要在适配器上拨打notifyDataSetChanged(),或者您应该使用adapter.add()代替job1.add()

注意:我说适配器,但您的类名为CustomList。您可能希望坚持使用命名约定并命名更具描述性的内容,并将Adapter放在最后。同样,work_available_list Java类是CamelCased,而不是lowercase_words,它应该是 - Activity