• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps imageview xml parser error

krishnaveni

Well-Known Member
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.
 
ya sure...
this is my coding part.
CustomizedListView.java

Code:
package com.example.truebranches;

import java.sql.Blob;
import java.util.ArrayList;
import java.util.HashMap;

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

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class CustomizedListView extends Activity {
    // All static variables
    static final String URL = "http://192.168.1.213/tbc/product_feed.xml";
    // XML node keys
    static final String KEY_SONG = "Product"; // parent node
    static final String KEY_ID = "Productid";
    static final String KEY_TITLE = "Name";
    static final String KEY_ARTIST = "Category";
    static final String KEY_DURATION = "Price";
    static final String KEY_THUMB_URL = "Image";
    
    ListView list;
    LazyAdapter adapter;

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

        ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element
        
        NodeList nl = doc.getElementsByTagName(KEY_SONG);
        // looping through all song nodes <song>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
            map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
            map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
            map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

            // adding HashList to ArrayList
            songsList.add(map);
        }
        

        list=(ListView)findViewById(R.id.list);
        
        // Getting adapter by passing xml data ArrayList
        adapter=new LazyAdapter(this, songsList);        
        list.setAdapter(adapter);
        

        // Click event for single list row
        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();
                
[COLOR=Red]                             Blob Image=((ImageView)view.findViewById(R.id.list_image)).getImageResource();
[/COLOR]
                  
                // Starting new intent
                Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
                in.putExtra(KEY_TITLE, Name);
                in.putExtra(KEY_ARTIST, Category);
               [COLOR=Red] in.putExtra(KEY_THUMB_URL, Image);[/COLOR]
                in.putExtra(KEY_DURATION, Price);
                                startActivity(in);
                

            }
        });        
    }    
}
Then my next activity is SingleMenuItemActivity.java file is below.

Code:
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();
        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);
       [COLOR=Red] lblImage.setImageResource(Image);[/COLOR]
        lblCost.setText(Price);
        
    }
}
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.
 
the program is having error...
In CustomizedListView,
Code:
                    Byte[] Image=((ImageView)view.findViewById(R.id.list_image)).getImageMatrix().toBitmap();
the above line having dis error.

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


In SingleMenuItem,
Code:
        lblImage.setImageResource(Image);
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.
 
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:
Drawable d = ((ImageView)view.findViewById(R.id.list_image)).getDrawable();

if (d instanceof BitmapDrawable) {
        Bitmap bm = ((BitmapDrawable)d).getBitmap();
        //Maybe more code here?
        lblImage.setImageBitmap(bm);
}

You need to pay attention to the API documentation and what can be passed to what methods.
 
Back
Top Bottom