java 如何在android java中从ArrayList获取对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34341598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get object from ArrayList in android java
提问by SeaDog
I have an ArrayListin my Adapter. When an item is clicked, I want to construct a new Intentusing an object from the ArrayList. How do I get the Object out of the list?
我的ArrayList适配器中有一个。单击项目时,我想Intent使用ArrayList. 如何从列表中取出对象?
Below is snippet of my onClick:
以下是我的 onClick 片段:
public class zlecenia extends Activity {
ListView listview;
ArrayList myList = new ArrayList();
String[] desc = new String[]{
"Desc 1", "Desc 2", "Desc 3", "Desc 4",
"Desc 5", "Desc 6", "Desc 7", "Desc 8"
};
String[] data = new String[]{
"20:11", "", "11:25", "", "15:11", "11:25", "15:11", "11:25"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.zlecenia);
listview = (ListView) findViewById(R.id.listView);
for (int i = 0; i < title.length; i++) {
ListData ld = new ListData();
ld.setDescription(desc[i]);
ld.setData(data[i]);
myList.add(ld);
}
listview.setAdapter(new MyBaseAdapter(this, myList));
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// What here?
}
});
}
}
public class ListData {
String Description;
String title;
String data;
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
回答by Mohammed Aouf Zouag
Change
改变
ArrayList myList = new ArrayList();
to
到
ArrayList<ListData> myList = new ArrayList<ListData>();
And use this:
并使用这个:
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// The 'position' argument represents the index of the clicked listview item
ListData listdata = myList.get(position);
// Do whatever you want with your listData item
}
回答by Amit Sinha
you can call ListData listdata = myList.get(position);
你可以调用 ListData listdata = myList.get(position);
on the basis of position we can fetch the object from arraylist
根据位置,我们可以从 arraylist 中获取对象
回答by Epicblood
ArrayLists (as well as most implementations of List) are supposed to be told what type of object they contains, in your case that is ListData, so you should change your construction of the list ArrayList myList = new ArrayList();to ArrayList<ListData> myList = new ArrayList<>();
ArrayLists(以及 的大多数实现List)应该被告知它们包含什么类型的对象,在您的情况下是ListData,因此您应该将列表的构造更改ArrayList myList = new ArrayList();为ArrayList<ListData> myList = new ArrayList<>();
All you have to do now, is change Array object = (Array) myList.get(position);to be ListData myData = myList.get(positiion);. Then you can do whatever you need to with your ListData
你现在要做的就是改变Array object = (Array) myList.get(position);自己ListData myData = myList.get(positiion);。然后你可以做任何你需要的事情ListData
I suggest you read the ArrayListdocumentation (and the java one)

