gpt4 book ai didi

带有额外隐藏字段的 Android ListView

转载 作者:太空宇宙 更新时间:2023-11-03 12:19:16 24 4
gpt4 key购买 nike

我在 android 中使用 ListView 。以前我曾经从数据库中提取数据,所以我使用了 CursorAdapter 但现在我正在从网站获取响应,并且在网站上我有不同的链接,每个链接都有与之关联的名称。我在两个字符串数组中有名称和链接。我必须只显示名称,当单击每个名称时,将打开一个相同的新 Intent ,并将链接作为参数。

这是我在做的事情

listView = (ListView) findViewById(R.id.list);


String[] names= new String[] { "name1",
"name2"
};
String[] links= new String[] { "link1",
"link2"
};

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, send names and links);

listView.setAdapter(adapter);

listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

String itemValue = (String) listView.getItemAtPosition(position);
Intent myIntent = new Intent(arg0.getContext(), NewPage.class);
myIntent.putExtra("Url",how to get this);
startActivity(myIntent);
}

});
}

最佳答案

将两条信息包装在自定义对象中:

class Data {
String name;
String link;

@Override
public String toString() {
return name;
}
}

构建数据项列表:

String[] names= new String[] { "name1", 
"name2"
};
String[] links= new String[] { "link1",
"link2"
};
List<Data> output = new ArrayList<Data>();
for (int i = 0; i < names.length; i++) {
Data d = new Data();
d.name = names[i];
d.link = links[i]
output.add(d);
}
ArrayAdapter<Data> adapter = new ArrayAdapter<Data>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, output);

然后您可以在 OnItemClickListener 中使用 Data:

Data  item = (Data) listView.getItemAtPosition(position);
Intent myIntent = new Intent(arg0.getContext(), NewPage.class);
myIntent.putExtra("Url", d.link);
startActivity(myIntent);

关于带有额外隐藏字段的 Android ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21369037/

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