Discussion Forums  >  iOS / Android Beta Testers

Replies: 0    Views: 129

Jake Chasan
Veteran developer
Profile
Posts: 1685
Reg: May 13, 2011
location unknow...
29,650
04/11/12 08:19 PM (13 years ago)

RSS 1.5 Android

Hi All, I have still been working on the BT_screen_rssReader.java file. I believe that I have had some progress here is the code (I have also imported Obj_RSSStory.java and Adapter_RSSStory.java from 1.4): package com.knightspolo; import java.util.ArrayList; //From 1.4 import org.json.JSONArray; import org.json.JSONObject; //From 1.4 import android.app.Dialog; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.Button; import android.widget.ImageButton; import android.widget.ListView; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; //From 1.5 import android.content.Context; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.LinearLayout; public class BT_screen_rssReader extends Act_ActivityBase { //From 1.5 private boolean didCreate = false; //From 1.4 public DownloadThread downloadThread; public ListView myListView; public ArrayList<Obj_RSSStory> menuItems; public Adapter_RSSStory menuItemAdapter; public int selectedIndex; //onCreate public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.act_global_list); //activity name thisActivityName = "BT_screen_rssReader"; //From 1.5 BT_debugger.showIt(activityName + ":onCreate"); LinearLayout baseView = (LinearLayout)findViewById(R.id.baseView); //remote command remoteDataCommand = "rssFeedViewController"; //From 1.5 //setup background colors... BT_viewUtilities.updateBackgroundColorsForScreen(this, this.screenData); //setup background images.. if(backgroundImageWorkerThread == null){ backgroundImageWorkerThread = new BackgroundImageWorkerThread(); backgroundImageWorkerThread.start(); } //setup navigation bar... LinearLayout navBar = BT_viewUtilities.getNavBarForScreen(this, this.screenData); if(navBar != null){ baseView.addView(navBar); } //inflate this screens layout file... LayoutInflater vi = (LayoutInflater)thisActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View thisScreensView = vi.inflate(R.layout.screen_imagecaption, null); //add the view to the base view... baseView.addView(thisScreensView); //flag as created.. didCreate = true; //appDelegate holds current screen this.appDelegate = (AppDelegate) this.getApplication(); appGuid = appDelegate.currentApp.guid; appApiKey = appDelegate.currentApp.apiKey; screenGuid = appDelegate.currentScreen.screenGuid; saveAsFileName = appDelegate.currentScreen.screenGuid + "_rssFeed.txt"; //back button.. ImageButton btnBack = (ImageButton) findViewById(R.id.btnBack); btnBack.setOnClickListener(new OnClickListener() { public void onClick(View v) { finish(); } }); //title ((TextView)findViewById(R.id.myTitle)).setText(appDelegate.currentScreen.screenTitle); //init selected index selectedIndex = -1; //list view myListView = (ListView)findViewById(R.id.myList); //array of items menuItems = new ArrayList<Obj_RSSStory>(); //setup data adapter int resID = R.layout.list_rss_style_1; menuItemAdapter = new Adapter_RSSStory(this, resID, menuItems); myListView.setAdapter(menuItemAdapter); } //onStart @Override protected void onStart() { super.onStart(); //local or remote JSON text... if(appDelegate.fileExists(saveAsFileName)){ Log.i("ZZ", thisActivityName + ":onStart local data exists"); JSONData = appDelegate.getLocalText(saveAsFileName); parseData(JSONData); }else{ //parse called after when done Log.i("ZZ", thisActivityName + ":onStart downloading data"); downloadData(); } } //onStop @Override protected void onStop(){ super.onStop(); if(downloadThread != null){ boolean retry = true; downloadThread.setThreadRunning(false); while(retry){ try{ downloadThread.join(); retry = false; }catch (Exception je){ Log.i("ZZ", thisActivityName + ":onStop ERROR: " + je.getMessage()); } } } } //download data public void downloadData(){ //show progress showProgress("Loading...", "We'll save this to speed things up for next time."); //appDelegate.currentScreen.jsonScreenOptions holds variables for RSS feed try{ JSONObject raw = new JSONObject(appDelegate.currentScreen.jsonScreenOptions); //build URL String tmpURL = raw.getString("url"); tmpURL += "?appGuid=" + appGuid; tmpURL += "&appApiKey=" + appApiKey; tmpURL += "&screenGuid=" + screenGuid; tmpURL += "&command=" + remoteDataCommand; downloadThread = new DownloadThread(); downloadThread.setDownloadURL(tmpURL); downloadThread.setSaveAsFileName(saveAsFileName); downloadThread.setDownloadType("text"); downloadThread.setThreadRunning(true); downloadThread.start(); }catch (Exception je){ hideProgress(); showAlert("Download Error", "The data returned by the RSS Feed URL is not well formed."); Log.i("ZZ", thisActivityName + ":downloadData appDelegate.currentScreen.jsonScreenOptions is not well formed"); } } //parse data.. public void parseData(String theJSONString){ //parse JSON string //Log.i("ZZ", thisActivityName + ":parseData: " + theJSONString); //empty list view data adapter menuItemAdapter.clear(); myListView.invalidate(); try{ //empty data if previously filled... menuItems.clear(); //create json objects from data string... JSONObject raw = new JSONObject(theJSONString); JSONObject results = raw.getJSONObject("results"); JSONArray stories = results.getJSONArray("stories"); //loop int i = 0; for (i = 0; i < stories.length(); i++){ JSONObject tmpJson = stories.getJSONObject(i); //RSS Story Obj_RSSStory tmpItem = new Obj_RSSStory(); tmpItem.setTitle(tmpJson.getString("title")); tmpItem.setLink(tmpJson.getString("link")); tmpItem.setDescription(tmpJson.getString("description")); menuItems.add(i, tmpItem); }//end for //setup data adapter int resID = R.layout.list_rss_style_1; menuItemAdapter = new Adapter_RSSStory(this, resID, menuItems); myListView.setAdapter(menuItemAdapter); //setup list click listener final OnItemClickListener listItemClickHandler = new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id){ //clicked item Obj_RSSStory tmpStory = (Obj_RSSStory) menuItems.get(position); if(tmpStory.getLink().length() > 1){ //create an Obj_Screen from RSSStory data.. Obj_Screen tmpItem = new Obj_Screen("0000", "0000"); tmpItem.setScreenType("screen_url"); tmpItem.setScreenTitle(tmpStory.title); //create JSON from RSSStory data String tmpKeys = "{"; tmpKeys += "\"title\":\"" + tmpStory.title + "\","; tmpKeys += "\"url\":\"" + tmpStory.link + "\""; tmpKeys += "}"; tmpItem.setJsonScreenOptions(tmpKeys); //remember selected index to select this row on resume... selectedIndex = position; //fire menuClick (method in Act_ActivityBase) to launch Screen_CustomURL menuTap(tmpItem); }else{ showAlert("No URL?", "This RSS Story does not have a URL associated with it?"); } } }; myListView.setOnItemClickListener(listItemClickHandler); }catch (Exception je){ //showAlert("Data Format Error", "There was a problem reading data associated with this app."); } //hide progress hideProgress(); } ///////////////////////////////////////////////////// //done downloading data.. Handler downloadTextHandler = new Handler(){ @Override public void handleMessage(Message msg){ if(JSONData.length() < 1){ hideProgress(); showAlert("Error Downloading", "Please check your internet connection then try again."); }else{ parseData(JSONData); } } }; //download class in new thread / class public class DownloadThread extends Thread{ boolean threadRunning = false; String downloadURL = ""; String saveAsFileName = ""; String downloadType = ""; void setThreadRunning(boolean bolRunning){ threadRunning = bolRunning; } void setDownloadURL(String theURL){ downloadURL = theURL; } void setSaveAsFileName(String theFileName){ saveAsFileName = theFileName; } void setDownloadType(String imageOrText){ downloadType = imageOrText; } @Override public void run(){ if(downloadType == "text"){ JSONData = appDelegate.downloadText(downloadURL); appDelegate.saveText(saveAsFileName, JSONData); downloadTextHandler.sendMessage(downloadTextHandler.obtainMessage()); this.setThreadRunning(false); } } } ///////////////////////////////////////////////////// //options menu @Override public boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); //set up dialog final Dialog dialog = new Dialog(this); dialog.setContentView(R.layout.menu_refresh); dialog.setTitle("Screen Options"); //refreshAppData (if we are on home screen) if(this.screenData.isHomeScreen() && knightspolo_appDelegate.rootApp.getDataURL().length() > 1){ final Button btnRefreshAppData = new Button(this); btnRefreshAppData.setText(getString(R.string.refreshAppData)); btnRefreshAppData.setOnClickListener(new OnClickListener(){ public void onClick(View v){ dialog.cancel(); refreshAppData(); } }); options.add(btnRefreshAppData); } //add each option to layout, set layoutParams as we go... for(int x = 0; x < options.size(); x++){ Button btn = (Button)options.get(x); btn.setTextSize(18); btn.setLayoutParams(btnLayoutParams); btn.setPadding(5, 5, 5, 5); optionsView.addView(btn); } //set content view.. dialog.setContentView(optionsView); if(options.size() > 1){ dialog.setTitle(getString(R.string.menuOptions)); }else{ dialog.setTitle(getString(R.string.menuNoOptions)); } //show dialog.show(); return true; } ///////////////////////////////////////////////////// } Thanks for all of your input on how to make this run. Here is the code: http://bit.ly/HtnliU Jake
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.