我收到了来自rest api的json数据。此数据包含id,title,body,图像数组“appImages”和teaserImage。现在我在控制器类中反序列化json。我有两个适配器。第一个adpter用于recyclerview。此回收器视图显示标题和teasetImage。这部分工作。如果用户点击项目,它会重定向到详细活动,在那里他可以看到teaserImage作为封面图像,身体作为描述。现在这个布局我用这种方式进行了组织,在fisrt上,ImageView用于封面图像,TextView用于描述。在下面的描述中,我创建了一个recyclerView来显示图像数组。 CoverImage和细节cativity的描述运作良好。对于recyclerview我创建了另一个Adpater,这个适配器用于显示基于新闻标题的所有图像。但我很高兴在recyclerview中展示这些图像。我已经在代码中解释了deatil。
我的控制器类
public class NewsController {
private static final String TAG = NewsController.class.getSimpleName();
private UserCallbackListener mListener;
private NewsRestApiManager mApiManager;
private AppImage appImages;
public NewsController(UserCallbackListener listener) {
mListener = listener;
mApiManager = new NewsRestApiManager();
}
public void startFetching(){
mApiManager.getNewsApi().getNews(new Callback<String>() {
@Override
public void success(String s, Response response) {
Log.d(TAG, "JSON :: " + s);
try {
JSONArray array = new JSONArray(s);
for(int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
NewsModel news = new NewsModel();
news.setTitle( jsonObject.optString( "title") );
news.setBody( jsonObject.optString( "body" ) );
ArrayList<AppImage> list = new ArrayList();
JSONArray imageArray =jsonObject.getJSONArray("appImages");
if (imageArray.length() > 1) {
for(int j=0; j<imageArray.length();j++){
appImages = new AppImage();
appImages.setSrc(new JSONArray( s ).getJSONObject( i ).getJSONArray( "appImages" ).getJSONObject( j ).getString( "src" ));
list.add(appImages);
}
}
news.setAppImages( list );
TeaserImageSmall coverImage=new TeaserImageSmall();
coverImage.setSrc( new JSONArray( s ).getJSONObject( i ).getJSONObject( "teaserImageSmall" ).getString( "src" ));
news.setTeaserImageSmall(coverImage);
mListener.onFetchProgressNews(news);
}
} catch (JSONException e) {
mListener.onFetchFailed();
}
mListener.onFetchComplete();
}
@Override
public void failure(RetrofitError error) {
Log.d(TAG, "Error :: " + error.getMessage());
mListener.onFetchComplete();
}
});
}
public interface UserCallbackListener{
void onFetchStart();
void onFetchProgressNews(NewsModel news);
void onFetchProgressNews(List<NewsModel> userList);
void onFetchComplete();
void onFetchFailed();
}
我的News Recyclerview的适配器类。现在这很完美。
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.NewsHolder>
........
public void addNews(NewsModel news) {
Log.d(TAG,news.getTeaserImageSmall().getSrc());
mNews.add(news);
notifyDataSetChanged();
}
@Override
public void onBindViewHolder(NewsHolder holder, int position) {
final NewsModel currentNews = mNews.get(position);
Picasso.with(holder.itemView.getContext());
Picasso.with(holder.itemView.getContext()).load(currentNews.getTeaserImageSmall().getSrc()).into( holder.newsImage );
holder.newsHeadline.setText(currentNews.getTitle());
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(context,DetailNews.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("src",currentNews.getTeaserImageSmall().getSrc());
i.putExtra("title",currentNews.getTitle());
i.putExtra("body",currentNews.getBody());
context.startActivity(i);
}
});
现在另一个适用于图像数组的适配器,它将详细显示新闻页面的活动。
已编辑的适配器类
public class NewsImageAdapter extends RecyclerView.Adapter<NewsImageAdapter.ImageHolder> {
public static String TAG = NewsImageAdapter.class.getSimpleName();
private Context context;
private List<AppImage> appImageList;
DetailNews detailNews = new DetailNews ();
public NewsImageAdapter(List<AppImage> imageObject,Context context) {
this.context = context;
this.appImageList = imageObject;
}
public void addImage(AppImage appImage) {
Log.d(TAG,appImage.getSrc());
appImageList.add(appImage);
notifyDataSetChanged();
}
@Override
public ImageHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.newsdetail_image_row,parent,false);
return new ImageHolder(view);
}
@Override
public void onBindViewHolder(ImageHolder holder, int position) {
final AppImage currentImage=appImageList.get(position);
//getting error for current news,
detailNews .navigate(context, appImageList, currentNews.getTeaserImageSmall().getSrc(), currentNews.getTitle(),currentNews.getBody());
Picasso.with(holder.itemView.getContext()).load(currentImage.getSrc()).into( holder.images);
}
@Override
public int getItemCount() {
return imageObject.size();
}
public class ImageHolder extends RecyclerView.ViewHolder {
public ImageView images;
public ImageHolder(View itemView) {
super(itemView);
images= itemView.findViewById(R.id.news_image);
}
}
}
这个时刻我正在展示封面图像和描述的新闻的详细活动。但我还想在说明下面显示图像列表。我想如何实现它。
编辑的详情活动
public class DetailNews extends AppCompatActivity{
public class DetailNews extends AppCompatActivity{
private RecyclerView recyclerView;
private NewsImageAdapter adapter;
private List<AppImage> imageList= new ArrayList<>();
private NewsController mController;
private CardView cardview;
private ImageView _coverImage;
private TextView _newsHeading;
private TextView _description;
private TextView _newsDate;
private static List<AppImage> appImageList,mAppImageList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_detail);
//newsObject=getAllImageList();
// setting up views and stuff
setUpUIViews();
Intent intent = getIntent();
//RECEIVE DATA
Log.e("_coverImage",""+_coverImage);
String coverImage = intent.getStringExtra ("src");
String heading=intent.getExtras().getString("title");
//String newsDate=intent.getExtras().getString("date");
String description=intent.getExtras().getString("body");
//BIND DATA
Picasso.with(this).load(coverImage ).into(_coverImage);
_newsHeading.setText(heading);
// _newsDate.setText(newsDate);
_description.setText(description);
Linkify.addLinks( _description,Linkify.WEB_URLS );
}
private void setUpUIViews() {
_coverImage=(ImageView)findViewById(R.id.news_cover);
_newsHeading=(TextView)findViewById(R.id.heading);
_description=(TextView)findViewById(R.id.news_description);
_newsDate=(TextView)findViewById(R.id.date);
cardview=(CardView) findViewById(R.id.cardView);
recyclerView = (RecyclerView)findViewById(R.id.image_list);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(DetailNews.this);
recyclerView.setLayoutManager(layoutManager);
adapter = new NewsImageAdapter(imageList,getApplicationContext() );
recyclerView.setAdapter(adapter);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
public void navigate(Context activity, List<AppImage> appImageList, String src, String title, String body) {
mAppImageList = appImageList;
Intent intent = new Intent(activity, DetailNews .class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("src",src);
intent .putExtra("title",title);
intent .putExtra("body",body);
activity.startActivity(intent);
try {
if (activity instanceof NewasPage) { //Error for news
((NewsPage) activity).finish();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
我的Mdel班是
public class NewsModel {
@Expose
private String _id;
@Expose
private String body;
@Expose
private String title;
@Expose
private List<AppImage> appImages;
public List<AppImage> getAppImages() {
return appImages;
}
public void setAppImages(List<AppImage> appImages) {
this.appImages = appImages;
}
AppImage Model Class
public class AppImage {
@Expose
private String _id;
@Expose
private String alt;
@Expose
private String src;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getAlt() {
return alt;
}
public void setAlt(String alt) {
this.alt = alt;
}
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
}
答案 0 :(得分:0)
在DetailNews Activity中,添加方法
Public Sub TestVLookup()
Dim wb As Workbook
Dim rng As Range
Set wb = Application.ThisWorkbook
Set sht = Application.Sheets("Tabelle1")
budgetItemValue = Application.VLookup(Cells.Range("D1"), sht.Range("A1:B5"), 2, False)
Debug.Print "The Result is " & budgetItemValue
End Sub
在适配器中执行
private static List<AppImage> appImageList mAppImageList;
public void navigate(Context activity, List<AppImage> appImageList,String src,String title,String body) {
mAppImageList = appImageList;
Intent intent = new Intent(activity, DetailNews .class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("src",src);
intent .putExtra("title",title);
intent .putExtra("body",body);
activity.startActivity(intent);
try {
if (activity instanceof FirstActivity) {
((FirstActivity) activity).finish();
}
} catch (Exception e) {
e.printStackTrace();
}
}