[Java for Android App Development] SpaceCaker:
Hello everyone.

I am trying to load the Images in an listview that the RSS provides me.

http://www.nutech.nl/rss is the rss i use.

i want to load the image in listview from these tags

<enclosure> </enclosure (

<enclosure url="http://media.nu.nl/m/m1nx89taa599_sqr256.jpg" length="None" type="image/jpeg"></enclosure>

)

Now i have tried several things i found on google but none worked or werent explaining enough.

Here is some of my main Codes

RSSreader.java

package com.thedutch.technews;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.os.StrictMode;

public class RSSReader {
DefaultHttpClient httpClient = new DefaultHttpClient();

public Document getRSSFromServer(String url) {
Document response = null;
response = getDomFromXMLString(getFeedFromServer(url));
return response;
}

private String getFeedFromServer(String url) {
String xml = null;
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
try {
HttpGet httpget =new HttpGet(url);
//HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpget);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (Exception e) {
e.printStackTrace();
}
return xml;
}

private Document getDomFromXMLString(String xml) {
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);

} catch (Exception e) {
}
return doc;
}

public String getValue(Element item, String key) {
NodeList nodeList = item.getElementsByTagName(key);
return this.getElementValue(nodeList.item(0));
}

public final String getElementValue(Node node) {
Node child;
if (node != null) {
if (node.hasChildNodes()) {
for (child = node.getFirstChild(); child != null; child = child
.getNextSibling()) {
if (child.getNodeType() == Node.TEXT_NODE) {
return child.getNodeValue();
}
}
}
}
return "";
}
}

Nutech.java

package com.thedutch.technews;

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

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.faizmalkani.floatingactionbutton.FloatingActionButton;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Nutech extends Fragment implements OnClickListener{

String key_items = "item";
String key_title = "title";
String key_description = "description";
String key_link = "link";
String key_date = "pubDate";
ListView lstPost = null;
List<HashMap<String, Object>> post_lists = new ArrayList<HashMap<String, Object>>();
List<String> lists = new ArrayList<String>();
ArrayAdapter<String> adapter23 = null;
RSSReader rssfeed = new RSSReader();
FloatingActionButton mFab;

public static Fragment newInstance(Context context) {
Nutech f = new Nutech();

return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(
R.layout.activity_main4, container, false);
((MainActivity) getActivity())
.setActionBarTitle("Nutech");
mFab = (FloatingActionButton) view.findViewById(R.id.fabbutton);
mFab.setOnClickListener(this);
return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Appstatus.getInstance(getActivity()).isOnline()) {
lstPost = (ListView) getView().findViewById(R.id.lstPosts);
mFab = (FloatingActionButton) getView().findViewById(R.id.fabbutton);
mFab.listenTo(lstPost);
adapter23 = new ArrayAdapter<String>(getActivity(),
R.layout.feed_list_item, R.id.title, lists) {

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView txt1 = (TextView) view
.findViewById(R.id.title);
TextView txt2 = (TextView) view
.findViewById(R.id.desc);
TextView txt3 = (TextView) view
.findViewById(R.id.date);
HashMap<String, Object> data = post_lists.get(position);
txt1.setText(data.get(key_title).toString());
txt2.setText(data.get(key_description).toString());
txt3.setText(data.get(key_date).toString());
return view;
}
};
Document xmlFeed = rssfeed
.getRSSFromServer("http://www.nutech.nl/rss");
NodeList nodes = xmlFeed.getElementsByTagName("item");
for (int i = 0; i < nodes.getLength(); i++) {
Element item = (Element) nodes.item(i);
HashMap<String, Object> feed = new HashMap<String, Object>();
feed.put(key_title, rssfeed.getValue(item, key_title));
feed.put(key_description, rssfeed.getValue(item, key_description));
feed.put(key_link, rssfeed.getValue(item, key_link));
feed.put(key_date, rssfeed.getValue(item, key_date));
post_lists.add(feed);
lists.add(feed.get(key_title).toString());
}
lstPost.setAdapter(adapter23);
lstPost.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
if (Appstatus.getInstance(getActivity()).isOnline()) {
Document xmlFeed = rssfeed
.getRSSFromServer("http://www.nutech.nl/rss");
NodeList nodes = xmlFeed.getElementsByTagName("item");
Element item = (Element) nodes.item(position);
Intent indisplay = new Intent(getActivity(),PostViewActivity.class);
indisplay.putExtra("link", rssfeed.getValue(item, key_link));
startActivity(indisplay);
} else {
getActivity().setContentView(R.layout.activity_main3);
Thread background = new Thread() {
public void run() {

try {
sleep(5*1100);
getActivity().finish();

} catch (Exception e) {

}
}
};
background.start();
}
}
});
lstPost.setLongClickable(true);
lstPost.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long id) {
if (Appstatus.getInstance(getActivity()).isOnline()) {
Document xmlFeed = rssfeed
.getRSSFromServer("http://www.nutech.nl/rss");
NodeList nodes = xmlFeed.getElementsByTagName("item");
Element item = (Element) nodes.item(position);
final Intent wintent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(rssfeed.getValue(item, key_link)));
startActivity(wintent);
return true;
} else {
getActivity().setContentView(R.layout.activity_main3);
Thread background = new Thread() {
public void run() {

try {
sleep(5*1100);
getActivity().finish();

} catch (Exception e) {

}
}
};
background.start();
}
return true;
}
});
} else {
getActivity().setContentView(R.layout.activity_main3);
Thread background = new Thread() {
public void run() {

try {
sleep(5*1100);
getActivity().finish();

} catch (Exception e) {

}
}
};
background.start();
}
}

public void fabClicked(View view) {
adapter23.notifyDataSetChanged();
}

@Override
public void onClick(View v) {
adapter23.notifyDataSetChanged();
this.adapter23.notifyDataSetChanged();
lstPost.invalidateViews();
lstPost.refreshDrawableState();
Toast.makeText(this.getActivity(),
"Refreshed Nutech News!", Toast.LENGTH_LONG).show();
}

public void hideFab(View view) {
mFab.hide(true);
//getActionBar().hide();
}

public void showFab(View view) {
mFab.hide(false);
//getActionBar().show();
}
}

MainActivity.java

package com.thedutch.technews;

import java.util.ArrayList;

import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.MapBuilder;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
private EasyTracker easyTracker = null;
private Toolbar mToolbar;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private ListView mDrawerList;
private ArrayList<ListMenuModel> mListMenu;
private ListMenuAdapter mListMenuAdapter;
final String[] data ={"Nutech","Tweakers","Hardware Info"};
final String[] fragmentos ={
"com.thedutch.technews.Nutech",
"com.thedutch.technews.Tweakers",
"com.thedutch.technews.Hardwareinfo"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
easyTracker = EasyTracker.getInstance(MainActivity.this);
mToolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(mToolbar);

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mListMenu = new ArrayList<ListMenuModel>();
mListMenu.add(new ListMenuModel("Nutech", "Nutech", R.drawable.ic_nu));
mListMenu.add(new ListMenuModel("Tweakers", "Tweakers", R.drawable.ic_tweakers));
mListMenu.add(new ListMenuModel("Hardware Info", "Hardware Info", R.drawable.ic_hardwareinfo));
mListMenuAdapter = new ListMenuAdapter(getApplicationContext(),
mListMenu);
mDrawerList.setAdapter(mListMenuAdapter);
mDrawerList.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
mDrawerToggle.syncState();
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.content_frame, Fragment.instantiate(MainActivity.this, fragmentos[pos]));
tx.commit();
mDrawerList.setSelection(pos);
easyTracker.send(MapBuilder.createEvent("Nav Drawer",
"Opened Navigation Drawer", "Navigation Drawer", null).build());
mDrawerLayout.closeDrawer(mDrawerList);
}
});
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.content_frame,Fragment.instantiate(MainActivity.this, fragmentos[0]));
tx.commit();
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
mToolbar,
R.string.drawer_open,
R.string.drawer_close){

public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}

/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
syncState();
}
};

// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
mToolbar.setClickable(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}

public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.about:
Intent intent = new Intent(this, AboutActivity.class);
this.startActivity(intent);
easyTracker.send(MapBuilder.createEvent("AboutActivity",
"Entered About Page", "about", null).build());
break;
default:
return super.onOptionsItemSelected(item);
}

return true;
}

public void setActionBarTitle(String title) {
getSupportActionBar().setTitle(title);
}

@Override
public void onBackPressed() {
if(mDrawerLayout.isDrawerOpen(Gravity.START|Gravity.LEFT)){
mDrawerLayout.closeDrawers();
return;
}
super.onBackPressed();
}

public void fabClicked(View view) {
Toast.makeText(this, "Refreshed.", Toast.LENGTH_LONG).show();
}

@Override
public void onStart() {
super.onStart();
EasyTracker.getInstance(this).activityStart(this);
}

@Override
public void onStop() {
super.onStop();
EasyTracker.getInstance(this).activityStop(this);
}
}

is there someone who could help me ?

thank you :)