【问题标题】:parse JSON data fetch from php mysql to android using android volley [duplicate]使用android volley解析从php mysql到android的JSON数据获取[重复]
【发布时间】:2016-09-06 07:40:05
【问题描述】:

我有这个 android 凌空代码。我这里没有任何问题。

private void sendRequest() {
    StringRequest stringRequest = new StringRequest(JSON_url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("HomeFragment", response);
            showJSON(response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_LONG).show();
        }
    });

    RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
    requestQueue.add(stringRequest);

}

private void showJSON(String json) {
    MySingleton listJson = new MySingleton(json);
    listJson.mySingleton();
    List<myHome> homeData = new ArrayList<>();
    homeData.add(new myHome(MySingleton.vid_title, MySingleton.vid_desc, MySingleton.vid_duration, MySingleton.vid_thumbnail, MySingleton.embed_url, MySingleton.ids) );
    mAdapter = new HomeAdapter(getContext(), homeData);
    mRecyclerView.setAdapter(mAdapter);

这是我的适配器。

private String[] vid_title;
private String[] vid_desc;
private String[] vid_duration;
private String[] vid_thumbnail;
private String[] vid_embedUrl;
private int[] type;

private LayoutInflater inflater = null;
private List<myHome> multipleRowModelList;
private Context context;


public HomeAdapter(Context context, String[] title, String[] desc, String[] duration, String[] thumbnail, String[] embedUrl, int[] type) {
    this.context = context;
    this.vid_title = title;
    this.vid_desc = desc;
    this.vid_duration = duration;
    this.vid_thumbnail = thumbnail;
    this.vid_embedUrl = embedUrl;
    this.type = type;
    inflater = LayoutInflater.from(context);
}

我的问题在这里。

@Override
public int getItemCount() {
    return (multipleRowModelList != null && multipleRowModelList.size() > 0 ) ? multipleRowModelList.size() : 0;
}

@Override
public int getItemViewType(int position) {

    myHome multipleRowModel = multipleRowModelList.get(position);

    if (multipleRowModel != null)
        return type[position];

    return super.getItemViewType(position);
}

@Override
public void onBindViewHolder(final FeaturedViewHolder holder, final int position) {

holder.featured_title.setText(multipleRowModelList.get(position).getTitle());
holder.featured_user.setText(multipleRowModelList.get(position).getUser());

错误是说,无法解析方法'setText(java.lang.String[])'

我的模型有这样的数据。 String[] 和 int[]。

有什么办法可以做到这一点?

这里是处理程序

public FeaturedViewHolder(View itemView, int type) {
    super(itemView);
    if (type == AppConstant.TYPE_FEATURED) {
        featured_title = (TextView)itemView.findViewById(R.id.title);
        featured_user = (TextView)itemView.findViewById(R.id.recvid_auth);
        mRelativeLayout = (RelativeLayout)itemView.findViewById(R.id.relative_recycler);

    }

    else if(type == AppConstant.TYPE_OTHER) {
        featured_title = (TextView)itemView.findViewById(R.id.recvid_title);
        featured_user = (TextView)itemView.findViewById(R.id.description);
        mRelativeLayout = (RelativeLayout)itemView.findViewById(R.id.relative_recycler_other);

    }

}

这是模型

public class myHome  {

String[] vid_title;
String[] vid_desc;
String[] vid_duration;
String[] vid_thumbnail;
String[] vid_embedUrl;
int[] type;


public myHome(String[] title, String[] desc, String[] duration, String[] thumbnail, String[] embedUrl, int[] type) {
    this.vid_title = title;
    this.vid_desc = desc;
    this.vid_duration = duration;
    this.vid_thumbnail = thumbnail;
    this.vid_embedUrl = embedUrl;
    this.type = type;
}

public String[] getTitle() {
    return vid_title;
}

public String[] getUser() {
    return vid_desc;
}

public String[] getViews() {
    return vid_duration;
}

public String[] getUrl() {
    return vid_thumbnail;
}

public String[] getDuration() {
    return vid_embedUrl;
}

public int[] getType() {
    return type;
}

我已经编辑了我的问题

【问题讨论】:

  • 您的具体要求是什么?你想打印字符串数组吗?
  • FeaturedViewHolder 和 myHome 类发布此代码。

标签: java php android mysql json


【解决方案1】:

你的代码应该是

public class myHome {

String vid_title;
String vid_desc;
String vid_duration;
String vid_thumbnail;
String vid_embedUrl;
int type;

public String getVid_title() {
    return vid_title;
}

public void setVid_title(String vid_title) {
    this.vid_title = vid_title;
}

public String getVid_desc() {
    return vid_desc;
}

public void setVid_desc(String vid_desc) {
    this.vid_desc = vid_desc;
}

public String getVid_duration() {
    return vid_duration;
}

public void setVid_duration(String vid_duration) {
    this.vid_duration = vid_duration;
}

public String getVid_thumbnail() {
    return vid_thumbnail;
}

public void setVid_thumbnail(String vid_thumbnail) {
    this.vid_thumbnail = vid_thumbnail;
}

public String getVid_embedUrl() {
    return vid_embedUrl;
}

public void setVid_embedUrl(String vid_embedUrl) {
    this.vid_embedUrl = vid_embedUrl;
}

public int getType() {
    return type;
}

public void setType(int type) {
    this.type = type;
}



}

【讨论】:

  • 这应该是评论
  • 这不是投反对票的理由
  • 是的,它是文本视图
  • 发布您的 FeaturedViewHolder 代码
  • 我已经编辑并发布了模型和处理程序
【解决方案2】:

替换:

@Override
public void onBindViewHolder(final FeaturedViewHolder holder, final int position) {

holder.featured_title.setText(multipleRowModelList.get(position).getTitle());
holder.featured_user.setText(multipleRowModelList.get(position).getUser());
}

带:

@Override
public void onBindViewHolder(final FeaturedViewHolder holder, final int position) {

holder.featured_title.setText(multipleRowModelList.get(position).getTitle().toString());
holder.featured_user.setText(multipleRowModelList.get(position).getUser().toString());

【讨论】:

  • 先生,这里怎么样? myHome multipleRowModel = multipleRowModelList.get(position); if (multipleRowModel != null) return multipleRowModel.getType().toString(); return 需要是 int
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-04
  • 1970-01-01
  • 2012-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多