Har*_*rsh 6 android android-listview android-adapter
我为我的实现了一个自定义适配器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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我调用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);
}
});
Run Code Online (Sandbox Code Playgroud)
和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>
Run Code Online (Sandbox Code Playgroud)
我想我不能使用onClickListener,因为它通常被使用,因为我是extendind自定义适配器.在那种情况下,他们可能是一个问题
public void onItemClick(AdapterView<?> myAdapter, View myView,
int myItemInt, long mylng)
Run Code Online (Sandbox Code Playgroud)
但是日志说:DashboardBean上的ClassCastException.
无论如何,我无法解决它.有人可以帮忙吗?
Giu*_*lli 11
这ClassCastException
是由您的以下行引起的OnItemClickListener
:
TableRow tableRow = (TableRow) (mListView.getItemAtPosition(myItemInt));
Run Code Online (Sandbox Code Playgroud)
那是因为你的适配器充满了DashboardBean
实例.该getItemAtPosition
方法返回属于适配器使用的数据结构的对象,而不是TableRow
显示对象的图形小部件(我假设)的实例.只写:
DashboardBean board = (DashboardBean) (mListView.getItemAtPosition(myItemInt));
Run Code Online (Sandbox Code Playgroud)
代替违规线就可以了.然后,您可以直接使用DashboardBean
对象中的字段,而不是通过TextView
s和类似的UI元素.
归档时间: |
|
查看次数: |
23317 次 |
最近记录: |