Java 自定义适配器不支持';不显示数据
我试图创建一个自定义适配器,并向它传递一个JSONArray,但它没有显示任何内容。 适配器如下所示:Java 自定义适配器不支持';不显示数据,java,android,adapter,Java,Android,Adapter,我试图创建一个自定义适配器,并向它传递一个JSONArray,但它没有显示任何内容。 适配器如下所示: public class MasonryAdapter extends RecyclerView.Adapter<MasonryAdapter.MasonryView> { int[] imgList = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launc
public class MasonryAdapter extends RecyclerView.Adapter<MasonryAdapter.MasonryView> {
int[] imgList = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher};
JSONArray arrayofresults;
private Context context;
public MasonryAdapter(Context context, String results) throws JSONException {
this.context = context;
arrayofresults = new JSONArray(results);
Toast.makeText(context, "length" + arrayofresults.length(), Toast.LENGTH_LONG).show();
}
@Override
public MasonryView onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, parent, false);
MasonryView masonryView = new MasonryView(layoutView);
return masonryView;
}
@Override
public void onBindViewHolder(MasonryView holder, int position) {
try {
JSONObject tmpObj = arrayofresults.getJSONObject(position);
holder.imageView.setImageResource(imgList[position]);
Toast.makeText(context, "username" + tmpObj.getString("username"), Toast.LENGTH_LONG).show();
holder.textView.setText(tmpObj.getString("username"));
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return arrayofresults.length();
}
class MasonryView extends RecyclerView.ViewHolder {
ImageView imageView;
TextView textView;
public MasonryView(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.img);
textView = (TextView) itemView.findViewById(R.id.img_name);
}
}
}
公共类MasonryAdapter扩展了RecyclerView.Adapter{
int[]imgList={R.mipmap.ic_启动器,R.mipmap.ic_启动器,R.mipmap.ic_启动器,R.mipmap.ic_启动器,
R.mipmap.ic_启动器,R.mipmap.ic_启动器,R.mipmap.ic_启动器,R.mipmap.ic_启动器,
R.mipmap.ic_启动器,R.mipmap.ic_启动器};
JSONArray阵列结果;
私人语境;
公共MasonryAdapter(上下文、字符串结果)抛出JSONException{
this.context=上下文;
arrayofresults=新的JSONArray(结果);
Toast.makeText(上下文,“length”+arrayofresults.length(),Toast.length_LONG.show();
}
@凌驾
public MasonryView onCreateViewHolder(视图组父级,int-viewType){
View layoutView=LayoutFlater.from(parent.getContext()).flate(R.layout.grid_项,parent,false);
MasonryView MasonryView=新建MasonryView(布局视图);
返回masonryView;
}
@凌驾
BindViewHolder上的公共无效(MasonryView holder,内部位置){
试一试{
JSONObject tmpObj=arrayofresults.getJSONObject(位置);
holder.imageView.setImageResource(imgList[position]);
Toast.makeText(上下文,“username”+tmpObj.getString(“username”),Toast.LENGTH_LONG.show();
holder.textView.setText(tmpObj.getString(“用户名”);
}捕获(JSONException e){
e、 printStackTrace();
}
}
@凌驾
public int getItemCount(){
返回arrayofresults.length();
}
类MasonryView扩展了RecyclerView.ViewHolder{
图像视图图像视图;
文本视图文本视图;
公共MasonryView(查看项目视图){
超级(项目视图);
imageView=(imageView)itemView.findViewById(R.id.img);
textView=(textView)itemView.findViewById(R.id.img_name);
}
}
}
我是这样设置的:
mRecyclerView.setAdapter(适配器);
但还是什么都没表现出来。我试图用一个片段来展示它。
以下是片段的代码:
public class UserListFragment extends Fragment {
TextView usernameView;
RecyclerView mRecyclerView;
Context context;
MasonryAdapter adapter;
private View view;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.home_fragment, container, false);
try {
init();
} catch (JSONException e) {
e.printStackTrace();
}
return view;
}
private void init() throws JSONException {
context = this.getActivity();
usernameView = (TextView) view.findViewById(R.id.user);
mRecyclerView = (RecyclerView) view.findViewById(R.id.masonry_grid);
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
new LoginRunner().execute("http://velvet.kavalinis.eu/getUsers.php");
}
public void showResult(String serverResponse) throws JSONException {
if (serverResponse == null || serverResponse.isEmpty()) {
final AlertDialog alertDialog = new AlertDialog.Builder(context).create();
TextView title = new TextView(context);
title.setText(context.getResources().getString(R.string.login_fail_title));
title.setPadding(10, 10, 10, 10); // Set Position
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.BLACK);
title.setTextSize(20);
alertDialog.setCustomTitle(title);
TextView msg = new TextView(context);
msg.setText(context.getResources().getString(R.string.login_fail));
msg.setGravity(Gravity.CENTER_HORIZONTAL);
msg.setTextColor(Color.BLACK);
alertDialog.setView(msg);
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, context.getResources().getString(R.string.try_again_btn), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.cancel();
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, context.getResources().getString(R.string.register_btn), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
new Dialog(context.getApplicationContext());
alertDialog.show();
} else {
adapter = new MasonryAdapter(this.getActivity(), serverResponse);
mRecyclerView.setAdapter(adapter);
SpacesItemDecoration decoration = new SpacesItemDecoration(16);
mRecyclerView.addItemDecoration(decoration);
}
}
private class LoginRunner extends AsyncTask<String, String, String> {
//private String resp;
ProgressDialog progressDialog;
@Override
protected String doInBackground(String... params) {
String responseStr = null;
try {
// HttpClient
HttpClient httpClient = new DefaultHttpClient();
// post header
HttpPost httpPost = new HttpPost("https://velvet.gr);
// add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("username", "test"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// execute HTTP post request
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
responseStr = EntityUtils.toString(resEntity).trim();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return responseStr;
}
@Override
protected void onPostExecute(String result) {
try {
if (progressDialog != null) {
progressDialog.dismiss();
}
showResult(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected void onPreExecute() {
Toast.makeText(context, "Loading...", Toast.LENGTH_SHORT).show();
}
}
公共类UserListFragment扩展了片段{
TextView用户名视图;
回收视图mRecyclerView;
语境;
MasonryAdapter适配器;
私人视野;
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图=充气机。充气(R.layout.home\u碎片,容器,假);
试一试{
init();
}捕获(JSONException e){
e、 printStackTrace();
}
返回视图;
}
私有void init()抛出JSONException{
context=this.getActivity();
usernameView=(TextView)view.findViewById(R.id.user);
mRecyclerView=(RecyclerView)view.findviewbyd(R.id.grid);
mRecyclerView.setLayoutManager(新的StaggedGridLayoutManager(2,StaggedGridLayoutManager.VERTICAL));
新建LoginRunner()。执行(“http://velvet.kavalinis.eu/getUsers.php");
}
public void showResult(字符串serverResponse)抛出JSONException{
if(serverResponse==null | | serverResponse.isEmpty()){
final AlertDialog AlertDialog=新建AlertDialog.Builder(context.create();
文本视图标题=新文本视图(上下文);
title.setText(context.getResources().getString(R.string.login\u fail\u title));
title.setPadding(10,10,10,10);//设置位置
标题:设置重力(重心);
标题.setTextColor(颜色.黑色);
标题:setTextSize(20);
alertDialog.setCustomTitle(标题);
TextView msg=新的TextView(上下文);
msg.setText(context.getResources().getString(R.string.login_fail));
msg.setGravity(重心水平);
msg.setTextColor(颜色为黑色);
alertDialog.setView(msg);
alertDialog.setButton(alertDialog.BUTTON_中立,context.getResources().getString(R.string.try_重试),new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
alertDialog.cancel();
}
});
alertDialog.setButton(alertDialog.BUTTON_负值,context.getResources().getString(R.string.register_btn),new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
}
});
新建对话框(context.getApplicationContext());
alertDialog.show();
}否则{
adapter=new MasonryAdapter(this.getActivity(),serverResponse);
mRecyclerView.setAdapter(适配器);
SpacesItemDecoration装饰=新的SpacesItemDecoration(16);
mRecyclerView.附加装饰(装饰);
}
}
私有类LoginRunner扩展异步任务{
//私有字符串resp;
进行对话进行对话;
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串responsest=null;
试一试{
//HttpClient
HttpClient HttpClient=新的DefaultHttpClient();
//柱头
HttpPost HttpPost=新的HttpPost(“https://velvet.gr);
//添加您的数据
List nameValuePairs=新的ArrayList(1);
添加(新的BasicNameValuePair(“用户名”、“测试”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP post请求
HttpResponse response=httpClient.execute(httpPost);
HttpEntity当前性=response.getEntity();
if(最近性!=null){
responsest=EntityUtils.toString(resEntity.trim();
}
}