我尝试在回收器视图中添加任何新项目时自动发布数据
我已经尝试过单击项目,它可以正常工作,但是当任何项目添加到回收站视图中时,我想自动发布
class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
public ArrayList<String> songNames;
Context context;
// MediaPlayer mediaPlayer = new MediaPlayer();
boolean wasPlaying = false;
Handler seekHandler = new Handler();
Runnable run;
public RecyclerViewAdapter(ArrayList<String> songNames, Context context) {
this.songNames = songNames;
this.context = context;
}
@NonNull
@Override
public RecyclerViewAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View listItem = LayoutInflater.from(parent.getContext()).inflate(R.layout.record_card, parent, false);
return new MyViewHolder(listItem);
}
@Override
public void onBindViewHolder(@NonNull final RecyclerViewAdapter.MyViewHolder holder, final int position) {
holder.textView.setText(songNames.get(position));
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.playerCard.setVisibility(View.VISIBLE);
boolean connected = false;
ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(context.CONNECTIVITY_SERVICE);
if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
//we are connected to a network
Toast.makeText(context, "Internet is working", Toast.LENGTH_SHORT).show();
connected = true;
postingData(position, holder);
if (connected == true ) {
}
}
else{
connected = false;
Toast.makeText(context, "net not", Toast.LENGTH_SHORT).show();
}
}
});
holder.cardView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.playerCard.setVisibility(View.VISIBLE);
}
});
final MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(CallRecoding.songs.get(position));
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
holder.seekBar.setMax(mediaPlayer.getDuration());
holder.seekBar.setTag(position);
holder.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (mediaPlayer != null && fromUser) {
mediaPlayer.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
holder.seekBarHint.setText("0:00/" + calculateDuration(mediaPlayer.getDuration()));
holder.playsong.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
holder.pauseRec.setVisibility(View.VISIBLE);
holder.playsong.setVisibility(View.INVISIBLE);
holder.playsong.setText(" Pause ");
run = new Runnable() {
@Override
public void run() {
// Updateing SeekBar every 100 miliseconds
holder.seekBar.setProgress(mediaPlayer.getCurrentPosition());
seekHandler.postDelayed(run, 100);
//For Showing time of audio(inside runnable)
int miliSeconds = mediaPlayer.getCurrentPosition();
if (miliSeconds != 0) {
//if audio is playing, showing current time;
long minutes = TimeUnit.MILLISECONDS.toMinutes(miliSeconds);
long seconds = TimeUnit.MILLISECONDS.toSeconds(miliSeconds);
if (minutes == 0) {
holder.seekBarHint.setText("0:" + seconds + "/" + calculateDuration(mediaPlayer.getDuration()));
} else {
if (seconds >= 60) {
long sec = seconds - (minutes * 60);
holder.seekBarHint.setText(minutes + ":" + sec + "/" + calculateDuration(mediaPlayer.getDuration()));
}
}
} else {
//Displaying total time if audio not playing
int totalTime = mediaPlayer.getDuration();
long minutes = TimeUnit.MILLISECONDS.toMinutes(totalTime);
long seconds = TimeUnit.MILLISECONDS.toSeconds(totalTime);
if (minutes == 0) {
holder.seekBarHint.setText("0:" + seconds);
} else {
if (seconds >= 60) {
long sec = seconds - (minutes * 60);
holder.seekBarHint.setText(minutes + ":" + sec);
}
}
}
}
};
run.run();
} else {
mediaPlayer.pause();
holder.pauseRec.setVisibility(View.INVISIBLE);
holder.playsong.setVisibility(View.VISIBLE);
holder.playsong.setText(" Play ");
}
}
});
holder.pauseRec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.pause();
holder.playsong.setVisibility(View.VISIBLE);
holder.pauseRec.setVisibility(View.GONE);
}
});
}
private void postingData(final int position, final MyViewHolder holder) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
final String value = preferences.getString("user_id", "0");
String currentTime = preferences.getString("currentDate", "1");
SharedPreferences sp = context.getSharedPreferences("MyPrefs", MODE_PRIVATE);
String name = sp.getString("prospectNumber", "0");
System.out.println("pressssss"+name);
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", songNames.get(position),
RequestBody.create(MediaType.parse("application/octet-stream"),
new File(CallRecoding.songs.get(position))))
.addFormDataPart("user_id", value)
.addFormDataPart("file_name", "Call Recording")
.addFormDataPart("creation_datetime", currentTime)
.addFormDataPart("prospect_number","1234567891")
.addFormDataPart("call_type","incoming")
.build();
Request request = new Request.Builder()
.url("http://159.65.145.32/api/file/upload/")
.post(requestBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
} else {
String json = response.body().string();
System.out.println("FileUploading >>> " + json);
}
}
});
}
@Override
public int getItemCount() {
return songNames.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView, seekBarHint;
TextView playsong, pauseRec;
public static SeekBar seekBar;
CardView playerCard, cardView1;
ImageView recdonpost,notdone;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.card_item);
playsong = itemView.findViewById(R.id.recplay);
seekBarHint = itemView.findViewById(R.id.songDur);
seekBar = itemView.findViewById(R.id.seekbar);
playerCard = itemView.findViewById(R.id.playerCard);
cardView1 = itemView.findViewById(R.id.layout1);
pauseRec = itemView.findViewById(R.id.recPause);
}
}
private String calculateDuration(int duration) {
String finalDuration = "";
long minutes = TimeUnit.MILLISECONDS.toMinutes(duration);
long seconds = TimeUnit.MILLISECONDS.toSeconds(duration);
if (minutes == 0) {
finalDuration = "0:" + seconds;
} else {
if (seconds >= 60) {
long sec = seconds - (minutes * 60);
finalDuration = minutes + ":" + sec;
}
}
return finalDuration;
}
}
这是我的Recycler视图适配器代码
我想在数据添加到回收站视图时自动发布数据
答案 0 :(得分:0)
在适配器中更新歌曲名称时,只需匹配差异并发布列表中新项目的数据
答案 1 :(得分:0)
您需要在适配器中制作这样的方法。
void addItem(String songName){
datas.add(songName); // Add new Item into the data list in your Adapter.
notifyDataSetChanged();
}
然后在这样的活动中调用它:
adapter.addItem(name);
然后,它将自动更改。
答案 2 :(得分:0)
您需要分离关注点。
notifyDatasetChanged()
。理想情况下,您的应用应在此进行网络通话(或在将商品添加到列表之前)。bind
方法是不可靠的,因为当用户滚动列表时会多次调用它。