I want data of arraylist ,List songs = new ArrayList<>(); , on my SlideAdapter. I have two addapter one is "Adapter.java" and second one is "SliderAdapert.java" I want to use List songs = new ArrayList<>(); on both adapter
private void fetchSongs() {
//define list to carry songs
List<ModelClass> songs = new ArrayList<>();
Uri songLibraryUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
songLibraryUri = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
} else {
songLibraryUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
//projection
String[] projection = new String[]{
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DATE_MODIFIED,
MediaStore.Audio.Media.DATA
};
//sort order
String sortOrder = MediaStore.Audio.Media.DATE_ADDED + " DESC" ;
//Querying
try (Cursor cursor = getContentResolver().query(songLibraryUri, projection, null, null, sortOrder)) {
// cache the cursor indices
int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
int nameColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
int dateColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATE_MODIFIED);
int pathColimn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
//getting the values
while (cursor.moveToNext()) {
// get values of colums for a give audio files
long id = cursor.getLong(idColumn);
String name = cursor.getString(nameColumn);
long date = cursor.getLong(dateColumn);
String path = cursor.getString(pathColimn);
//song uri
Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
//album art uri
// Uri albumartUri = ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"),alb)
//remove .mp3 extension on song's name
name = name.substring(0, name.lastIndexOf("."));
// song item
ModelClass song = new ModelClass(path, name, uri, date);
// add song to songs list
songs.add(song);
}
//show songs on rv
showSongs(songs);
Toast.makeText(this, "Number of Songs:" +songs.size(), Toast.LENGTH_SHORT).show();
}
}
private void showSongs(List<ModelClass> songs) {
// songs.clear();
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(RecyclerView.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
adapter = new Adapter(songs);
recyclerView.setAdapter(adapter);
// adapter.notifyDataSetChanged();
}
I want List < ModalClass> songs = new ArrayList<>(); on my SlideAdapter.java
public class SlideAdapter extends PagerAdapter {
Context context;
LayoutInflater layoutInflater;
RelativeLayout relativeLayout1, relativeLayout2;
public static final String MyPREFERENCES = "MyPrefs";
public static final String Name = "nameKey";
public static final String Phone = "phoneKey";
public static final String Email = "emailKey";
SharedPreferences sharedpreferences;
public SlideAdapter(Context context) {
this.context = context;
}
//Array
public int[] slide_images = {
R.drawable.intro_call,
R.drawable.intro_call4,
R.drawable.intro_call4
};
public String[] slide_heading = {
"Please allow auto call recording on your device to get better experience",
"Please make a dummy call",
"Is this is your recorded dummy call ?"
};
public String[] slide_btn = {
"I Got it, Continue",
"Please Make a dummy call",
"No"
};
public String[] slide_des = {
"",
"It helps us to take accurate location of your recorded calls",
""
};
@Override
public int getCount() {
return slide_heading.length;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == (RelativeLayout) object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.intro_item_layout, container, false);
TextView head = (TextView) view.findViewById(R.id.introtext);
TextView head2 = (TextView) view.findViewById(R.id.introtext2);
TextView des = (TextView) view.findViewById(R.id.introdesc);
TextView recHead = (TextView) view.findViewById(R.id.recText);
TextView recTime = (TextView) view.findViewById(R.id.recTime);
TextView recSource = (TextView) view.findViewById(R.id.recSource);
ImageView img = (ImageView) view.findViewById(R.id.introImg);
Button btn = (Button) view.findViewById(R.id.introB);
Button noB = (Button) view.findViewById(R.id.noB);
Button yesB = (Button) view.findViewById(R.id.yesB);
ImageView playB = (ImageView) view.findViewById(R.id.play);
ImageView pauseB = (ImageView) view.findViewById(R.id.pause);
relativeLayout1 = (RelativeLayout) view.findViewById(R.id.relative1);
relativeLayout2 = (RelativeLayout) view.findViewById(R.id.relativ2);
relativeLayout1.setVisibility(View.VISIBLE);
relativeLayout2.setVisibility(View.GONE);
img.setImageResource(slide_images[position]);
head.setText(slide_heading[position]);
des.setText(slide_des[position]);
btn.setText(slide_btn[position]);
if (position == 1) {
sharedpreferences = context.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String n = "ed1.getText().toString()";
String ph = "ed2.getText().toString()";
String e = "ed3.getText().toString()";
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Name, n);
editor.putString(Phone, ph);
editor.putString(Email, e);
editor.commit();
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:"));//change the number
context.startActivity(callIntent);
}
});
}
if (position == 2) {
relativeLayout2.setVisibility(View.VISIBLE);
relativeLayout1.setVisibility(View.INVISIBLE);
// I want the data of the List <ModalClass> songs = new ArrayList<>();
// here
}
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
//container.removeView(container);
Intent intent = new Intent(context, MainActivity.class);
context.startActivity(intent);
}
}