如何从 ListView 中提取对象 - getItemAtPosition 不起作用

Posted

技术标签:

【中文标题】如何从 ListView 中提取对象 - getItemAtPosition 不起作用【英文标题】:How to extract objects from ListView - getItemAtPosition won't work 【发布时间】:2012-06-25 05:52:57 【问题描述】:

我为我的ListView 实现了一个自定义适配器。如何从特定列表项中提取对象。这是我的自定义适配器的外观:

MyCustomAdapter.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;

   

这是 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;
    

这是我调用 onClickListener() 的 java sn-p :

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

      

  );

还有xml代码:

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabRow1"
    android:layout_
    android:layout_
    style="@style/BodyRow"
    android:gravity="center_horizontal" >

    <RelativeLayout 
    android:id="@+id/relativeLayout1"
    android:layout_
    android:layout_

    android:gravity="center_horizontal" >

    <TextView
        android:id="@+id/status"
        android:layout_
        style="@style/BodyText"
        android:layout_

        android:layout_alignParentRight="true"
         android:textColor="@color/textColor"
        android:text="TextView" />

    <TextView
        android:id="@+id/start_date"
        style="@style/HourText"
        android:layout_
        android:layout_
        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_
        android:layout_
        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_
        style="@style/BodyText"
        android:layout_
        android:layout_alignLeft="@+id/project"
        android:layout_below="@+id/project"
        android:text="TextView"
         android:textColor="@color/textColor" />

</RelativeLayout>
</TableRow>

我想我不能像通常使用的那样使用 onClickListener,因为我是扩展自定义适配器。在这种情况下,它们可能是一个问题

public void onItemClick(AdapterView<?> myAdapter, View myView, 
            int myItemInt, long mylng)

但是日志说:ClassCastException at DashboardBean。

无论如何,我无法修复它。有人可以帮忙吗?

【问题讨论】:

ClassCastException 到底在哪里?您究竟想在哪里附加监听器、整个列表项或其中的某些项目? 【参考方案1】:

ClassCastException 是由您的OnItemClickListener 中的以下行引起的:

TableRow tableRow = (TableRow) (mListView.getItemAtPosition(myItemInt));

那是因为您的适配器充满了DashboardBean 实例。 getItemAtPosition 方法返回一个对象,该对象属于适配器使用的数据结构,不是是显示对象的图形小部件(我假设是TableRow)的实例。随便写:

DashboardBean board = (DashboardBean) (mListView.getItemAtPosition(myItemInt));

代替有问题的行就可以了。然后,您可以直接使用 DashboardBean 对象中的字段,而不是通过 TextViews 和类似的 UI 元素。

【讨论】:

【参考方案2】:

您应该能够像这样在 onItemclick 中获取 DashboardBean 对象:

DashboardBean bean = (DashboardBean) mListView.getItemAtPosition(myItemInt);

【讨论】:

【参考方案3】:

如果您只是想提取对象,那么OnItemClickListener 会让您的任务更轻松。您可以查看行的实例。您可以从中提取内容。

改变

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

      

  );

到这里

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

    

);

【讨论】:

以上是关于如何从 ListView 中提取对象 - getItemAtPosition 不起作用的主要内容,如果未能解决你的问题,请参考以下文章

从 Listview Control 复选框获取通知代码

如何从sql server数据库提取数据到安卓,并用listview展示出来??求大神帮助

Android 从 ListView 项中提取特定值

如何从 C++ 访问 QML ListView 委托项目?

如何从对象列表添加到 ListView

vb.net listview中一行数据的提取