ListView中的隐藏字段传递变量

发布于 2024-10-10 04:58:51 字数 233 浏览 0 评论 0原文

我正在尝试构建我的第一个应用程序,它几乎从网上的 XML 文件创建一个列表。一旦我点击一个列表项,它就会打开另一个活动并访问另一个在线生成的 XML 文件,其中包含该项目的详细信息。 我设法使列表以及包含详细信息的第二个活动窗口正常工作。剩下要做的唯一一件事就是将 id 从一个活动传递到另一个活动。 有没有办法存储列表项中每个条目的ID?我没有使用本地 SQLite 数据库或任何东西,所以我需要找到一种方法来“即时”执行此操作(如果可能) 提前致谢

I'm trying to build my first application which pretty much creates a list from an XML file on the net. as soon as i click on a list item it should open another activity and access another online generated XML file with the details for the item.
ive managed to get the list working aswell as the 2nd activity window with the details. the only thing thats left to do is to pass the id from one activity to another.
is there a way to store the ID of each entry in the list items? im not using a local SQLite db or anything, so i need to find a way to do this "on the fly" if possible
thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷爱 2024-10-17 04:58:51

谢谢我让它工作了!
这是代码,以防万一有人遇到同样的问题:

package en.android.itleaked.com;

import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

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

public class getReleases extends Activity {
    public ListView lv1;
    private static final int ACTIVITY_CREATE = 0;

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

    }


    public void getFeed() {
        try {
            URL url = new URL("http://www.it-leaked.com/app/main.php");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();
            NodeList nodeList = doc.getElementsByTagName("item");
            String lv_arr[] = new String[nodeList.getLength()];
            final String lv_arr_id[] = new String[nodeList.getLength()];  
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                Element fstElmnt = (Element) node;
                NodeList nameList = fstElmnt.getElementsByTagName("title");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();
                lv_arr[i] = ((Node) nameList.item(0)).getNodeValue();

                NodeList idList = fstElmnt.getElementsByTagName("id");
                Element idElement = (Element) idList.item(0);
                idList = idElement.getChildNodes();
                lv_arr_id[i] = ((Node) idList.item(0)).getNodeValue();
            }

            lv1=(ListView)findViewById(R.id.ListView01);
            lv1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lv_arr));

            lv1.setTextFilterEnabled(true);
            lv1.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                            Intent i = new Intent(getReleases.this, showReleases.class);
                            i.putExtra("id", lv_arr_id[position]);
                            startActivityForResult(i, ACTIVITY_CREATE);
                    }
            });


        }

            catch (Exception e) {
                Toast.makeText(this, "Error: " + e, Toast.LENGTH_LONG).show();
            }
        }

}

这是接收端:

package en.android.itleaked.com;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class showReleases extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.releasedetails);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String releaseid = extras.getString("id");
            Toast.makeText(this, releaseid, Toast.LENGTH_LONG).show();
        }
    }
}

thanks i got it to work!!
here is the code just in case someone has the same problem:

package en.android.itleaked.com;

import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

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

public class getReleases extends Activity {
    public ListView lv1;
    private static final int ACTIVITY_CREATE = 0;

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

    }


    public void getFeed() {
        try {
            URL url = new URL("http://www.it-leaked.com/app/main.php");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();
            NodeList nodeList = doc.getElementsByTagName("item");
            String lv_arr[] = new String[nodeList.getLength()];
            final String lv_arr_id[] = new String[nodeList.getLength()];  
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                Element fstElmnt = (Element) node;
                NodeList nameList = fstElmnt.getElementsByTagName("title");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();
                lv_arr[i] = ((Node) nameList.item(0)).getNodeValue();

                NodeList idList = fstElmnt.getElementsByTagName("id");
                Element idElement = (Element) idList.item(0);
                idList = idElement.getChildNodes();
                lv_arr_id[i] = ((Node) idList.item(0)).getNodeValue();
            }

            lv1=(ListView)findViewById(R.id.ListView01);
            lv1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lv_arr));

            lv1.setTextFilterEnabled(true);
            lv1.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                            Intent i = new Intent(getReleases.this, showReleases.class);
                            i.putExtra("id", lv_arr_id[position]);
                            startActivityForResult(i, ACTIVITY_CREATE);
                    }
            });


        }

            catch (Exception e) {
                Toast.makeText(this, "Error: " + e, Toast.LENGTH_LONG).show();
            }
        }

}

And here is the receiving end:

package en.android.itleaked.com;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class showReleases extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.releasedetails);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String releaseid = extras.getString("id");
            Toast.makeText(this, releaseid, Toast.LENGTH_LONG).show();
        }
    }
}
风筝在阴天搁浅。 2024-10-17 04:58:51

您的列表应该存在于数据模型上,一旦用户单击一行,您应该将该位置映射到模型中的一个点。如果您想将其组合在一起,可以使用 setTag() 在视图上存储任何类型的对象。这不是该函数的传统用法。如果您发布如何创建列表视图,它可能会更有帮助。

查看代码后,您可能想要执行以下操作:

public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent i = new Intent(getReleases.this, showReleases.class);
i.putExtra("id", lv_arr[position]);
startActivityForResult(i, ACTIVITY_CREATE);
}

查看传递额外内容 关于如何使用 putExtra()。

Your list should live on a model for data, and once a user clicks a row you should be mapping that location to a point in your model. If you want to just hack this together you can use setTag() on views to store any type of object. This is not a traditional use of this function. If you post how you are creating the the listview it might be more helpful.

After looking at your code you will probably want to do something like this:

public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent i = new Intent(getReleases.this, showReleases.class);
i.putExtra("id", lv_arr[position]);
startActivityForResult(i, ACTIVITY_CREATE);
}

Check out Passing Extras on how to use putExtra().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文