0

im using sax parser,asynctask.eclipse juno.

here is where im stucked at:

i already parsed some tags and are appearing on my ListView, when the user tap/click a row of the lisview it should open a new intent,it is open the new layout but is blank, well, the title,author,guid tags are appearing on the list view and i want them to appear too when the new layout/class is open, and also i want to display the description :

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
    long id) {

    Intent intent=new Intent(SAXParserAsyncTaskActivity.this,MessageActivity.class);

    startActivity(intent);


}

i know i need to put some code on the code above to display the tags that i already have on the Listview,.thats the thing i don't know how to do that !

here is the code, this is code as is,its open with the button the list of messages and when i click on message it just opens a new layout in blank,i want to pass the info from the list view to this layout.

this is the SAXParserAsynctaskActivity(main activity):

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;



import com.theopentutorials.android.adapters.CustomListViewAdapter;
import com.theopentutorials.android.beans.Laptop;
 import com.theopentutorials.android.xml.sax.SAXXMLParser;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

public class SAXParserAsyncTaskActivity extends Activity implements
    OnClickListener, OnItemClickListener {
Button button;
ListView listView;
List<Laptop> laptops;
CustomListViewAdapter listViewAdapter;

static final String URL = "http://www.revgrades.com/laptops.xml";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    findViewsById();
    button.setOnClickListener(this);
    listView.setOnItemClickListener(this);
}

private void findViewsById() {
    button = (Button) findViewById(R.id.button);
    listView = (ListView) findViewById(R.id.laptopList);
}


@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
    long id) {


    Intent intent=new Intent(SAXParserAsyncTaskActivity.this,MessageActivity.class);

    startActivity(intent);


}

@Override
public void onClick(View view) {
    GetXMLTask task = new GetXMLTask(this);
    task.execute(new String[] { URL });
}

//private inner class extending AsyncTask
private class GetXMLTask extends AsyncTask<String, Void, List<Laptop>> {
    private Activity context;
    public GetXMLTask(Activity context) {
        this.context = context;
    }

    @Override
    protected void onPostExecute(List<Laptop> laptops) {
        listViewAdapter = new CustomListViewAdapter(context, laptops);
        listView.setAdapter(listViewAdapter);
    }

    /* uses HttpURLConnection to make Http request from Android to download
     the XML file */
    private String getXmlFromUrl(String urlString) {
        StringBuffer output = new StringBuffer("");
        try {
            InputStream stream = null;
            URL url = new URL(urlString);
            URLConnection connection = url.openConnection();

            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setRequestMethod("GET");
            httpConnection.connect();

            if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                stream = httpConnection.getInputStream();

                BufferedReader buffer = new BufferedReader(
                        new InputStreamReader(stream));
                String s = "";
                while ((s = buffer.readLine()) != null)
                    output.append(s);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return output.toString();
}  

    @Override
    protected List<Laptop> doInBackground(String... urls) {
        List<Laptop> laptops = null;
        String xml = null;
        for (String url : urls) {
            xml = getXmlFromUrl(url);

            InputStream stream = new ByteArrayInputStream(xml.getBytes());
            laptops = SAXXMLParser.parse(stream);

            for (Laptop laptop : laptops) {
                String imageURL = laptop.getImageURL();
                Bitmap bitmap = null;
                BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                bmOptions.inSampleSize = 1;

                try {
                    bitmap = BitmapFactory
                            .decodeStream(new
                            URL(imageURL).openStream(),
                            null, bmOptions);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                laptop.setImageBitmap(bitmap);
            }
        }
        // stream.close();
        return laptops;
    }
}
}
Chapo
  • 31
  • 1
  • 7
  • I've already tried this answer(http://stackoverflow.com/questions/15173134/pass-the-value-from-1st-activity-to-2nd-activity-in-android?rq=1 ),but its not working,any more ideas? – Chapo Jan 29 '14 at 20:11

1 Answers1

0

Create a Bundle object. Add your data as fields to the Bundle object using the put methods of Bundle. Then use intent.putExtras(bundle) to attach the Bundle to your Intent.

In the SAXParserAsyncTaskActivity, use Activity.getIntent() to get a reference to the Intent and then Intent.getExtras() to get a reference to the Bundle object you passed. You then use the Bundle getter methods to extract your data.

David C Adams
  • 1,953
  • 12
  • 12
  • can you write me some code,or some link or any kind of illustration ? – Chapo Jan 29 '14 at 21:22
  • Bundle extras = new Bundle(); extras.putString("title", titleString); extras.putString("author", authorString); Intent intent=new Intent(SAXParserAsyncTaskActivity.this,MessageActivity.class); intent.putExtras(extras); startActivity(intent); In your activity (onCreate() or wherever): Bundle extras = this.getIntent().getExtras(); String titleString = extras.getString("title"); String authorString = extras.getString("author"); – David C Adams Jan 29 '14 at 21:39
  • Did you replace the example variables with your own data? Or did you just cut and paste? – David C Adams Jan 29 '14 at 22:31
  • yeah i did replace the string names. – Chapo Jan 29 '14 at 22:57
  • David i already update the question with code,when i put the code you gave me i put it like this : – Chapo Jan 29 '14 at 23:15
  • Bundle extras = this.getIntent().getExtras(); String brand = extras.getString("brand"); String model = extras.getString("model"); @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { Bundle extras = new Bundle(); extras.putString("brand", brand); extras.putString("model", model); Intent intent=new Intent(SAXParserAsyncTaskActivity.this,MessageActivity.class); intent.putExtras(extras); startActivity(intent); } – Chapo Jan 29 '14 at 23:16