How to set a imageButton is an RSS

Posted by L?c Song on Stack Overflow See other posts from Stack Overflow or by L?c Song
Published on 2014-08-23T04:13:51Z Indexed on 2014/08/23 4:20 UTC
Read the original article Hit count: 445

Filed under:

I have a feed_layout.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <LinearLayout
                android:baselineAligned="false"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:orientation="horizontal" >
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical" >

                    <ImageButton
                        android:layout_width="138dp"
                        android:layout_height="138dp"
                        android:layout_gravity="center"
                        android:onClick="homeImageButton"
                        android:scaleType="fitStart"
                        android:src="@drawable/home"
                        android:tag="1" />

                </LinearLayout>
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical" >

                    <ImageButton
                        android:layout_width="138dp"
                        android:layout_height="138dp"
                        android:layout_gravity="center"
                        android:scaleType="centerCrop"
                        android:onClick="thegioiImageButton"
                        android:src="@drawable/home"
                        android:tag="2" />

                </LinearLayout>

    </LinearLayout>
    <LinearLayout
                    android:baselineAligned="false"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:orientation="horizontal" >
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:orientation="vertical" >

                        <ImageButton
                            android:layout_width="138dp"
                            android:layout_height="138dp"
                            android:layout_gravity="center"
                            android:scaleType="centerCrop"
                            android:onClick="giaitriImageButton"
                            android:src="@drawable/home"
                            android:tag="3" />

                    </LinearLayout>
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:orientation="vertical" >

                        <ImageButton
                            android:layout_width="138dp"
                            android:layout_height="138dp"
                            android:layout_gravity="center"
                            android:scaleType="centerCrop"
                            android:onClick="thethaoImageButton"
                            android:src="@drawable/home"
                            android:tag="4" />
                    </LinearLayout>              
    </LinearLayout> 
    <LinearLayout
                    android:baselineAligned="false"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:orientation="horizontal" >
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:orientation="vertical" >

                        <ImageButton
                            android:layout_width="138dp"
                            android:layout_height="138dp"
                            android:layout_gravity="center"
                            android:scaleType="centerCrop"
                            android:onClick="khoahocImageButton"
                            android:src="@drawable/home"
                            android:tag="5" />

                    </LinearLayout>
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:orientation="vertical" >

                        <ImageButton
                            android:layout_width="138dp"
                            android:layout_height="138dp"
                            android:layout_gravity="center"
                            android:scaleType="centerCrop"
                            android:onClick="xeImageButton"
                            android:src="@drawable/home"
                            android:tag="6" />
                    </LinearLayout>              
    </LinearLayout> 

</LinearLayout>

and feedActivity.java

package com.dqh.vnexpressrssreader;
import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class FeedActivity extends Activity {
    public String  tagImg;
    private static final String TAG = "FeedActivity";
    @Override    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feed_layout);
    }
    public void homeImageButton(View v) {
        ImageButton imageButtonClicked = (ImageButton)v;     
        tagImg = imageButtonClicked.getTag().toString();
        setTagImg(tagImg);
        String tt = getTagImg();
        Log.d(TAG, "FeedId: " + tt);
        Intent intent = new Intent(getApplicationContext(), ItemsActivity.class);
        startActivityForResult(intent, 0);
    }
    public void thegioiImageButton(View v) {
        ImageButton imageButtonClicked = (ImageButton)v;    
        tagImg = imageButtonClicked.getTag().toString();
        //Log.d(TAG, "FeedId: " + imageButtonClicked.getTag());
        Log.d(TAG, "FeedId: " + tagImg);
        Intent intent = new Intent(getApplicationContext(), ItemsActivity.class);
        startActivityForResult(intent, 0);
    }
}

and RssReader.java

/**
 * 
 */
package com.dqh.vnexpressrssreader.reader;

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

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

import com.dqh.vnexpressrssreader.FeedActivity;
import com.dqh.vnexpressrssreader.NewsRssReaderDB;
import com.dqh.vnexpressrssreader.util.RSSHandler;
import com.dqh.vnexpressrssreader.util.Tintuc;

import android.content.Context;
import android.text.Html;
import android.util.Log;

/**
 * @author rob
 *
 */
public class RssReader {

    private final static String TAG = "RssReader";

    private final static String BOLD_OPEN = "<B>";
    private final static String BOLD_CLOSE = "</B>";
    private final static String BREAK = "<BR>";
    private final static String ITALIC_OPEN = "<I>";
    private final static String ITALIC_CLOSE = "</I>";
    private final static String SMALL_OPEN = "<SMALL>";
    private final static String SMALL_CLOSE = "</SMALL>";

    /**
     * This method defines a feed URL and then calles our SAX Handler to read the tintuc list
     * from the stream
     * 
     * @return List<JSONObject> - suitable for the List View activity
     */
    public static List<JSONObject> getLatestRssFeed(Context context) {
        NewsRssReaderDB newsRssReaderDB = new NewsRssReaderDB(context);

        List<Tintuc> tintucsFromDB = newsRssReaderDB.getLists();
        return fillData(tintucsFromDB);
    }
    public static void getLatestRssFeed(Context context, String feed) {
        NewsRssReaderDB newsRssReaderDB = new NewsRssReaderDB(context);

        feed = "http://vnexpress.net/rss/the-gioi.rss";
    //RSS 2    feed = "http://vnexpress.net/rss/the-thao.rss";
    //RSS 3    feed = "http://vnexpress.net/rss/home.rss";

        RSSHandler rh = new RSSHandler();
        List<Tintuc> tintucs =  rh.getLatestTintucs(feed);
        if ((tintucs != null) && (tintucs.size() > 0)) {
            for (Tintuc tintuc : tintucs) {
                if ((tintuc.getUrl() != null) && !newsRssReaderDB.checkUrlExist(tintuc.getUrl().toString())) {
                    long tintucId = newsRssReaderDB.insertTintuc(tintuc);
                    if (tintucId > 0) {
                        Log.d(TAG, "saved tintucId: " + tintucId);
                    } else {
                        Log.e(TAG, "saved tintucId fail");
                    }
                } else {
                    Log.e(TAG, "tintucs exist!");
                }
            }
        }
    }

    /**
     * This method takes a list of Tintuc objects and converts them in to the 
     * correct JSON format so the info can be processed by our list view
     * 
     * @param tintucs - list<Tintuc>
     * @return List<JSONObject> - suitable for the List View activity
     */
    private static List<JSONObject> fillData(List<Tintuc> tintucs) {

        List<JSONObject> items = new ArrayList<JSONObject>();
        for (Tintuc tintuc : tintucs) {
            JSONObject current = new JSONObject();
            try {
                buildJsonObject(tintuc, current);
            } catch (JSONException e) {
                Log.e("RSS ERROR", "Error creating JSON Object from RSS feed");
            }
            items.add(current);
        }

        return items;
    }


    /**
     * This method takes a single Tintuc Object and converts it in to a single JSON object
     * including some additional HTML formating so they can be displayed nicely
     * 
     * @param tintuc
     * @param current
     * @throws JSONException
     */
    private static void buildJsonObject(Tintuc tintuc, JSONObject current) throws JSONException {
        String title = tintuc.getTieude();
        String description = tintuc.getMota();

        /////////////////////////
        //////// 2 /////////////
        String date = tintuc.getPubDate();
        String imgLink = tintuc.getImgLink();

        StringBuffer sb = new StringBuffer();
        sb.append(BOLD_OPEN).append(title).append(BOLD_CLOSE);
        sb.append(BREAK);
        sb.append(description);
        sb.append(BREAK);
        sb.append(SMALL_OPEN).append(ITALIC_OPEN).append(date).append(ITALIC_CLOSE).append(SMALL_CLOSE);

        current.put("text", Html.fromHtml(sb.toString()));
        current.put("imageLink", imgLink);
        current.put("url", tintuc.getUrl().toString());
        current.put("title", tintuc.getTieude());
    }
}

I have 1 array RSS and I want each ImageButton is assigned a Rss??.

I have attempt to call to FeedActivity from RSSReader but not be help me !

© Stack Overflow or respective owner

Related posts about rss