我的recyclerview显示了一个数据,但我以json
的形式在logcat中收到了多个数据这是主文件
Stitle.java
package com.desktop.app;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
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.HashMap;
import java.util.List;
import java.util.Map;
public class Stitle extends AppCompatActivity {
EditText searchtitle;
private RequestQueue requestQueue ;
private List<list> productList = new ArrayList<>();
private RecyclerView recyclerView;
private RecyclerView.Adapter listAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stitle);
searchtitle = findViewById(R.id.searchtitle);
searchtitle.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
foji();
setContentView(R.layout.activity_smain);
listAdapter = new listAdapter(Stitle.this,productList);
recyclerView = (RecyclerView) findViewById(R.id.recylcerView);
recyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(Stitle.this);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(listAdapter);
return true;
}
return false;
}
});
}
public void foji(){
RequestQueue requestQueue=Volley.newRequestQueue(this);
Uri.Builder builder=new Uri.Builder();
builder.scheme("http")
.authority("192.168.0.136")
.appendPath("fyp")
.appendPath("stitle.php")
.appendQueryParameter("Title",searchtitle.getText().toString());
StringRequest stringRequest=new StringRequest(Request.Method.POST, builder.build().toString(), new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("Info",response.toString());
try {
JSONObject jsonObject=new JSONObject(response);
JSONArray jsonArray=jsonObject.getJSONArray("search");
for(int i=0;i<jsonArray.length();i++){
JSONObject product=jsonArray.getJSONObject(i);
boolean add = productList.add(new list(
product.getLong("isbn"),
product.getString("title"),
product.getString("authors"),
product.getInt("accession"),
product.getString("publisher"),
product.getInt("pubyear"),
product.getInt("pages"),
product.getInt("rak"),
product.getInt("hr"),
product.getInt("vr"),
product.getLong("barcode")
));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Stitle.this, "Foji Error", Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(stringRequest);
}
}
这是setter和getter的文件
list.java
package com.desktop.app;
import java.io.Serializable;
public class list implements Serializable {
private long isbn;
private String title;
private String authors;
private int accession;
private String publisher;
private int pubyear;
private int pages;
private int rak;
private int hr;
private int vr;
private long barcode;
public list(long isbn, String title, String authors, int accession, String publisher, int pubyear, int pages, int rak, int hr, int vr, long barcode) {
this.isbn = isbn;
this.title = title;
this.authors = authors;
this.accession = accession;
this.publisher = publisher;
this.pubyear = pubyear;
this.pages = pages;
this.rak = rak;
this.hr = hr;
this.vr = vr;
this.barcode = barcode;
}
public long getIsbn() {
return isbn;
}
public String getTitle() {
return title;
}
public String getAuthors() {
return authors;
}
public int getAccession() {
return accession;
}
public String getPublisher() {
return publisher;
}
public int getPubyear(){
return pubyear;
}
public int getPages(){
return pages;
}
public int getRak(){
return rak;
}
public int getHr(){
return hr;
}
public int getVr(){
return vr;
}
public long getBarcode() {
return barcode;
}
public void setIsbn(long isbn) {
this.isbn = isbn;
}
public void setTitle(String title) {
this.title = title;
}
public void setAuthors(String authors) {
this.authors = authors;
}
public void setAccession(int accession) {
this.accession = accession;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public void setPubyear(int pubyear) {
this.pubyear = pubyear;
}
public void setPages(int pages) {
this.pages = pages;
}
public void setRak(int rak) {
this.rak = rak;
}
public void setHr(int hr) {
this.hr = hr;
}
public void setVr(int vr) {
this.vr = vr;
}
public void setBarcode(long barcode) {
this.barcode = barcode;
}
}
这是lisAdapter的文件
listAdapter.java
package com.desktop.app;
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.TextView;
import com.bumptech.glide.request.RequestOptions;
import java.util.List;
public class listAdapter extends RecyclerView.Adapter<listAdapter.ProductViewHolder> {
RequestOptions options ;
private Context mCtx;
private List<list> pdata;
listAdapter(Context mCtx, List productList) {
this.mCtx = mCtx;
this.pdata = productList;
}
@Override
public ProductViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
View view ;
LayoutInflater inflater = LayoutInflater.from(mCtx);
view = inflater.inflate(R.layout.slist,parent,false);
return new ProductViewHolder(view);
}
@Override
public void onBindViewHolder( ProductViewHolder holder, final int position) {
list product = pdata.get(position);
holder.textviewisbn.setText(String.valueOf(product.getIsbn()));
holder.textviewtitle.setText(product.getTitle());
holder.textviewauthors.setText(product.getAuthors());
holder.textviewacc.setText(String.valueOf(product.getAccession()));
holder.textviewpublisher.setText(product.getPublisher());
holder.textviewpubyear.setText(String.valueOf(product.getPubyear()));
}
@Override
public int getItemCount() {
return pdata.size();
}
public static class ProductViewHolder extends RecyclerView.ViewHolder {
TextView textviewisbn, textviewtitle, textviewauthors, textviewacc, textviewpublisher, textviewpubyear;
public ProductViewHolder(View itemView) {
super(itemView);
textviewisbn = itemView.findViewById(R.id.textviewisbn);
textviewtitle = itemView.findViewById(R.id.textviewtitle);
textviewauthors = itemView.findViewById(R.id.textviewauthors);
textviewacc = itemView.findViewById(R.id.textviewacc);
textviewpublisher = itemView.findViewById(R.id.textviewpublisher);
textviewpubyear = itemView.findViewById(R.id.textviewpubyear);
}
}
}
slist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<TextView
android:id="@+id/textviewisbn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_above="@id/textviewtitle"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textColor="#000000" />
<TextView
android:id="@+id/textviewtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textColor="#000000" />
<TextView
android:id="@+id/textviewauthors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textviewtitle"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small" />
<TextView
android:id="@+id/textviewacc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textviewauthors"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small.Inverse"
android:textStyle="bold" />
<TextView
android:id="@+id/textviewpublisher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textviewacc"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
<TextView
android:id="@+id/textviewpubyear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textviewpublisher"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
recylerview xml文件
activity_smain.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.desktop.app.listactivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recylcerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="745dp"
tools:layout_editor_absoluteY="-51dp" >
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
Recyclerview显示一个数据,但我以json
的形式在logcat中接收多个数据I / Info:{&#34; status&#34;:true,&#34; search&#34;:[{&#34; isbn&#34;:195472462,&#34; title&#34;: &#34; Oxford Practice Grammer&#34;,&#34;作者&#34;:&#34; john Eastwood&#34;,&#34;加入&#34;:1,&#34;出版商&#34;: &#34; Ameena Saiyid牛津大学&#34;,#34; pubyear&#34;:2014,&#34; pages&#34;:432,&#34; rak&#34;:1,&#34; hr& #34;:1,&#34; VR&#34;:1,&#34;条形码&#34;:195472462},{&#34; ISBN&#34;:9694946719,&#34;标题&#34 ;: &#34;高中英语语法&#34;,&#34;作者&#34;:&#34; Wren,马丁&#34;,#34;加入&#34;:4,&#34;出版商&# 34;:&#34; Paramount Publishing&#34;,&#34; pubyear&#34;:2010,&#34; pages&#34;:418,&#34; rak&#34;:1,&#34;小时&#34;:1,&#34; VR&#34:4,&#34;条形码&#34;:9694946719}]}
此数据收录为json,但只有一个记录显示在列表image show's that only one record on list
中
答案 0 :(得分:1)
您可以在滚动中查看数据,因为您已将列表项高度设置为
即可match_parent
..您只需将其设置为wrap_content
<强> slist.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<TextView
android:id="@+id/textviewisbn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_above="@id/textviewtitle"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textColor="#000000" />
<TextView
android:id="@+id/textviewtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textColor="#000000" />
<TextView
android:id="@+id/textviewauthors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textviewtitle"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small" />
<TextView
android:id="@+id/textviewacc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textviewauthors"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small.Inverse"
android:textStyle="bold" />
<TextView
android:id="@+id/textviewpublisher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textviewacc"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
<TextView
android:id="@+id/textviewpubyear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textviewpublisher"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>