我有一个应用程序,显示带有自定义ArrayAdapter的项目列表。可以将监听器设置为允许用户点击一行以打开另一个活动来处理该行引用的项目,或者长按以删除项目。以下是其中一个侦听器的示例,它在相应的ListActivity中定义:
// single tap selects an item for viewing
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id)
{
Project p = projects.get(position);
Intent intent = new Intent(ListProjectActivity.this, CountingActivity.class);
intent.putExtra("project_id",p.id);
startActivity(intent);
}
});
然而,我发现长时间按压是许多用户不直观的事情,因此我更喜欢将按钮放入每行的自定义布局中,然后附加一个监听行中的TextView(表示单击以加载另一个活动)和按钮(用于删除)。所以,我将这样的监听器放入ArrayAdapter本身:
public class ProjectListAdapter extends ArrayAdapter<Project>
{
// set up constructor and project holder
//....
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
ProjectHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ProjectHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.txtTitle);
holder.deleteProject = (ImageButton) row.findViewById(R.id.deleteProject);
holder.txtTitle.setOnClickListener(mOnTitleClickListener);
holder.deleteProject.setOnClickListener(mOnDeleteClickListener);
// the button and textview above could be tagged with position, but that
// re-sets as the rows are re-used
row.setTag(holder);
}
else
{
holder = (ProjectHolder)row.getTag();
}
holder.txtTitle.setText(project.name);
return row;
}
private View.OnClickListener mOnTitleClickListener = new View.OnClickListener() {
@Override
public void onClick(View v)
{
Project p = null; // how can I get this here?
Intent intent = new Intent(getContext(), CountingActivity.class);
intent.putExtra("project_id",p.id);
mContext.startActivity(intent);
}
};
// the delete listener is similar
}
这里的困难在于我需要在轻敲的位置获取与ArrayAdapter关联的Project对象,因此我可以删除对象或打开一个新活动来处理它。有人可以建议我在使用那种类型的监听器时如何获得与该行相关联的对象吗?
答案 0 :(得分:2)
您可以拥有一个实现OnClickListener
的类,将该对象传递给构造函数:
private class MyOnClickListener implements OnClickListener {
private Project mProject;
public MyOnClickListener(Project p) {
mProject = p;
}
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), CountingActivity.class);
intent.putExtra("project_id",mProject.id);
mContext.startActivity(intent);
}
}
getView(...) {
// other logic
holder.txtTitle.setOnClickListener(new MyOnClickListener((Project)getItem(position)));
holder.txtTitle.setText(project.name);
return row;
}
答案 1 :(得分:1)
public class ProjectListAdapter extends ArrayAdapter<Project>
{
// set up constructor and project holder
//....
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
ProjectHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ProjectHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.txtTitle);
holder.deleteProject = (ImageButton) row.findViewById(R.id.deleteProject);
row.setTag(holder);
}
else
{
holder = (ProjectHolder)row.getTag();
}
holder.txtTitle.setText(project.name);
holder.txtTitle.setOnClickListener() {
@Override
public void onClick(View v)
{
// you can get the PROJECT refrence here
Intent intent = new Intent(getContext(), CountingActivity.class);
intent.putExtra("project_id",p.id);
mContext.startActivity(intent);
}
};
// the delete listener is similar
return row;
}
}