简体   繁体   English

如何从xml解析器获取值到另一个类

[英]how to get the value from a xml parser to an other class

I'm searching all over the internet, but I didn't find anything, I could use. 我正在整个Internet上搜索,但没有找到任何可以使用的东西。 So here is my problem: I have an xml parser that works in an list view, but I want to give the id of the song to an other activity , so I can display it. 所以这是我的问题:我有一个可以在列表视图中使用的xml解析器,但是我想将歌曲的ID提供给其他活动,以便可以显示它。

您可以通过使用setTag()getTag()方法来查看我的答案, 如何从onItemClick访问arrayadapter getView中的变量

1. First you need OnItemClickListener() method for ListView 1.首先,需要ListView OnItemClickListener()方法

2. Then once you capture the index of the row on which you clicked, use Intent() to pass it to another activity. 2.然后,一旦捕获了单击index of the rowindex of the row ,请使用Intent()将其传递给另一个活动。

Note: 注意:

For this to work, please make the Map as static, and please do map.clear() , everytime before loading the Map with parsed data.

3. When you go to the another Activity , I hope the Order of your Song in Map and in the ListView are same, 3.当您转到另一个Activity ,希望您的歌曲在MapListView中的顺序相同,

So Suppose you pressed the 2nd item on the ListView , the index passed will be 1, and in other Activity just call the Map's 1st index. 因此,假设您在ListView上按下了第二项,则传递的索引将为1,而在其他Activity只需调用Map's第一项索引即可。

Note: 注意:

Adding header will effect the index of ListView so its better to use getItemIdAtPosition(arg) in the onClick() method. 添加标头将影响 ListView 的索引 ,因此最好在onClick()方法中使用getItemIdAtPosition(arg)

Eg: 例如:

ListView lv = (ListView)findViewById(R.id.MyListView);

lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                i = new Intent(ProductActivity.this, ProductDetails.class);


                i.putExtra("keyAapo", (int) lv.getItemIdAtPosition(arg2));

                startActivity(i);

            }

        });

The Intent extra passing option was already said, however, if you are getting to the point where you are passing a bunch of data, you can always make the XML processor reusable and just pass the XML. 前面已经提到了Intent额外传递选项,但是,如果要传递大量数据,始终可以使XML处理器可重用,而只需传递XML。 It's already an encoded format 已经是编码格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM