gpt4 book ai didi

android - 我的 RecyclerView Activity 显示空白 Activity

转载 作者:行者123 更新时间:2023-11-29 18:26:13 25 4
gpt4 key购买 nike

我开发了一个应用程序,通过查看教程(简化编码)来显示使用 RecyclerView 和卡片 View 制作的列表。卡片 View 有两个 TextView 和一个 ImageView 。我想从 mysql 数据库获取数据和图像 URL使用 picasso 加载到 ImageView 。我的应用程序正在运行,但没有显示任何内容,并且出现错误响应。

我的应用程序 gradle 文件是

apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.example.selvam.listapp"
minSdkVersion 14
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:recyclerview-v7:24.2.1'
compile 'com.android.support:cardview-v7:24.2.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.volley:volley:1.0.0'



testCompile 'junit:junit:4.12'
}

我的 recycler_view xml 文件是

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.selvam.listapp.MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/myRecyclerView"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"></android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

我的card_view xml文件是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="219dp"
android:layout_weight="0.16">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp"
android:id="@+id/tvName"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp"
android:id="@+id/tvPrice"
/>

<ImageView
android:id="@+id/imageView"
android:layout_width="309dp"
android:layout_height="148dp"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"/>


</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>

“我要在类(class)中列出的项目”

package com.example.selvam.listapp;

/**
* Created by SELVAM on 11-09-2017.
*/

public class ListItems {
private String name;
private String price;
private String image;

public ListItems(String name,String price,String image) {
this.image=image;
this.price=price;
this.name=name;

}

public String getName() {
return name;
}

public String getPrice() {
return price;
}

public String getImage() {
return image;
}


}

我的回收器适配器是

package com.example.selvam.listapp;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

/**
* Created by SELVAM on 11-09-2017.
*/

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

private List<ListItems> listItems;
private Context context;

public RecyclerAdapter(List<ListItems> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view,parent);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ListItems item=listItems.get(position);
holder.tvName.setText(item.getName());
holder.tvPrice.setText(item.getPrice());
Picasso.with(context).load(item.getImage()).into(holder.imageView);


}


@Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView tvName;
public TextView tvPrice;
public ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
tvName=(TextView)itemView.findViewById(R.id.tvName);
tvPrice=(TextView)itemView.findViewById(R.id.tvPrice);
imageView=(ImageView)itemView.findViewById(R.id.imageView);
}
}
}

我使用 volley 进行 json 解析的主要 Activity 是

package com.example.selvam.listapp;

import android.app.ProgressDialog;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
private static final String URL_LIST="http://192.168.28.2/signup/activities/getDataToListview.php";

private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<ListItems> listitems;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=(RecyclerView)findViewById(R.id.myRecyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
listitems=new ArrayList<>();
loadRecyclerView();

}
public void loadRecyclerView(){
final ProgressDialog progressDialog=new ProgressDialog(this);
progressDialog.setMessage("loading lists...");
progressDialog.show();
StringRequest stringRequest=new StringRequest(Request.Method.GET, URL_LIST, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
try {
JSONObject jsonObject=new JSONObject(response);
JSONArray array=jsonObject.getJSONArray("list");

for(int i=0;i<array.length();i++){
JSONObject js=array.getJSONObject(i);
ListItems item=new ListItems(
js.getString("name"),js.getString("price"),js.getString("image")
);
listitems.add(item);
}
adapter=new RecyclerAdapter(listitems,getApplicationContext());
recyclerView.setAdapter(adapter);



} catch (JSONException e) {
e.printStackTrace();
}


}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(),"lists cannot be displayed",Toast.LENGTH_SHORT).show();

}
}
);
RequestQueue requestQueue= Volley.newRequestQueue(this);
requestQueue.add(stringRequest);


}
}

我的 mysql 数据库的 json 输出是

{"list":[{"name":"messi","price":"hundred","image":"http:\/\/192.168.28.2\/img\/messi.jpeg"},{"name":"goat","price":"twohundred","image":"http:\/\/192.168.28.2\/img\/goat.jpg"},{"name":"cristiano","price":"threehundred","image":"http:\/\/192.168.28.2\/img\/cr7.jpg"}]}

我搜索并尝试了很多。请帮助我纠正我的错误。提前致谢

最佳答案

试试吧!

第 1 步:检查 recyclerView 的宽度 = 0dp。尝试更改=“match_parent”。如果不起作用,请尝试步骤2

第 2 步:尝试以下代码:

public void loadRecyclerView(){
final ProgressDialog progressDialog=new ProgressDialog(this);
progressDialog.setMessage("loading lists...");
progressDialog.show();
adapter=new RecyclerAdapter(listitems,getApplicationContext());
recyclerView.setAdapter(adapter);
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
// display response
Log.d("Response", response.toString());
try {
JSONArray array=response.getJSONArray("list");

for(int i=0;i<array.length();i++){
JSONObject js=array.getJSONObject(i);
ListItems item=new ListItems(
js.getString("name"),js.getString("price"),js.getString("image")
);
listitems.add(item);
}

} catch (JSONException e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();

}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", response);
}
}
);

RequestQueue requestQueue= Volley.newRequestQueue(this);
requestQueue.add(getRequest);
}

关于android - 我的 RecyclerView Activity 显示空白 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46169194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com