Skip to content

Instantly share code, notes, and snippets.

@rymate1234
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rymate1234/bc08c36b956ec4652284 to your computer and use it in GitHub Desktop.
Save rymate1234/bc08c36b956ec4652284 to your computer and use it in GitHub Desktop.
my recycler view isn't populated wat do
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".NotesListActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/listView"
android:divider="@null"
android:dividerHeight="0dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:fastScrollEnabled="true" />
</RelativeLayout>
package net.rymate.rymatenotes2;
/**
* Created by Ryan on 01/01/2015.
*/
public class Note {
private int id;
private String title;
private String content;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
package net.rymate.rymatenotes2;
import com.google.gson.JsonObject;
import com.koushikdutta.async.future.FutureCallback;
import java.util.List;
/**
* Created by Ryan on 01/01/2015.
*/
public class Notes {
private List<Note> notes;
public Notes(List<Note> notes) {
this.notes = notes;
}
public List<Note> getNotes() {
return notes;
}
public void setNotes(List<Note> notes) {
this.notes = notes;
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="4dp"
android:clickable="true"
android:elevation="2dp"
android:minHeight="?android:attr/listPreferredItemHeight"
android:foreground="?android:attr/selectableItemBackground"
android:mode="twoLine"
card_view:cardCornerRadius="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="6dp"
android:orientation="vertical">
<TextView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/title_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/content_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="3"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</LinearLayout>
</android.support.v7.widget.CardView>
package net.rymate.rymatenotes2;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Base64;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.koushikdutta.ion.Ion;
import java.util.concurrent.ExecutionException;
public class NotesListActivity extends Activity implements RecyclerNotesAdapter.OnItemClickListener {
private RecyclerView mNotesRecycler;
private RecyclerNotesAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes_list);
mNotesRecycler = (RecyclerView) findViewById(R.id.listView);
String credentials = "rymate1234" + ":" + "passw0rd";
String base64EncodedCredentials = Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
JsonObject json = null;
try {
json = Ion.with(this)
.load("http://dev.rymate.co.uk:5000/api/notes")
.setHeader("Authorization", "Basic " + base64EncodedCredentials)
.asJsonObject().get(); // gets a json file like https://gist.github.com/rymate1234/6c9a635f55a53e6652dd
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
Gson gson = new Gson();
Notes notes = gson.fromJson(json.toString(), Notes.class);
adapter = new RecyclerNotesAdapter(notes);
adapter.SetOnItemClickListener(this);
mNotesRecycler.setAdapter(adapter);
mNotesRecycler.setHasFixedSize(true);
mNotesRecycler.setLayoutManager(new LinearLayoutManager(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.notes_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_login) {
// this will do something eventually
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(View view, int position) {
return;
}
}
package net.rymate.rymatenotes2;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
/**
* Created by Ryan on 01/01/2015.
*/
public class RecyclerNotesAdapter extends RecyclerView.Adapter<RecyclerNotesAdapter.ListItemViewHolder> {
private List<Note> notes;
private OnItemClickListener mItemClickListener;
public RecyclerNotesAdapter(Notes notes) {
if (notes == null) {
throw new IllegalArgumentException("modelData must not be null");
}
this.notes = notes.getNotes();
}
@Override
public ListItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.notes_list_item, viewGroup, false);
return new ListItemViewHolder(itemView);
}
@Override
public void onBindViewHolder(ListItemViewHolder viewHolder, int position) {
Note note = notes.get(position);
viewHolder.title.setText(note.getTitle());
viewHolder.text.setText(note.getContent());
viewHolder.id = note.getId();
}
@Override
public int getItemCount() {
return notes.size();
}
public class ListItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView title, text;
int id;
public ListItemViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title_text);
text = (TextView) itemView.findViewById(R.id.content_text);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(v, id);
}
}
}
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
}
@slightfoot
Copy link

<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".NotesListActivity"
    />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment