Listview not updated in first time I Have entered to Activity Sound

Question

1- MainActivity:

public class MainActivity extends AppCompatActivity {

    private List<String> mListItems = Arrays.asList("Hamaki","Amr Diab");
    private ArtistsAdapter mAdapter;

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

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

        mAdapter = new ArtistsAdapter(this, mListItems);
        listView.setAdapter(mAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    Intent intent = new Intent(MainActivity.this,SoundActivity.class);
                    intent.putExtra("Artist", mListItems.get(position));
                    startActivity(intent);
                }

        });

    }

}

2-Sound Activity:

public class SoundActivity extends AppCompatActivity {


    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sound);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().add(R.id.container, new SoundFragment()).commit();
        }

    }

3-Sound Fragment:

public class SoundFragment extends Fragment {

    static SCTrackAdapter mAdapter;
    static DatabaseReference db;
    static FirebaseHelper helper;
    private TextView mSelectedTrackTitle;
    static ArrayList<Music> mTracks = new ArrayList<>();
    static MediaPlayer mMediaPlayer;
    private ImageView mPlayerControl;
    static String Artist;
    static ListView listView;
    int currentTrack;
    private static String fileName;

    public SoundFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();

        new Fetchtracks().execute();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_sound, container, false);

        Intent intent = getActivity().getIntent();
        if (intent != null) {
            Artist = intent.getStringExtra("Artist");
        }

        listView = (ListView) rootView.findViewById(R.id.track_list_view);
        mAdapter = new SCTrackAdapter(getActivity(), mTracks);
        listView.setAdapter(mAdapter);
        return rootView;
    }

4- STrack Adapter:

public class SCTrackAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<Music> mTracks;

    public SCTrackAdapter(Context context, ArrayList<Music> tracks) {
        mContext = context;
        mTracks = tracks;
    }

    public void update_tracks(ArrayList<Music> list)
    {
        mTracks.clear();
        mTracks.addAll(list);
    }
    @Override
    public int getCount() {
        return mTracks.size();
    }

    @Override
    public Music getItem(int position) {
        return mTracks.get(position);
    }

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

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

        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.track_list_row, parent, false);
            holder = new ViewHolder();
            holder.titleTextView = (TextView) convertView.findViewById(R.id.track_title);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.titleTextView.setText(mTracks.get(position).getName());
        return convertView;
    }

    static class ViewHolder {
        TextView titleTextView;
    }

}

5- Fetchtracks:

public class Fetchtracks extends AsyncTask> {

@Override

protected ArrayList<Music> doInBackground(Void... voids) {

    db = FirebaseDatabase.getInstance().getReference().child(Artist);
    helper = new FirebaseHelper(db);
    mTracks.addAll(helper.retrieve());
   // Log.e("doInBackground: ",mTracks.size()+"");

    return mTracks;
}

@Override
protected void onPostExecute(ArrayList<Music> list) {
    super.onPostExecute(list);
    Log.e("doInBackground: ",list.size()+"");
    mAdapter.update_tracks(list);
    mAdapter.notifyDataSetChanged();
    listView.setAdapter(mAdapter);
}

}

6- FirebaseHelper:

public class FirebaseHelper {

DatabaseReference db;
Boolean saved=null;
ArrayList<Music> A = new ArrayList<>();

public FirebaseHelper(DatabaseReference db) {
    this.db = db;
}
//WRITE

public Boolean save(Music m)
{
    if(m==null)
    {
        saved=false;
    }else
    {
        try
        {
           // db.child(Artist).push().setValue(m);
            saved=true;
        }catch (DatabaseException e)
        {
            e.printStackTrace();
            saved=false;
        }
    }
    return saved;
}
//READ
public ArrayList<Music> retrieve()
{
    A.clear();
    db.addChildEventListener(new ChildEventListener() {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Log.e("onChildAdded: ", "1");
            fetchData(dataSnapshot);
        }
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            Log.e("onChildAdded: ", "2");
            fetchData(dataSnapshot);
        }
        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
           // Log.e("onChildAdded: ", "3");
        }
        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
           // Log.e("onChildAdded: ", "4");
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
           // Log.e("onChildAdded: ", "5");
        }
    });

    return A;
}
private void fetchData(DataSnapshot dataSnapshot)
{
        Music m=new Music();

        m.setName(dataSnapshot.child("name").getValue().toString());
        m.setUrl(dataSnapshot.child("url").getValue().toString());
        A.add(m);

// SoundFragment.mTracks.add(m); // Log.e("onFetch: ", SoundFragment.mTracks.size()+"");

}

}

7- Music:

public class Music {

    String name;
    String url;

    public Music() {

    }
    public void setName(String name) {
        this.name = name;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getName() {
        return name;
    }
    public String getURL() {
        return url;
    }
}

Show source
| android   2017-01-03 13:01 3 Answers

Answers ( 3 )

  1. 2017-01-03 13:01

    in doInBackground() mTracks is empty

    return value is empty

  2. 2017-01-03 14:01

    doInBackground() is always returning mTracks which is not modified before hence an empty list is return! you need to return data receive from helper.retrieve(); and onPostExecute() pass data to adapter!

  3. 2017-01-03 14:01

    Check mTracks list size in onCreateView() where you set the adapter to your listview.

    Possibly your tracks request not completed(doInBackground() is running) and fragment is displayed on screen with empty list.

◀ Go back