错误 - 如何将 setOnItemClickListener 设置为 listView?
ERROR - How to set setOnItemClickListener to listView?
我正在尝试将方法 setOnItemClickListener 设置为列表视图,但它报告错误
代码
//*******///
public class ViewAdapter extends BaseAdapter
{
LayoutInflater mInflater;
public ViewAdapter ()
{
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount ()
{
return favoriteList.size();
}
@Override
public Object getItem (int position)
{
return null;
}
@Override
public long getItemId (int position)
{
return position;
}
@Override
public View getView (final int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.itemlista, null);
}
final TextView infraText = (TextView) convertView.findViewById(R.id.tv_infra);
infraText.setText(favoriteList.get(position).getInfraccion());
final TextView ageText = (TextView) convertView.findViewById(R.id.matricula);
ageText.setText(favoriteList.get(position).getMatricula());
final TextView fechaText = (TextView) convertView.findViewById(R.id.tv_dia);
fechaText.setText(favoriteList.get(position).getFecha());
final TextView horaText = (TextView) convertView.findViewById(R.id.tv_hora);
horaText.setText(favoriteList.get(position).getHora());
final TextView tipoinfraText = (TextView) convertView.findViewById(R.id.tv_tipo);
tipoinfraText.setText(favoriteList.get(position).getTipoinfra());
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0,
View arg1, int position, long arg3)
{
Toast.makeText(InciEnviadasActivity.this, "posicion:" + position, Toast.LENGTH_LONG).show();
}
});
return convertView;
}
}
创建时//
//******//
//list = (Button) findViewById(R.id.list);
list =(ImageView) findViewById(R.id.list);
list.setOnClickListener(listOnClick);
listView = (ListView) findViewById(R.id.listView);
db = new BBDD(this, "BBDD", null, 1);
;
View.OnClickListener listOnClick = new OnClickListener()
{
@Override
public void onClick (View v)
{
favoriteList = db.getFavList();
listView.setAdapter(new ViewAdapter());
}
};
//list = (Button) findViewById(R.id.list);
list.setOnClickListener(listOnClick);
listView = (ListView) findViewById(R.id.listView);
db = new BBDD(this, "BBDD", null, 1);
;
//******/
我做错了什么?有什么建议可以解决吗?
顺便说一句,我从内部 SQlite 数据库填充 ListView。
更新:已解决,我混淆了 LISTVIEW 的名称,我正在尝试将方法分配给 IMAGEVIEW,我很抱歉
您正在尝试 setOnItemClickListener
到 ImageView
。
应该是listView.setOnItemClickListener...
list
是一个ImageView
,ImageView
没有setOnItemClickListener
。
在你的适配器 getView
中设置一个新的适配器也是不正确的,在你的 Activity
中只设置一次。在 onClick
方法中设置新的适配器也是如此,这不是一个好主意。
我正在尝试将方法 setOnItemClickListener 设置为列表视图,但它报告错误
代码
//*******///
public class ViewAdapter extends BaseAdapter
{
LayoutInflater mInflater;
public ViewAdapter ()
{
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount ()
{
return favoriteList.size();
}
@Override
public Object getItem (int position)
{
return null;
}
@Override
public long getItemId (int position)
{
return position;
}
@Override
public View getView (final int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.itemlista, null);
}
final TextView infraText = (TextView) convertView.findViewById(R.id.tv_infra);
infraText.setText(favoriteList.get(position).getInfraccion());
final TextView ageText = (TextView) convertView.findViewById(R.id.matricula);
ageText.setText(favoriteList.get(position).getMatricula());
final TextView fechaText = (TextView) convertView.findViewById(R.id.tv_dia);
fechaText.setText(favoriteList.get(position).getFecha());
final TextView horaText = (TextView) convertView.findViewById(R.id.tv_hora);
horaText.setText(favoriteList.get(position).getHora());
final TextView tipoinfraText = (TextView) convertView.findViewById(R.id.tv_tipo);
tipoinfraText.setText(favoriteList.get(position).getTipoinfra());
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0,
View arg1, int position, long arg3)
{
Toast.makeText(InciEnviadasActivity.this, "posicion:" + position, Toast.LENGTH_LONG).show();
}
});
return convertView;
}
}
创建时//
//******//
//list = (Button) findViewById(R.id.list);
list =(ImageView) findViewById(R.id.list);
list.setOnClickListener(listOnClick);
listView = (ListView) findViewById(R.id.listView);
db = new BBDD(this, "BBDD", null, 1);
;
View.OnClickListener listOnClick = new OnClickListener()
{
@Override
public void onClick (View v)
{
favoriteList = db.getFavList();
listView.setAdapter(new ViewAdapter());
}
};
//list = (Button) findViewById(R.id.list);
list.setOnClickListener(listOnClick);
listView = (ListView) findViewById(R.id.listView);
db = new BBDD(this, "BBDD", null, 1);
;
//******/
我做错了什么?有什么建议可以解决吗?
顺便说一句,我从内部 SQlite 数据库填充 ListView。
更新:已解决,我混淆了 LISTVIEW 的名称,我正在尝试将方法分配给 IMAGEVIEW,我很抱歉
您正在尝试 setOnItemClickListener
到 ImageView
。
应该是listView.setOnItemClickListener...
list
是一个ImageView
,ImageView
没有setOnItemClickListener
。
在你的适配器 getView
中设置一个新的适配器也是不正确的,在你的 Activity
中只设置一次。在 onClick
方法中设置新的适配器也是如此,这不是一个好主意。