You are on page 1of 7

Recycler view

//add recycler view dependency

//activity_list.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=".ListActivity">

<android.support.v7.widget.RecyclerView

android:id="@+id/recyclerview"

android:layout_width="match_parent"

android:layout_height="match_parent">

</android.support.v7.widget.RecyclerView>

</RelativeLayout>

//list_data.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:padding="10dp"

xmlns:app="http://schemas.android.com/apk/res-auto">

<LinearLayout

android:padding="10dp"

android:layout_width="match_parent"
android:layout_height="wrap_content"

android:orientation="vertical">

<LinearLayout

android:layout_marginLeft="10dp"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal">

<TextView

android:textColor="#111"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Name:"/>

<TextView

android:id="@+id/txt_description"

android:textColor="#111"

android:layout_marginLeft="10dp"

style="@style/Base.TextAppearance.AppCompat.Medium"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>

</LinearLayout>

<LinearLayout

android:layout_marginTop="10dp"

android:layout_marginLeft="10dp"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal">
<TextView

android:textColor="#111"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Sell Price:"/>

<TextView

android:id="@+id/txt_sell_price"

android:textColor="#111"

android:layout_marginLeft="10dp"

style="@style/Base.TextAppearance.AppCompat.Medium"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

</LinearLayout>

</LinearLayout>

</LinearLayout>

//Model class item_data.java

public class item_data {

private String description;

private String sell_price;

public item_data(String description, String sell_price) {

this.description = description;

this.sell_price = sell_price;

}
public String getDescription() {

return description;

public String getSell_price() {

return sell_price;

//myAdapter.java

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

private List<item_data>list_data;

public myAdapter(List<item_data> item_data) {

this.list_data = item_data;

@Override

public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view=
LayoutInflater.from(parent.getContext()).inflate(R.layout.list_data,parent,false);

return new ViewHolder(view);

@Override

public void onBindViewHolder(ViewHolder holder, int position) {

item_data listData=list_data.get(position);

holder.txtdescription.setText(listData.getDescription());

holder.txtsell_price.setText(listData.getSell_price());

}
@Override

public int getItemCount() {

return list_data.size();

public class ViewHolder extends RecyclerView.ViewHolder{

private TextView txtdescription,txtsell_price;

public ViewHolder(View itemView) {

super(itemView);

txtdescription=(TextView)itemView.findViewById(R.id.txt_description);

txtsell_price=(TextView)itemView.findViewById(R.id.txt_sell_price);

//mainActivity.java

btnview.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Intent intent = new Intent(getApplicationContext(),


ListActivity.class);

startActivity(intent);

});

//ListActivity.java

public class ListActivity extends AppCompatActivity {

private static final String urlString ="http://192.168.1.7/api/item/" ;

private List<item_data> list_data;

private RecyclerView rv;


private myAdapter adapter;

private URL myURL;

private Context mContext;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

mContext = getApplicationContext();

setContentView(R.layout.activity_list);

rv=(RecyclerView)findViewById(R.id.recyclerview);

rv.setHasFixedSize(true);

rv.setLayoutManager(new LinearLayoutManager(this));

list_data=new ArrayList<>();

adapter=new myAdapter(list_data);

getItemData();

private void getItemData() {

try {

myURL = new URL(urlString);

} catch (MalformedURLException e) {

e.printStackTrace();

Log.i("url","url"+ myURL);

//RequestQueue requestQueue = Volley.newRequestQueue(mContext);

// stringRequest=new StringRequest(Request.Method.GET,

JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.GET,

urlString,null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {

try {

//JSONObject jsonObject=new JSONObject(response);

//JSONArray array=jsonObject.getJSONArray("data");

JSONArray array=response.getJSONArray("data");

for (int i=0; i<array.length(); i++ ){

JSONObject ob=array.getJSONObject(i);

item_data listData=new
item_data(ob.getString("description")

,ob.getString("sell_price"));

list_data.add(listData);

rv.setAdapter(adapter);

} catch (JSONException e) {

e.printStackTrace();

}, new Response.ErrorListener() {

@Override

public void onErrorResponse(VolleyError error) {

});

RequestQueue requestQueue= Volley.newRequestQueue(this);

requestQueue.add(jsonRequest);

You might also like