我从业务片段中的API获取新闻,我将这些数据传输到BusinessDetail。但是,当我点击特定新闻时,它会向我显示相同的新闻。我不能改变数据;即使我再次安装了apk,它也会向我展示同样的旧消息。
某些代码已被删除。
主要活动:
public void send(StringList stringList) {
bundle = new Bundle();
bundle.putParcelable("businessnews", stringList);
BusinessDetail businessDetail = new BusinessDetail();
businessDetail.setArguments(bundle);
frag = getSupportFragmentManager();
frag.beginTransaction().replace(R.id.content_frame, businessDetail).commit();
}
商务:
public class Business extends Fragment {
public List<StringList> businessNews = new ArrayList<>();
StringList stringList;
Transfer transfer;
private RecyclerView recyclerView;
public Business() {
}
public interface Transfer {
public void send(StringList stringList);
}
public class BusinessHolder extends RecyclerView.ViewHolder {
public TextView headlineTextview;
public TextView authorTextview;
public TextView timeTextview;
public BusinessHolder(View itemView) {
super(itemView);
headlineTextview = (TextView) itemView.findViewById(R.id.id_headline);
authorTextview = (TextView) itemView.findViewById(R.id.id_author);
timeTextview = (TextView) itemView.findViewById(R.id.id_time);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
transfer.send(stringList);
Log.d("ashu","business onclick stringlist value: "+stringList);
}
});}}}
BusinessDetail:
public class BusinessDetail extends Fragment {
private TextView headlineSecond;
public TextView authorSecond;
private TextView detailsSecond;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_business_detail, container, false);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
headlineSecond = (TextView) view.findViewById(R.id.id_headline_second);
authorSecond = (TextView) view.findViewById(R.id.id_author_second);
detailsSecond = (TextView) view.findViewById(R.id.id_details_second);
Bundle bundle=getArguments();
if (bundle==null || !bundle.containsKey("businessnews")){
throw new IllegalArgumentException("stringlist should not be null");
}
StringList stringList=bundle.getParcelable("businessnews");
Log.d("ashu","stringlist value business detail"+stringList);
authorSecond.setText(stringList.authorName);
headlineSecond.setText(stringList.headline);
detailsSecond.setText(stringList.newsDetail);
}}
的StringList:
public class StringList implements Parcelable{
public String authorName;
public String headline;
public String publishedTime;
public String newsDetail;
protected StringList(Parcel in) {
authorName = in.readString();
headline = in.readString();
publishedTime = in.readString();
newsDetail = in.readString();
}
public static final Parcelable.Creator<StringList> CREATOR = new Parcelable.Creator<StringList>() {
@Override
public StringList createFromParcel(Parcel in) {
return new StringList(in);
}
@Override
public StringList[] newArray(int size) {
return new StringList[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(authorName);
parcel.writeString(headline);
parcel.writeString(publishedTime);
parcel.writeString(newsDetail);
}
}
适配器代码:
public class BusinessAdapter extends RecyclerView.Adapter<BusinessHolder> {
int prevposition = 0;
private List<StringList> c;
public BusinessAdapter(Business context, List<StringList> result) {
c = context.businessNews;
}
@Override
public BusinessHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_news, parent, false);
return new BusinessHolder(view);
}
@Override
public void onBindViewHolder(BusinessHolder holder, int position) {
StringList m = c.get(position);
holder.bindListName(m, position);
if (position > prevposition) {
AnimationClass.animate(holder, true);
} else {
AnimationClass.animate(holder, false);
}
}
@Override
public int getItemCount() {
return c.size();
}
}
答案 0 :(得分:2)
为什么你发送&#34; stringList&#34;每次都是场?
public class Business extends Fragment {
public List<StringList> businessNews = new ArrayList<>();
StringList stringList;
Transfer transfer;
private RecyclerView recyclerView;
public Business() {
}
public interface Transfer {
public void send(StringList stringList);
}
public class BusinessHolder extends RecyclerView.ViewHolder {
public TextView headlineTextview;
public TextView authorTextview;
public TextView timeTextview;
public BusinessHolder(View itemView) {
super(itemView);
headlineTextview = (TextView) itemView.findViewById(R.id.id_headline);
authorTextview = (TextView) itemView.findViewById(R.id.id_author);
timeTextview = (TextView) itemView.findViewById(R.id.id_time);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StringList v = businessNews.get(getAdapterPosition());
transfer.send(v);
Log.d("ashu","business onclick stringlist value: "+stringList);
}
});}}}
答案 1 :(得分:1)
主要活动:
StringList mStringList;
public void send(StringList stringList) {
setStringList(stringList);
BusinessDetail businessDetail = new BusinessDetail();
frag = getSupportFragmentManager();
frag.beginTransaction().replace(R.id.content_frame, businessDetail).commit();
}
void setStringList(StringList stringList){
mStringList = stringList;
}
public StringList getStringList(){
return mStringList;
}
BusinessDetail:
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
headlineSecond = (TextView) view.findViewById(R.id.id_headline_second);
authorSecond = (TextView) view.findViewById(R.id.id_author_second);
detailsSecond = (TextView) view.findViewById(R.id.id_details_second);
StringList stringList = ((MainActivity)getActivity()).getStringList();
Log.d("ashu","stringlist value business detail"+stringList);
authorSecond.setText(stringList.authorName);
headlineSecond.setText(stringList.headline);
detailsSecond.setText(stringList.newsDetail);
}
-------------------------------------------或--- -------------------
MainActivity:
public void send(StringList stringList) {
bundle = new Bundle();
bundle.putParcelable("businessnews", stringList);
BusinessDetail businessDetail = new BusinessDetail(bundle);
frag = getSupportFragmentManager();
frag.beginTransaction().replace(R.id.content_frame, businessDetail).commit();
}
BusinessDetail:
public class BusinessDetail extends Fragment {
private TextView headlineSecond;
public TextView authorSecond;
private TextView detailsSecond;
public BusinessDetail(Bundle b){
BusinessDetail bd = new BusinessDetail();
bd .setArguments(b)
return bd;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_business_detail, container, false);
return view;
}
.
.
.
答案 2 :(得分:0)
从我到目前为止的数据来看,您似乎正在引用旧的StringList
对象,这可能永远不会得到更新。最重要的是,您还持有对旧onClickListener
的引用,因为您只在创建BusindesHolder
类时设置了一次。我建议如下:
public class Business extends Fragment {
public List<StringList> businessNews = new ArrayList<>();
//StringList stringList; //You may be holding a reference to the wrong object. Don't use this here.
Transfer transfer;
private RecyclerView recyclerView;
public Business() {}
public interface Transfer {
public void send(StringList stringList);
}
public class BusinessHolder extends RecyclerView.ViewHolder {
public TextView headlineTextview;
public TextView authorTextview;
public TextView timeTextview;
public BusinessHolder(View itemView) {
super(itemView);
headlineTextview = (TextView) itemView.findViewById(R.id.id_headline);
authorTextview = (TextView) itemView.findViewById(R.id.id_author);
timeTextview = (TextView) itemView.findViewById(R.id.id_time);
}
public void bindListName(final StringList m, int position) {
//YOUR CODE HERE....
//You were passing the onClickListener only once, and it could have been holding a reference to the wrong StringList object. Move that code here instead.
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
transfer.send(m);
Log.d("ashu","business onclick stringlist value: " + m);
}
});
}
}
}
将itemView.setOnClickListener()
移至bindListName
方法,然后使用此处传递的StringList
对象。这样,只要RecyclerView
中的单元格通过onBindViewHolder
方法更新,就会使用最新信息进行更新。