Android 如何从 ListView 中提取对象 - getItemAtPosition 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11164543/
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 extract objects from ListView - getItemAtPosition won't work
提问by Harsh
I've a custom adapter implemented for my ListView
. How do I extract objects from a particular list item. Here's how my custom adapter looks:
我为我的ListView
. 如何从特定列表项中提取对象。这是我的自定义适配器的外观:
MyCustomAdapter.java
我的自定义适配器.java
public class MyCustomAdapter extends ArrayAdapter<DashboardBean> {
Context context;
int layoutResourceId;
DashboardBean currentMRB;
Vector<DashboardBean> data;
public MyCustomAdapter(Context context, int layoutResourceId, Vector<DashboardBean> data)
{
super(context,layoutResourceId,data);
this.layoutResourceId = layoutResourceId;
this.context=context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MyStringReaderHolder holder;
if(row==null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent,false);
holder= new MyStringReaderHolder();
holder.project = (TextView)row.findViewById(R.id.project);
holder.workRequest = (TextView)row.findViewById(R.id.work_request);
holder.startDate = (TextView)row.findViewById(R.id.start_date);
holder.status = (TextView) row.findViewById(R.id.status);
row.setTag(holder);
}
else
{
holder=(MyStringReaderHolder) row.getTag();
}
DashboardBean mrb =data.elementAt(position);
System.out.println("Position="+position);
holder.project.setText(mrb.project);
holder.workRequest.setText(mrb.workRequest);
holder.startDate.setText(mrb.startDate);
holder.status.setText(mrb.status);
return row;
}
static class MyStringReaderHolder {
TextView project, workRequest, startDate, status;
}
}
Here's the DashboardBean.java
这是 DashboardBean.java
public class DashboardBean {
public String project;
public String workRequest;
public String startDate;
public String status;
public DashboardBean(String project,String workRequest,String startDate,String status)
{
this.project = project;
this.workRequest = workRequest;
this.startDate = startDate;
this.status = status;
}
}
Here's java snippet from where I call onClickListener() :
这是我调用 onClickListener() 的 java 片段:
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView,
int myItemInt, long mylng) {
TableRow tableRow = (TableRow) (mListView.getItemAtPosition(myItemInt));
String project = ((TextView) tableRow.findViewById(R.id.project)).getText().toString();
String workRequest = ((TextView) tableRow.findViewById(R.id.work_request)).getText().toString();
String startDate = ((TextView) tableRow.findViewById(R.id.start_date)).getText().toString();
String status = ((TextView) tableRow.findViewById(R.id.status)).getText().toString();
showWorkRequest(project, workRequest, startDate, status);
}
});
And the xml code:
和 xml 代码:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/BodyRow"
android:gravity="center_horizontal" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal" >
<TextView
android:id="@+id/status"
android:layout_width="wrap_content"
style="@style/BodyText"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="@color/textColor"
android:text="TextView" />
<TextView
android:id="@+id/start_date"
style="@style/HourText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="TextView" android:textColor="@color/textColor" android:textSize="10sp"/>
<TextView
android:id="@+id/project"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/start_date"
android:gravity="center_horizontal"
style="@style/LeftHeaderText"
android:text="TextView" android:layout_marginLeft="5dp"
android:textColor="@color/textColor" android:textStyle="bold"/>
<TextView
android:id="@+id/work_request"
android:layout_width="wrap_content"
style="@style/BodyText"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/project"
android:layout_below="@+id/project"
android:text="TextView"
android:textColor="@color/textColor" />
</RelativeLayout>
</TableRow>
I guess I can't use onClickListener as it is usually used because I'm extendind custom adapter. In that case they may be a problem with
我想我不能使用通常使用的 onClickListener,因为我扩展了自定义适配器。在这种情况下,他们可能会遇到问题
public void onItemClick(AdapterView<?> myAdapter, View myView,
int myItemInt, long mylng)
But the log says: ClassCastException at DashboardBean.
但是日志说:DashboardBean 的 ClassCastException。
Anyway, I can't fix it. Could anyone help?
无论如何,我无法解决它。有人可以帮忙吗?
回答by Giulio Piancastelli
The ClassCastException
is caused by the following line in your OnItemClickListener
:
这ClassCastException
是由您的以下行引起的OnItemClickListener
:
TableRow tableRow = (TableRow) (mListView.getItemAtPosition(myItemInt));
And that's because your adapter is filled with DashboardBean
instances. The getItemAtPosition
method returns an object belonging to the data structure the adapter works with, notan instance of the graphic widget (TableRow
, I assume) which the object is shown on. Just writing:
那是因为您的适配器充满了DashboardBean
实例。该getItemAtPosition
方法返回一个属于适配器使用的数据结构的对象,而不是TableRow
显示该对象的图形小部件(我假设为 )的实例。只是写:
DashboardBean board = (DashboardBean) (mListView.getItemAtPosition(myItemInt));
in place of the offending line would do the trick. You can then work directly with the fields in the DashboardBean
object instead of passing through TextView
s and similar UI elements.
代替违规行就可以了。然后您可以直接使用DashboardBean
对象中的字段,而不是通过TextView
s 和类似的 UI 元素。
回答by Jules Colle
You should be able to get your DashboardBean object in onItemclick like this:
您应该能够像这样在 onItemclick 中获取 DashboardBean 对象:
DashboardBean bean = (DashboardBean) mListView.getItemAtPosition(myItemInt);
回答by Vipul Shah
If you just want to extract object then OnItemClickListener
makes your task easier.You ger view instance of the row.from that you can extract the contents.
如果您只想提取对象,那么OnItemClickListener
您的任务就更容易了。您可以查看行的实例。从中您可以提取内容。
Change
改变
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView,
int myItemInt, long mylng) {
TableRow tableRow = (TableRow) (mListView.getItemAtPosition(myItemInt));
String project = ((TextView) tableRow.findViewById(R.id.project)).getText().toString();
String workRequest = ((TextView) tableRow.findViewById(R.id.work_request)).getText().toString();
String startDate = ((TextView) tableRow.findViewById(R.id.start_date)).getText().toString();
String status = ((TextView) tableRow.findViewById(R.id.status)).getText().toString();
showWorkRequest(project, workRequest, startDate, status);
}
});
To This
至此
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView <? > myAdapter, View myView, int myItemInt, long mylng) {
String project = ((TextView) myView.findViewById(R.id.project)).getText().toString();
String workRequest = ((TextView) myView.findViewById(R.id.work_request)).getText().toString();
String startDate = ((TextView) myView.findViewById(R.id.start_date)).getText().toString();
String status = ((TextView) myView.findViewById(R.id.status)).getText().toString();
showWorkRequest(project, workRequest, startDate, status);
}
});