Skip to content

Instantly share code, notes, and snippets.

@alvareztech
Last active May 29, 2017 02:52
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 alvareztech/34ad5603d7399cd803584127dbb034d1 to your computer and use it in GitHub Desktop.
Save alvareztech/34ad5603d7399cd803584127dbb034d1 to your computer and use it in GitHub Desktop.
Android: Noticias App, Realm
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="tech.alvarez.noticias.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tech.alvarez.noticias">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
// del módulo
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "tech.alvarez.noticias"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:design:24.0.0'
compile 'com.android.support:cardview-v7:24.0.0'
compile 'com.android.support:recyclerview-v7:24.0.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="colorPrimaryText">#DE000000</color>
<color name="colorSecondaryText">#8A000000</color>
<color name="colorDisabledText">#61000000</color>
<color name="colorPrimaryDarkText">#FFFFFF</color>
<color name="colorSecondaryDarkText">#B3FFFFFF</color>
<color name="colorDisabledDarkText">#80FFFFFF</color>
</resources>
<?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_marginBottom="0dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:orientation="horizontal"
android:paddingBottom="16dp"
card_view:cardCornerRadius="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/fotoImageView"
android:layout_width="match_parent"
android:layout_height="168dp"
android:background="@color/colorAccent" />
<TextView
android:id="@+id/tituloTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingEnd="16dp"
android:paddingStart="16dp"
android:paddingTop="24dp"
android:text="@string/app_name"
android:textColor="@color/colorPrimaryText"
android:textSize="24sp" />
<TextView
android:id="@+id/descripcionTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:paddingEnd="16dp"
android:paddingStart="16dp"
android:paddingTop="4dp"
android:text="@string/app_name"
android:textColor="@color/colorSecondaryText"
android:textSize="14sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
package tech.alvarez.noticias.utils;
import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Xml;
import org.xml.sax.Attributes;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import tech.alvarez.noticias.models.Noticia;
/**
* Created by Daniel Alvarez on 15/7/16.
* Copyright © 2016 Alvarez.tech. All rights reserved.
*/
public class LaRazonParserXML {
private URL url;
private Noticia noticiaActual;
public LaRazonParserXML(String url) {
try {
this.url = new URL(url);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
public ArrayList<Noticia> parse() {
final ArrayList<Noticia> noticias = new ArrayList<Noticia>();
RootElement root = new RootElement("rss");
Element channel = root.getChild("channel");
Element item = channel.getChild("item");
item.setStartElementListener(new StartElementListener() {
@Override
public void start(Attributes attributes) {
noticiaActual = new Noticia();
}
});
item.getChild("title").setEndTextElementListener(new EndTextElementListener() {
@Override
public void end(String s) {
noticiaActual.setTitulo(s);
}
});
item.getChild("description").setEndTextElementListener(new EndTextElementListener() {
@Override
public void end(String s) {
noticiaActual.setDescripcion(s);
}
});
item.getChild("link").setEndTextElementListener(new EndTextElementListener() {
@Override
public void end(String s) {
noticiaActual.setUrlNoticia(s);
}
});
item.getChild("enclosure").setStartElementListener(new StartElementListener() {
@Override
public void start(Attributes attributes) {
String urlFoto = attributes.getValue("url");
noticiaActual.setUrlFoto(urlFoto);
}
});
item.setEndElementListener(new EndElementListener() {
@Override
public void end() {
noticias.add(noticiaActual);
}
});
try {
Xml.parse(getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
} catch (Exception ex) {
throw new RuntimeException(ex);
}
return noticias;
}
private InputStream getInputStream() {
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
package tech.alvarez.noticias;
import android.os.AsyncTask;
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 java.util.ArrayList;
import tech.alvarez.noticias.adapters.NoticiasAdapter;
import tech.alvarez.noticias.models.Noticia;
import tech.alvarez.noticias.utils.LaRazonParserXML;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private NoticiasAdapter noticiasAdapter;
private ArrayList<Noticia> noticias;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
noticiasAdapter = new NoticiasAdapter(this);
recyclerView.setAdapter(noticiasAdapter);
ObtenerLaRazonXML tarea = new ObtenerLaRazonXML();
tarea.execute("http://www.la-razon.com/rss/nacional/");
}
public class ObtenerLaRazonXML extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... strings) {
String url = strings[0];
LaRazonParserXML parserXML = new LaRazonParserXML(url);
noticias = parserXML.parse();
return null;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
for (int i = 0; i < noticias.size(); i++) {
Noticia n = noticias.get(i);
noticiasAdapter.adicionar(n);
Log.i("MIAPP", n.getTitulo());
Log.i("MIAPP", n.getDescripcion());
Log.i("MIAPP", n.getUrlFoto());
Log.i("MIAPP", n.getUrlNoticia());
}
}
}
}
package tech.alvarez.noticias.models;
/**
* Created by Daniel Alvarez on 15/7/16.
* Copyright © 2016 Alvarez.tech. All rights reserved.
*/
public class Noticia {
private String titulo;
private String descripcion;
private String urlFoto;
private String urlNoticia;
public String getTitulo() {
return titulo;
}
public void setTitulo(String titulo) {
this.titulo = titulo;
}
public String getDescripcion() {
return descripcion;
}
public void setDescripcion(String descripcion) {
this.descripcion = descripcion;
}
public String getUrlFoto() {
return urlFoto;
}
public void setUrlFoto(String urlFoto) {
this.urlFoto = urlFoto;
}
public String getUrlNoticia() {
return urlNoticia;
}
public void setUrlNoticia(String urlNoticia) {
this.urlNoticia = urlNoticia;
}
}
package tech.alvarez.noticias.adapters;
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.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import java.util.ArrayList;
import tech.alvarez.noticias.R;
import tech.alvarez.noticias.models.Noticia;
/**
* Created by Daniel Alvarez on 15/7/16.
* Copyright © 2016 Alvarez.tech. All rights reserved.
*/
public class NoticiasAdapter extends RecyclerView.Adapter<NoticiasAdapter.ViewHolder> {
private ArrayList<Noticia> dataset;
private Context context;
public NoticiasAdapter(Context context) {
this.dataset = new ArrayList<>();
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_noticia, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Noticia n = dataset.get(position);
holder.tituloTextView.setText(n.getTitulo());
holder.descripcionTextView.setText(n.getDescripcion());
Glide.with(context).load(n.getUrlFoto())
.centerCrop()
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(holder.fotoImageView);
}
@Override
public int getItemCount() {
return dataset.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView tituloTextView;
private TextView descripcionTextView;
private ImageView fotoImageView;
public ViewHolder(View itemView) {
super(itemView);
tituloTextView = (TextView) itemView.findViewById(R.id.tituloTextView);
descripcionTextView = (TextView) itemView.findViewById(R.id.descripcionTextView);
fotoImageView = (ImageView) itemView.findViewById(R.id.fotoImageView);
}
}
public void adicionar(Noticia n) {
dataset.add(n);
notifyDataSetChanged();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment