我想从服务器加载数据,为此我使用$this->load->database();
$this->load->dbutil();
// check connection details
if( !$this->dbutil->database_exists('myDatabase'))
echo 'Not connected to a database, or database not exists';
。我想要加载帖子(标题,描述,图片等等)以及我希望从此帖子加载评论的数据。
我可以加载标题,描述,图片等等...并显示它。但我不知道如何在活动中显示帖子评论!
ModelResponse(使用getter和setter):
Retrofit
模型(使用getter和setter):
public class R_CatModelResponse {
@SerializedName("status")
public String Cat_status;
@SerializedName("count")
public int Cat_count;
@SerializedName("pages")
public int Cat_pages;
@SerializedName("category")
public Retrofit_ColoniesModel category;
@SerializedName("posts")
public List<R_CatModel> Cat_posts;
PostComment(使用getter和setter):
public class R_CatModel {
@SerializedName("id")
public Integer id;
@SerializedName("type")
public String type;
@SerializedName("slug")
public String slug;
@SerializedName("url")
public String url;
@SerializedName("status")
public String status;
@SerializedName("title")
public String title;
@SerializedName("title_plain")
public String title_plain;
@SerializedName("content")
public String content;
@SerializedName("excerpt")
public String excerpt;
@SerializedName("date")
public String date;
@SerializedName("modified")
public String modified;
@SerializedName("comment_count")
public int comment_count;
@SerializedName("comment_status")
public String comment_status;
@SerializedName("thumbnail")
public String thumbnail;
@SerializedName("thumbnail_images")
public R_CatThumbnailImages thumbnail_images;
@SerializedName("categories")
public List<R_CatCategory> categories;
@SerializedName("author")
public R_CatAuthor catAuthor;
@SerializedName("comments")
public List<R_PostComment> comments;
适配器代码(用于加载评论):
@SerializedName("id")
public int cmID;
@SerializedName("name")
public String cmName;
@SerializedName("url")
public String cmUrl;
@SerializedName("date")
public String cmDate;
@SerializedName("content")
public String cmContent;
@SerializedName("parent")
public int cmParent;
活动代码:
public class CommentAdapter2 extends RecyclerView.Adapter<CommentAdapter2.ViewHolder> {
private List<R_PostComment> mDataSet;
private Context context;
public CommentAdapter2(Context context, List<R_PostComment> mDataSet) {
this.mDataSet = mDataSet;
this.context = context;
}
@Override
public CommentAdapter2.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.comment_layout, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(CommentAdapter2.ViewHolder viewHolder, int position) {
viewHolder.comment_name.setText( ??? );
viewHolder.comment_date.setText( ??? );
viewHolder.comment_content.setText(Html.fromHtml( ??? ));
}
@Override
public int getItemCount() {
return mDataSet.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView comment_name, comment_content, comment_date;
public ViewHolder(View view) {
super(view);
comment_name = (TextView) itemView.findViewById(R.id.comment_userName_text);
comment_content = (TextView) itemView.findViewById(R.id.comment_comment_text);
comment_date = (TextView) itemView.findViewById(R.id.comment_date_text);
}
}
}
ApiInterface:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.post_show_page);
bindActivity();
// Initialize
favDB = new FavHelper(context);
//Give Data
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
postID = bundle.getInt("postID");
title = bundle.getString("title");
image = bundle.getString("image");
content = bundle.getString("content");
dateTime = bundle.getString("dateTime");
author = bundle.getString("author");
category = bundle.getString("category");
categoryID = bundle.getString("categoryID");
}
mAppBarLayout.addOnOffsetChangedListener(this);
// Setup comment RecyclerView
comment_Recyclerview.setLayoutManager(mLayoutManager);
comment_Recyclerview.setHasFixedSize(true);
startAlphaAnimation(mTitle, 0, View.INVISIBLE);
// Set Data into views
if (title != null) {
postShow_title.setText(title);
postShow_title2.setText(title);
title_sliding.setText(getResources().getString(R.string.comment_title) + " " + title);
}
loadPostProgressDialog.createAndShow(this);
if (image != null) {
Glide.with(this)
.load(image)
.placeholder(R.drawable.post_image)
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target,
boolean isFromMemoryCache, boolean isFirstResource) {
loadPostProgressDialog.dissmis();
return false;
}
})
.into(post_cover);
}
if (content != null) {
post_content_web.getSettings().setJavaScriptEnabled(true);
WebSettings settings = post_content_web.getSettings();
settings.setDefaultTextEncodingName("utf-8");
post_content_web.loadData(content, "text/html; charset=utf-8", "utf-8");
}
if (dateTime != null) {
postShow_dateTime.setText(dateTime);
}
if (author != null) {
postShow_author.setText(author);
}
if (category != null) {
postShow_category.setText(category);
}
post_cover.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BlurBehind.getInstance().execute(PostShow_page.this, new OnBlurCompleteListener() {
@Override
public void onBlurComplete() {
startActivity(new Intent(PostShow_page.this, DialogImage_page.class)
.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
.putExtra("imageCover", image));
}
});
}
});
postShow_category.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (categoryID != null) {
startActivity(new Intent(PostShow_page.this, Category_page.class)
.putExtra("categoryID", categoryID)
.putExtra("categoryTitle", category));
}
}
});
private void bindData() {
// Setup Connect
Retrofit_ApiInterface apiInterface = Retrofit_ApiClient.getClient().create(Retrofit_ApiInterface.class);
call.enqueue( ??? )
@Override
public void onFailure(Call<R_CatModelResponse> call, Throwable t) {
Toast.makeText(PostShow_page.this, "Failed", Toast.LENGTH_SHORT).show();
}
});
}
JSON链接: [链接] [1],@GET("?json=get_category_posts")
Call<R_CatModelResponse> getCatResponse(@Query("id") Integer id);
(id= ?
链接结束)我发送 categoryID 以查看此类别讯息!
如何在此帖子中显示帖子的评论? 谢谢所有&lt; 3
答案 0 :(得分:0)
在你的活动课上。当你这样做时:
call.enqueue()
让我们假设你正确地恢复了身体,因为你说你可以加载标题,描述和图像。
所以你可能有类似的东西:
R_CatModelResponse catModelResponse;
call.enqueue(new CallBack<R_CatModelResponse>()
{
@Override
public void onResponse(Call<R_CatModelResponse> call, Response<R_CatModelResponse> response)
{
catModelResponse = response.body();
}
}
...
List<R_CatModel> catPosts = catModelResponse.getCat_posts();
//For whatever index of the posts you're interested in. Take 0 for example:
List<R_PostComment> comments = catPosts.get(0).getCatPosts();
您可以使用“R_CatModel”的getter获取List,然后使用R_CatModel的getter来检索R_PostComment的List。当您获得List注释对象时,将其传递到RecycerView的适配器(CommentAdapter2)。所以你的活动内容如下:
CommentAdapter2 adapter = new CommentAdapter2(this, comments);
comment_Recyclerview.set(adapter);
请注意,由于Retrofit的入队呼叫是异步的,因此您应该在呼叫成功后进行RecyclerView的设置。
答案 1 :(得分:0)
试试这段代码:
private void bindData() {
// Setup Connect
Retrofit_ApiInterface apiInterface = Retrofit_ApiClient.getClient().create(Retrofit_ApiInterface.class);
Call<R_PostModelResponse> call = apiInterface.getPostResponse(postID);
final ProgressDialog progressDialog = new ProgressDialog(context);
progressDialog.show();
Log.d("PostID", "Post : " + postID);
call.enqueue(new Callback<R_PostModelResponse>() {
@Override
public void onResponse(Call<R_PostModelResponse> call, Response<R_PostModelResponse> response) {
if (response.body() != null) {
models.addAll(response.body().getPost().getComments());
Log.e("testComment", "" + response.body().getPost().getComment_count());
mAdaper.notifyDataSetChanged();
comment_Recyclerview.setAdapter(mAdaper);
progressDialog.dismiss();
} else {
progressDialog.dismiss();
}
}
@Override
public void onFailure(Call<R_PostModelResponse> call, Throwable t) {
Toast.makeText(PostShow_page.this, "Failed", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
});
}