Java 如何在不单击的情况下从recyclerview适配器中删除所需项目?或者从列表中选择
我有天气预报。我已经为列表项设置了JSON对象。一切正常。单击recycleradapter时,我会从中删除这些项Java 如何在不单击的情况下从recyclerview适配器中删除所需项目?或者从列表中选择,java,android,json,arraylist,Java,Android,Json,Arraylist,我有天气预报。我已经为列表项设置了JSON对象。一切正常。单击recycleradapter时,我会从中删除这些项 public class ForecastAdapters extends RecyclerView.Adapter<ForecastAdapters.ForecastHolder> { private Context mContext; private List<OpenWeatherMapDaysTo> openWeatherMap
public class ForecastAdapters extends RecyclerView.Adapter<ForecastAdapters.ForecastHolder> {
private Context mContext;
private List<OpenWeatherMapDaysTo> openWeatherMapDaysTos = new ArrayList<>();
private OpenWeatherMapDaysTo mOpenWeatherMapDaysTo = new OpenWeatherMapDaysTo();
private Toast mToast;
private ForecastAdapters mForecastAdapters;
public ForecastAdapters(Context context, List<OpenWeatherMapDaysTo> op) {
mContext = context;
openWeatherMapDaysTos = op;
}
public class ForecastHolder extends RecyclerView.ViewHolder {
private TextView mTextName, mMornigText, mNightText,mClock;
private ImageView mImage;
public ForecastHolder(View view) {
super(view);
mTextName = (TextView) view.findViewById(R.id.fr_days_txt);
mMornigText = (TextView) view.findViewById(R.id.fr_morning);
mNightText = (TextView) view.findViewById(R.id.fr_night);
mClock =(TextView)view.findViewById(R.id.fr_clock);
mImage = (ImageView) view.findViewById(R.id.fr_imageView);
}
public void bindData(OpenWeatherMapDaysTo opData) {
mTextName.setText(VolleyURL.getJustAllDate(opData.getList().get(getAdapterPosition()).getDt_txt()));
mMornigText.setText(String.format("%d°C", Math.round(opData.getList().get(getAdapterPosition()).getMain().getTemp_max() - 273.15)));
mNightText.setText(String.format("%d°C", Math.round(opData.getList().get(getAdapterPosition()).getMain().getTemp_min() - 273.15)));
mClock.setText(String.format(VolleyURL.unixTimeStampToDateTime(opData.getList().get(getAdapterPosition()).getDt())));
Picasso.with(mContext).load(VolleyURL.getImage(opData.getList().get(getAdapterPosition()).getWeather().get(0).getIcon())).into(mImage);
}
}
@Override
public ForecastAdapters.ForecastHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_fragment, parent, false);
return new ForecastHolder(view);
}
@Override
public void onBindViewHolder(final ForecastAdapters.ForecastHolder holder, final int position) {
OpenWeatherMapDaysTo op = openWeatherMapDaysTos.get(position);
holder.bindData(op);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(mContext, holder.getAdapterPosition()+ " Clicked", Toast.LENGTH_SHORT);
mToast.show();
/* removeAt(holder.getAdapterPosition());*/
}
});
}
@Override
public int getItemCount() {
return openWeatherMapDaysTos.size();
}
public void removeAt(int position) {
openWeatherMapDaysTos.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, openWeatherMapDaysTos.size());
}
}
当适配器不创建时,我想删除所需的JSON对象。或者适配器将创建。然后,我将删除所需的项目,但不可单击。适配器将被续订并显示。你怎么能这么做?或
有什么想法吗
例如,json包括周一、周二、周三、周四、周五等。它们也是重复的。因为5天预报每3小时包含一次天气数据。
星期一我没法搬走三个
最后,这是我在主要活动中使用的方法,我使用了截击和gson
更新-1方法:(我更改了由两个For循环分隔的方法。)
RecyclerAdapter显示前5个项目,所有其他元素均未显示。但我不想显示前5个元素。我想在前5个元素之后显示
编辑-1:这是我的模型类和对象
public class OpenWeatherMapDaysTo {
private String cod;
private double message;
private int cnt;
private List<Lists2> list;
private City city;
private Main main;
public OpenWeatherMapDaysTo(){
}
public OpenWeatherMapDaysTo(String cod, double message, int cnt, List<Lists2> list, City city, Main main) {
this.cod = cod;
this.message = message;
this.cnt = cnt;
this.list = list;
this.city = city;
this.main = main;
}
public String getCod() {
return cod;
}
public void setCod(String cod) {
this.cod = cod;
}
public double getMessage() {
return message;
}
public void setMessage(double message) {
this.message = message;
}
public int getCnt() {
return cnt;
}
public void setCnt(int cnt) {
this.cnt = cnt;
}
public List<Lists2> getList() {
return list;
}
public void setList(List<Lists2> list) {
this.list = list;
}
public City getCity() {
return city;
}
public void setCity(City city) {
this.city = city;
}
public Main getMain() {
return main;
}
public void setMain(Main main) {
this.main = main;
}
}
公共类OpenWeatherMapDaysTo{
私有字符串cod;
私人双重信息;
私人互联网;
私人名单;
私人城市;
私人主楼;
公共OpenWeatherMapDaysTo(){
}
public OpenWeatherMapDaysTo(字符串cod,双消息,int-cnt,列表列表,城市,主要){
this.cod=cod;
this.message=消息;
this.cnt=cnt;
this.list=列表;
this.city=城市;
this.main=main;
}
公共字符串getCod(){
返回cod;
}
公共无效设置代码(字符串代码){
this.cod=cod;
}
公共双getMessage(){
返回消息;
}
公共无效设置消息(双重消息){
this.message=消息;
}
public int getCnt(){
返回cnt;
}
公共无效设置cnt(内部cnt){
this.cnt=cnt;
}
公共列表getList(){
退货清单;
}
公共无效集合列表(列表){
this.list=列表;
}
公共城市{
回归城市;
}
公共城市(城市){
this.city=城市;
}
公共Main getMain(){
回水总管;
}
公共无效设置主(主){
this.main=main;
}
}
我的列表2类:
public class Lists2 {
private int dt;
private Main main;
private List<Weather> weather;
private Clouds clouds;
private Wind wind;
private Sys sys;
private String dt_txt;
public Lists2(int dt, Main main, List<Weather> weather, Clouds clouds, Wind wind, Sys sys, String dt_txt) {
this.dt = dt;
this.main = main;
this.weather = weather;
this.clouds = clouds;
this.wind = wind;
this.sys = sys;
this.dt_txt = dt_txt;
}
public int getDt() {
return dt;
}
public void setDt(int dt) {
this.dt = dt;
}
public Main getMain() {
return main;
}
public void setMain(Main main) {
this.main = main;
}
public List<Weather> getWeather() {
return weather;
}
public void setWeather(List<Weather> weather) {
this.weather = weather;
}
public Clouds getClouds() {
return clouds;
}
public void setClouds(Clouds clouds) {
this.clouds = clouds;
}
public Wind getWind() {
return wind;
}
public void setWind(Wind wind) {
this.wind = wind;
}
public Sys getSys() {
return sys;
}
public void setSys(Sys sys) {
this.sys = sys;
}
public String getDt_txt() {
return dt_txt;
}
public void setDt_txt(String dt_txt) {
this.dt_txt = dt_txt;
}
}
公共类列表2{
私人int dt;
私人主楼;
私人天气列表;
私密的云朵;
私人风;
私有系统;
私有字符串dt_txt;
公共列表2(int-dt、Main-Main、列表天气、云彩、风、Sys-Sys、字符串dt_-txt){
这1.dt=dt;
this.main=main;
这个天气=天气;
云=云;
风=风;
this.sys=sys;
this.dt_txt=dt_txt;
}
公共整数getDt(){
返回dt;
}
公共无效设置(int dt){
这1.dt=dt;
}
公共Main getMain(){
回水总管;
}
公共无效设置主(主){
this.main=main;
}
公共列表getWeather(){
回归天气;
}
公众天气(列出天气){
这个天气=天气;
}
公共云getClouds(){
回归云;
}
公共云(云){
云=云;
}
公共风{
回风;
}
公共空间风(风){
风=风;
}
公共系统getSys(){
返回系统;
}
公共无效设置系统(Sys Sys){
this.sys=sys;
}
公共字符串getDt_txt(){
返回dt_txt;
}
公共无效设置dt_txt(字符串dt_txt){
this.dt_txt=dt_txt;
}
}
还有一个模型课
最后:我的问题是
编辑1:
据我所知,当您只想显示一个列表2
对象(每个RecyclerView
项)时,您正在将整个预测列表(list
)发送到您的ForecasAdapters
)
您的for循环
正在迭代Lists2
对象,据我所知,这是一天中特定时间的预测。因此,您应该创建一个ArrayList
(ArrayList singleDays=new ArrayList();
)并将日期的每个第一个实例添加到singleDays
)。在for循环的每次迭代中
,检查openWeatherMapDaysTo.getList().get(j)
对象是否已存在于singleDays
中。如果不存在,则将其添加到singleDays
为了使代码更简洁,您应该重写Lists2
类的equals方法。在覆盖的equals
方法中,检查日期是否相同,但不是一天中的时间。您可以通过使用仅包含yyyy-MM-dd
且不包含HH:MM:ss
的SimpleDataFormat
来实现这一点
public class Lists2 {
...
@Override
boolean equals(Object object) {
if (object == null || !(object instanceof Lists2)) return false;
Lists2 toCheck = (Lists2) object;
String thisDateString = getDt_txt();
String toCheckDateString = toCheck.getDt_txt();
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date thisDate = sdf.parse(thisDateString);
Date toCheckDate = sdf.parse(toCheckDateString);
return thisDate.compareTo(toCheckDate) == 0;
}
}
在for循环
完成对数据集的迭代后,您可以创建预测适配器
,并将其分配给回收视图
将循环的getFiveDays()
方法的第二个更改为此
ArrayList<Lists2> singleDays = new ArrayList<>();
for (int j = 0; j < openWeatherMapDaysTo.getList().size(); j++) {
if (!singleDays.contains(openWeatherMapDaysTo.getList().get(j))) {
singleDays.add(openWeatherMapDaysTo.getList().get(j));
}
}
mForecastAdapters = new ForecastAdapters(getApplicationContext(), singleDays);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mForecastAdapters);
在第1行,您正在调用get(j)
,但没有将其分配给任何对象。我假设这是您希望在RecyclerView
中显示的项目
在第2行,您正在将整个openWeatherMapDaysTo
对象(其中包含您不想要的天数列表)添加到getmOpenWeatherMapListto
ArrayList
(我假设它是ArrayList
)中。因此,我认为正在发生的是,即使您排除了前面的那些项目(基于比较检查),它们也被添加到整个openWeatherMapDaysTo
对象中
我想你想做的是
getmOpenWeatherMapListto.add(openWeatherMapDaysTo.getList().get(j));
根据日期添加一天
public class Lists2 {
...
@Override
boolean equals(Object object) {
if (object == null || !(object instanceof Lists2)) return false;
Lists2 toCheck = (Lists2) object;
String thisDateString = getDt_txt();
String toCheckDateString = toCheck.getDt_txt();
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date thisDate = sdf.parse(thisDateString);
Date toCheckDate = sdf.parse(toCheckDateString);
return thisDate.compareTo(toCheckDate) == 0;
}
}
ArrayList<Lists2> singleDays = new ArrayList<>();
for (int j = 0; j < openWeatherMapDaysTo.getList().size(); j++) {
if (!singleDays.contains(openWeatherMapDaysTo.getList().get(j))) {
singleDays.add(openWeatherMapDaysTo.getList().get(j));
}
}
mForecastAdapters = new ForecastAdapters(getApplicationContext(), singleDays);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mForecastAdapters);
Log.d(TAG, "===TRUE====: ");
openWeatherMapDaysTo.getList().get(j); // line 1
getmOpenWeatherMapListto.add(openWeatherMapDaysTo); // line 2
getmOpenWeatherMapListto.add(openWeatherMapDaysTo.getList().get(j));
if ((finalDate.compareTo(VolleyURL.getDateNumber())!=0)) {
Log.d(TAG, "===TRUE====: ");
getmOpenWeatherMapListto.add(openWeatherMapDaysTo.getList().get(j));
Log.d(TAG, "openWeatherDaysTo SIZE ===> " + getmOpenWeatherMapListto.size());
mForecastAdapters = new ForecastAdapters(getApplicationContext(), getmOpenWeatherMapListto);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mForecastAdapters);
mForecastAdapters.notifyDataSetChanged();
// break out of the for loop here otherwise your adapter will be created
// again and set to your recyclerview
break;
}
for (int j = 0; j < openWeatherMapDaysTo.getList().size(); j++) {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_YEAR, 0);
String mNowDay = sdf.format(c.getTime());
Date dtNow = null;
Calendar cT = Calendar.getInstance();
cT.add(Calendar.DAY_OF_YEAR, 1);
String mTomorrow = sdf.format(cT.getTime());
Date dtTomorrow = null;
try {
dtNow = sdf.parse(mNowDay);
dtTomorrow = sdf.parse(mTomorrow);
} catch (ParseException e) {
e.printStackTrace();
}
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String finalDate = dateFormat.format(dtNow);
String finalTomorrow = dateFormat.format(dtTomorrow);
Log.d(TAG, "JSON ----------------------->: " + VolleyURL.unixTimeStampToDateTime(openWeatherMapDaysTo.getList().get(j).getDt()));
Log.d(TAG, "VolleyURL.getDateNumber----------------------->: " + VolleyURL.getDateNumber());
Log.d(TAG, "FINALDATE ---------> : " + finalDate);
Log.d(TAG, "IF IC ---------> : " + VolleyURL.unixTimeStampToDateTime(openWeatherMapDaysTo.getList().get(j).getDt()));
if (VolleyURL.unixTimeStampToDateTime(openWeatherMapDaysTo.getList().get(j).getDt()).equals(finalDate + " 12:00")) {
Log.d(TAG, " \n ===TRUE====: ");
openWeatherMapDaysTo.getList().remove(j);
j--;
} else if (VolleyURL.unixTimeStampToDateTime(openWeatherMapDaysTo.getList().get(j).getDt()).equals(finalDate + " 15:00")) {
Log.d(TAG, " \n ===TRUE====: ");
openWeatherMapDaysTo.getList().remove(j);
j--;
} else if (VolleyURL.unixTimeStampToDateTime(openWeatherMapDaysTo.getList().get(j).getDt()).equals(finalDate + " 18:00")) {
Log.d(TAG, " \n ===TRUE====: ");
openWeatherMapDaysTo.getList().remove(j);
j--;
} else if (VolleyURL.unixTimeStampToDateTime(openWeatherMapDaysTo.getList().get(j).getDt()).equals(finalDate + " 21:00")) {
Log.d(TAG, " \n ===TRUE====: ");
openWeatherMapDaysTo.getList().remove(j);
j--;
} else if (VolleyURL.unixTimeStampToDateTime(openWeatherMapDaysTo.getList().get(j).getDt()).equals(finalTomorrow + " 00:00")) {
Log.d(TAG, " \n ===TRUE====: ");
openWeatherMapDaysTo.getList().remove(j);
j--;
} else if (VolleyURL.unixTimeStampToDateTime(openWeatherMapDaysTo.getList().get(j).getDt()).equals(finalTomorrow + " 03:00")) {
Log.d(TAG, " \n ===TRUE====: ");
openWeatherMapDaysTo.getList().remove(j);
j--;
} else {
Log.d(TAG, " \n ===FALSE====: ");
getmOpenWeatherMapListto.add(openWeatherMapDaysTo);
}