1. Download our Official Android App: Forums for Android!

Apps imageview xml parser error

Discussion in 'Android Development' started by krishnaveni, May 11, 2012.

  1. krishnaveni

    krishnaveni Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    55
    Posts:
    158
    Joined:
    Dec 16, 2011

    Dec 16, 2011
    158
    0
    55
    chennai
    Hi am developed XMLParsing android application.Here i followed dis tutorials Android Custom ListView with Image and Text.
    its successfully worked for me.But hear am implementing dis activity to one of my project. It works great but i am having trouble to intent it to another activity.
    list.setOnItemClickListener(new OnItemClickListener() { @Override
    public void onItemClick(AdapterView parent, View view,
    int position, long id) {
    String Name = ((TextView) view.findViewById(R.id.title)).getText().toString();
    String Category = ((TextView) view.findViewById(R.id.artist)).getText().toString();
    String Price = ((TextView) view.findViewById(R.id.duration)).getText().toString();
    String Image=((ImageView)view.findViewById(R.id.list_image)).getImageMatrix().toString();

    // Starting new intent
    Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
    in.putExtra(KEY_TITLE, Name);
    in.putExtra(KEY_ARTIST, Category);
    in.putExtra(KEY_THUMB_URL, Image);
    in.putExtra(KEY_DURATION, Price);
    startActivity(in);
    the next activity purpose is u r clicking one particular product from list of products that time the next activity have to displayed that particular product details.
    the nextactivity is SingleMenuLietItem.java source code is
    package com.example.truebranches;
    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Matrix;
    import android.os.Bundle;
    import android.widget.ImageView;
    import android.widget.TextView;
    public class SingleMenuItemActivity extends Activity {

    // XML node keys
    static final String KEY_TITLE = "Name";
    static final String KEY_ARTIST = "Category";
    static final String KEY_THUMB_URL = "Image";
    static final String KEY_DURATION = "Price";

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.single_list_item);

    // getting intent data
    Intent in = getIntent();

    // Get XML values from previous intent
    String Name = in.getStringExtra(KEY_TITLE);
    String ProductURL = in.getStringExtra(KEY_ARTIST);
    String Image = in.getStringExtra(KEY_THUMB_URL);
    String Price = in.getStringExtra(KEY_DURATION);


    // Displaying all values on the screen
    TextView lblName = (TextView) findViewById(R.id.name_label);
    TextView lblProductURL = (TextView) findViewById(R.id.producturl_label);
    ImageView lblImage = (ImageView) findViewById(R.id.image_label);
    TextView lblCost = (TextView) findViewById(R.id.price_label);

    lblName.setText(Name);
    lblProductURL.setText(ProductURL);
    lblImage.setImageResource(Image);
    lblCost.setText(Price);

    }
    }
    Here the next activity have to displayed textview product details..but not displayed image..because i am struggling in dis part...so which line i change it here for displayed image on next activity.please help me.
     

    Advertisement

  2. jonbonazza

    jonbonazza Android Expert
    Rank:
    None
    Points:
    163
    Posts:
    1,930
    Joined:
    Jul 13, 2010

    Jul 13, 2010
    1,930
    457
    163
    Male
    Hello,
    Can you please place your code inside of
    Code (Text):
    1.  tags?
     
  3. krishnaveni

    krishnaveni Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    55
    Posts:
    158
    Joined:
    Dec 16, 2011

    Dec 16, 2011
    158
    0
    55
    chennai
    ya sure...
    this is my coding part.
    CustomizedListView.java

    Code (Text):
    1.  
    2. package com.example.truebranches;
    3.  
    4. import java.sql.Blob;
    5. import java.util.ArrayList;
    6. import java.util.HashMap;
    7.  
    8. import org.w3c.dom.Document;
    9. import org.w3c.dom.Element;
    10. import org.w3c.dom.NodeList;
    11.  
    12. import android.app.Activity;
    13. import android.content.Intent;
    14. import android.os.Bundle;
    15. import android.view.View;
    16. import android.widget.AdapterView;
    17. import android.widget.ImageView;
    18. import android.widget.TextView;
    19. import android.widget.AdapterView.OnItemClickListener;
    20. import android.widget.ListView;
    21.  
    22. public class CustomizedListView extends Activity {
    23.     // All static variables
    24.     static final String URL = "http://192.168.1.213/tbc/product_feed.xml";
    25.     // XML node keys
    26.     static final String KEY_SONG = "Product"; // parent node
    27.     static final String KEY_ID = "Productid";
    28.     static final String KEY_TITLE = "Name";
    29.     static final String KEY_ARTIST = "Category";
    30.     static final String KEY_DURATION = "Price";
    31.     static final String KEY_THUMB_URL = "Image";
    32.    
    33.     ListView list;
    34.     LazyAdapter adapter;
    35.  
    36.     @Override
    37.     public void onCreate(Bundle savedInstanceState) {
    38.         super.onCreate(savedInstanceState);
    39.         setContentView(R.layout.main);
    40.        
    41.  
    42.         ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
    43.  
    44.         XMLParser parser = new XMLParser();
    45.         String xml = parser.getXmlFromUrl(URL); // getting XML from URL
    46.         Document doc = parser.getDomElement(xml); // getting DOM element
    47.        
    48.         NodeList nl = doc.getElementsByTagName(KEY_SONG);
    49.         // looping through all song nodes <song>
    50.         for (int i = 0; i < nl.getLength(); i++) {
    51.             // creating new HashMap
    52.             HashMap<String, String> map = new HashMap<String, String>();
    53.             Element e = (Element) nl.item(i);
    54.             // adding each child node to HashMap key => value
    55.             map.put(KEY_ID, parser.getValue(e, KEY_ID));
    56.             map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
    57.             map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
    58.             map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
    59.             map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
    60.  
    61.             // adding HashList to ArrayList
    62.             songsList.add(map);
    63.         }
    64.        
    65.  
    66.         list=(ListView)findViewById(R.id.list);
    67.        
    68.         // Getting adapter by passing xml data ArrayList
    69.         adapter=new LazyAdapter(this, songsList);        
    70.         list.setAdapter(adapter);
    71.        
    72.  
    73.         // Click event for single list row
    74.         list.setOnItemClickListener(new OnItemClickListener() {
    75.  
    76.             @Override
    77.             public void onItemClick(AdapterView<?> parent, View view,
    78.                     int position, long id) {
    79.                 String Name = ((TextView) view.findViewById(R.id.title)).getText().toString();
    80.                 String Category = ((TextView) view.findViewById(R.id.artist)).getText().toString();
    81.                 String Price = ((TextView) view.findViewById(R.id.duration)).getText().toString();
    82.                
    83. [COLOR=Red]                             Blob Image=((ImageView)view.findViewById(R.id.list_image)).getImageResource();
    84. [/COLOR]
    85.                  
    86.                 // Starting new intent
    87.                 Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
    88.                 in.putExtra(KEY_TITLE, Name);
    89.                 in.putExtra(KEY_ARTIST, Category);
    90.                [COLOR=Red] in.putExtra(KEY_THUMB_URL, Image);[/COLOR]
    91.                 in.putExtra(KEY_DURATION, Price);
    92.                                 startActivity(in);
    93.                
    94.  
    95.             }
    96.         });        
    97.     }    
    98. }
    Then my next activity is SingleMenuItemActivity.java file is below.

    Code (Text):
    1.  
    2. package com.example.truebranches;
    3.  
    4. import android.app.Activity;
    5. import android.content.Intent;
    6. import android.graphics.Matrix;
    7. import android.os.Bundle;
    8. import android.widget.ImageView;
    9. import android.widget.TextView;
    10.  
    11. public class SingleMenuItemActivity  extends Activity {
    12.    
    13.     // XML node keys
    14.     static final String KEY_TITLE = "Name";
    15.     static final String KEY_ARTIST = "Category";
    16.     static final String KEY_THUMB_URL = "Image";
    17.     static final String KEY_DURATION = "Price";
    18.    
    19.     @Override
    20.     public void onCreate(Bundle savedInstanceState) {
    21.         super.onCreate(savedInstanceState);
    22.         setContentView(R.layout.single_list_item);
    23.        
    24.         // getting intent data
    25.         Intent in = getIntent();
    26.         String Name = in.getStringExtra(KEY_TITLE);
    27.         String ProductURL = in.getStringExtra(KEY_ARTIST);
    28.         String Image = in.getStringExtra(KEY_THUMB_URL);
    29.         String Price = in.getStringExtra(KEY_DURATION);
    30.        
    31.        
    32.         // Displaying all values on the screen
    33.         TextView lblName = (TextView) findViewById(R.id.name_label);
    34.         TextView lblProductURL = (TextView) findViewById(R.id.producturl_label);
    35.         ImageView lblImage = (ImageView) findViewById(R.id.image_label);
    36.         TextView lblCost = (TextView) findViewById(R.id.price_label);
    37.              
    38.         lblName.setText(Name);
    39.         lblProductURL.setText(ProductURL);
    40.        [COLOR=Red] lblImage.setImageResource(Image);[/COLOR]
    41.         lblCost.setText(Price);
    42.        
    43.     }
    44. }
    45.  
    Here the textview parts all are displayed in the next activity.But unable to display imageview.Red color line all are getting error.how it is cleared..i don't know the method for imageview.please help me yar.
     
  4. jonbonazza

    jonbonazza Android Expert
    Rank:
    None
    Points:
    163
    Posts:
    1,930
    Joined:
    Jul 13, 2010

    Jul 13, 2010
    1,930
    457
    163
    Male
    What is Log Cat saying the error is? Can you post a copy of the log cat output?
     
  5. krishnaveni

    krishnaveni Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    55
    Posts:
    158
    Joined:
    Dec 16, 2011

    Dec 16, 2011
    158
    0
    55
    chennai
    the program is having error...
    In CustomizedListView,
    Code (Text):
    1.    
    2.                     Byte[] Image=((ImageView)view.findViewById(R.id.list_image)).getImageMatrix().toBitmap();
    3.  
    the above line having dis error.

    The method toBitmap() is undefined for the type Matrix.


    In SingleMenuItem,
    Code (Text):
    1.         lblImage.setImageResource(Image);
    2.  
    the error is:
    The method setImageResource(int) in the type ImageView is not applicable for the arguments (String).

    How it is cleared...whaen i cleared above 2 error means that time only am going to run the application.so please guide me.
     
  6. jonbonazza

    jonbonazza Android Expert
    Rank:
    None
    Points:
    163
    Posts:
    1,930
    Joined:
    Jul 13, 2010

    Jul 13, 2010
    1,930
    457
    163
    Male
    1) If you look here: Matrix | Android Developers You will see that the Matrix class does not have a toBitmap() method. You could do something like this instead:
    Code (Text):
    1.  
    2. Drawable d = ((ImageView)view.findViewById(R.id.list_image)).getDrawable();
    3.  
    4. if (d instanceof BitmapDrawable) {
    5.         Bitmap bm = ((BitmapDrawable)d).getBitmap();
    6.         //Maybe more code here?
    7.         lblImage.setImageBitmap(bm);
    8. }
    9.  
    You need to pay attention to the API documentation and what can be passed to what methods.
     

Share This Page

Loading...