XML解析图像OnClick

时间:2012-06-21 08:52:42

标签: android xml parsing imageview

嗨我已经解析了一个包含图片的在线xml,如果有人知道答案你能帮忙解决,我会一直解析图片吗?

public class SingleMenuItem  extends Activity {

// XML node keys
static final String KEY_SONG = "song";
static final String KEY_ARTIST = "artist";
static final String KEY_THUMB_URL = "thumb_url";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.libraryonclick);

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

    // Get XML values from previous intent
    String song = in.getStringExtra(KEY_SONG);
    String artist = in.getStringExtra(KEY_ARTIST);
    final String thumb_url = in.getStringExtra(KEY_THUMB_URL);

    // Displaying all values on the screen
    TextView lblSong = (TextView) findViewById(R.id.textView1);
    TextView lblArtist = (TextView) findViewById(R.id.textView2);
    ImageView lblThumb_Url = (ImageView) findViewById(R.id.onclickthumb);


    Button link3Btn = (Button)findViewById( R.id.button1 );
    link3Btn.setOnClickListener( new View.OnClickListener()
    {
    public void onClick(View v)
    {
        Uri uri = Uri.parse(thumb_url);
        startActivity( new Intent( Intent.ACTION_VIEW, uri ) );
    }
    });

    lblSong.setText(song);
    lblArtist.setText(artist);
    lblThumb_Url.setSrc(thumb_url);

谢谢你知道它与上面开始编码的行有关但是卡住了

Main Activity
public class CustomizedListView extends Activity {
// All static variables
static final String URL = "http://dl.dropbox.com/u/48258247/music.xml";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";

ListView list;
LazyAdapter adapter;

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


    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() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String song = ((TextView) view.findViewById(R.id.title)).getText().toString();
            String artist = ((TextView) view.findViewById(R.id.artist)).getText().toString();
            String thumb_url = ((TextView) view.findViewById(R.id.duration)).getText().toString();

            // Starting new intent
            Intent in = new Intent(CustomizedListView.this, com.example.androidhive.SingleMenuItem.class);
            in.putExtra(KEY_SONG, song);
            in.putExtra(KEY_ARTIST, artist);
            in.putExtra(KEY_THUMB_URL, thumb_url);
            startActivity(in);

        }
    });     
}   

1 个答案:

答案 0 :(得分:0)

为什么在on create方法中使用按钮功能的代码?

 public void onClick(View v)
 {
     Uri uri = Uri.parse(thumb_url);
     startActivity( new Intent( Intent.ACTION_VIEW, uri ) );
 }

我感觉这个展示位置超出了实际的onclick事件的范围。首先从onCreate方法转移on click函数,这样你的程序实际上可以在onClick调用上执行某些操作。

你是从网上解析当地的uris还是uris?您无法直接从uris访问本地资源,您必须使用内容提供商。这可能也是问题所在,但如果没有看到更多代码就无法说出来。