gpt4 book ai didi

java - 当回收者 View 中添加任何新项目时如何自动发布数据

转载 作者:行者123 更新时间:2023-12-02 04:14:25 27 4
gpt4 key购买 nike

当回收器中添加的任何新项目查看它时自动发布数据时,我尝试发布数据

我已经尝试过单击项目,它工作正常,但我想在回收器 View 中添加任何项目时自动发布

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 View 适配器代码

我想在回收器 View 中添加数据时自动发布数据

最佳答案

当您更新适配器中的songNames时,只需匹配差异并发布列表中新项目的数据

关于java - 当回收者 View 中添加任何新项目时如何自动发布数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56669730/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com