Click here to Skip to main content
15,887,349 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my recycleview in android v7.support display the list sometime and it dont display the list when i restart my app from my phone it can able to count all the audio file but when i set it to textview it just display the only the last added data in the list view this are my code please help me...and please tell me how to display Recycle list data concurrent with using the Androi's new AsyncListUtil
Java
package fragment.dev.concurrentmedia;

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.DefaultItemAnimator;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;

    public class MainActivity extends AppCompatActivity {

        RecyclerView recyclerView;
        RecAdapter adapter;

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

            recyclerView = (RecyclerView) findViewById (R.id.recycleview);
            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager (getApplicationContext ());
            recyclerView.setLayoutManager (layoutManager);
            recyclerView.setItemAnimator (new DefaultItemAnimator ());
            recyclerView.setHasFixedSize (true);

            adapter = new RecAdapter (getApplicationContext ());
            recyclerView.setAdapter (adapter);
        }
    }


Java
package fragment.dev.concurrentmedia;

/**
 * Created by Jagdish on 6/19/2016.
 */
public class AudioModel {
    private String displayName;
    private String duration;
    private String path;

    public String getDisplayName () {
        return displayName;
    }

    public void setDisplayName (String displayName) {
        this.displayName = displayName;
    }

    public String getDuration () {
        return duration;
    }

    public void setDuration (String duration) {
        this.duration = duration;
    }

    public String getPath () {
        return path;
    }

    public void setPath (String path) {
        this.path = path;
    }
}


Java
package fragment.dev.concurrentmedia;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jagdish on 6/19/2016.
 */
public class RecAdapter extends RecyclerView.Adapter<RecAdapter.MyViewHolder> implements AudioSync.Response {
    public List<AudioModel> models = new ArrayList<> ();

    public RecAdapter (Context context) {
        AudioSync sync = new AudioSync ();
        sync.delegate = this;
        sync.execute (context);
    }

    @Override
    public MyViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from (parent.getContext ()).inflate (R.layout.recycle_layout, parent, false);
        return new MyViewHolder (itemView);
    }

    @Override
    public void onBindViewHolder (MyViewHolder holder, int position) {
        AudioModel audioModel = models.get (position);
        holder.txtName.setText (audioModel.getDisplayName ());
        holder.txtDuration.setText (audioModel.getDuration ());
    }

    @Override
    public int getItemCount () {
        return models.size ();
    }

    @Override
    public void processFinish (List<AudioModel> output) {
        for (int i = 0; i < output.size (); i++) {
            models.add (output.get (i));
        }
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView txtName;
        public TextView txtDuration;

        public MyViewHolder (View itemView) {
            super (itemView);
            txtName = (TextView) itemView.findViewById (R.id.txtName);
            txtDuration = (TextView) itemView.findViewById (R.id.txtDuration);
        }
    }
}


Java
package fragment.dev.concurrentmedia;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;
import java.util.ArrayList;
import java.util.List;
/**
 * Created by Jagdish on 6/19/2016.
 */
public class AudioSync extends AsyncTask<Context, Void, List<AudioModel>> {
    public Response delegate = null;
    private String selection;
    private Uri uri;
    private Cursor cursor;
    @Override
    protected List<AudioModel> doInBackground (Context... params) {
        List<AudioModel> modelList = new ArrayList<> ();
        AudioModel model = new AudioModel ();
        try {
            selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
            uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            String projection[] = {MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DURATION};
            cursor = params[0].getContentResolver ().query (uri, projection, selection, null, null);
            cursor.moveToFirst ();
            int i = 0;
            do {
                model.setPath (cursor.getString (cursor.getColumnIndexOrThrow (MediaStore.Audio.Media.DATA)));
                model.setDisplayName (cursor.getString (cursor.getColumnIndexOrThrow (MediaStore.Audio.Media.DISPLAY_NAME)));
                model.setDuration (cursor.getString (cursor.getColumnIndexOrThrow (MediaStore.Audio.Media.DURATION)));
                modelList.add (model);
            } while (cursor.moveToNext ());
            return modelList;
        } catch (Exception e) {
            L.l ("EXCEPTION", e.getStackTrace ());
            return modelList;
        }
    }
    @Override
    protected void onPostExecute (List<AudioModel> audioModels) {
        delegate.processFinish (audioModels);
    }
    public interface Response {
        void processFinish (List<AudioModel> output);
    }
}

this is my first post with code so if i had any mistake then sorry but please help me thanks in advance

What I have tried:

im trying to google for it but its not working all site give the same result as i typed but i dont know how my code working like this
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900