I need to show in a listview content of a local xml file, which contains item and subitem, can anyone cite an example of how to do? I found examples that take a xml file from a URL, but want to do it locally. Thanks!
try it .it will help you.
private void xmlPullParser()
{
XmlPullParser xpp = getResources().getXml(R.xml.words);
try {
while (xpp.getEventType()!=XmlPullParser.END_DOCUMENT)
{
if (xpp.getEventType()==XmlPullParser.START_TAG)
{
if (xpp.getName().equals("word"))
items.add(xpp.getAttributeValue(0));
}
xpp.next();
}
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
make folder at res like
-
Related
I have implemented facebook sdk in my android application and I have spent past 3hrs in downloading profile picture from facebook and yet i'm not successful in downloading it.
This is my code.
try {
String user = Facebuk.fb.request("me");
jsonObject = Util.parseJson(user);
String id = jsonObject.optString("id");
name = jsonObject.optString("name");
URL img_url = new URL("http://graph.facebook.com/" + id
+ "/picture?type=large");
dp = BitmapFactory.decodeStream(img_url.openConnection().getInputStream());
if (dp == null)
Log.i("", "afafaffgwgwgwg");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FacebookError e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
i get notified from the logcat that BitmapFactory.decodestream returns null
There were similar posts asked in Stack Overflow and there solution didn't workout for me.. please help me out.
Try using AQuery library which provides callback in which u get bitmap.
My OutputStreamWrite refuses to append to my file. It only overwrites the first line constantly.
The String lightRowValues are sent from another method that goes through a table and gets one row at a time, sends that row data here, where that row is written to this file. Then the method loops back and gets the next row. SO I should have a list of rows but instead only have one line of the very last entry.
public static void appendToLtCSV(String lightRowValues, String CSVFinalFileName){
try {
csvfos = new FileOutputStream(CSVFinalFileName, true);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
OutputStreamWriter sensorCSVWriter = new OutputStreamWriter(csvfos);
try {
sensorCSVWriter.append(lightRowValues);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
sensorCSVWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I have set the append flag to true and still the same problem....
I'm using a custom adapter and wrapping it around the cwac-endless adapter. The problem is that the wrapping condition is being ignored and the method inside the cacheInBackground() is being called infinitely. I'm attaching the concerned code.Please suggest me a solution for this. Thank you.
#Override
protected boolean cacheInBackground() {
SystemClock.sleep(100); // pretend to do work
try {
msg=getMsgs();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("count", " "+ getWrappedAdapter().getCount());
return(getWrappedAdapter().getCount()<100);
}
#Override
protected void appendCachedData() {
if (getWrappedAdapter().getCount()<100) {
#SuppressWarnings("unchecked")
MsgAdapter a=(MsgAdapter)getWrappedAdapter();
for(String s:msg)
{
Log.d("msg", s);
}
}
}
}
I fixed the error it had to do with the logic of my getCount() in my custom adapter. Fixing it made the code work perfectly.
My app crashes when it tryes to load a file that isnt there. how do i stop it from loading the file when there isnt a file but when there is a file of that name it does load it.
Here is the code I am using to load the file. Any comments would be appreciated :)
Cheers
try {
fis = openFileInput(FILENAME1);
byte[] dataArray = new byte[fis.available()];
while (fis.read(dataArray) != -1){
task1 = new String(dataArray);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Try something like this:
fis = getContext().getFileStreamPath(FILENAME1);
if( fis.exists() ) {
...
}
else {
....
}
I have tried to find the answer online and none are clear to me. I am starting of programming and dont know much so any assistance that makes sense would help. My brother developed a website I'm trying to stream audio from and found on the developer web page a code to use. eclipse gives me no errors but when I run the app it forces close here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
String url = "http://beatswith.us/uploads/Mac%20Miller%20- %20Paper%20Route%20feat.%20Kev%20Da%20Hustla.mp3"; // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // might take long! (for buffering, etc)
mediaPlayer.start();
}
};
Its probably throwing an exception at mediaPlayer.start()
The url must be bad. looks to me like theres an actual space in your String, which is invalid. You should put setDataSource, prepare, and start in the same try/catch block
Edit:nevermind the url seems fine. Can you post the stacktrace of the exception?
Edit2: did you add your activity to the AndroidManifest.xml? Are you navigating here from another activity?
Found the Answer I had my button set up with the wrong package name