gpt4 book ai didi

android - ListView中的隐藏字段传递一个变量

转载 作者:行者123 更新时间:2023-11-29 00:48:59 27 4
gpt4 key购买 nike

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

最佳答案

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

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();
}
}
}

关于android - ListView中的隐藏字段传递一个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4597497/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com