在adapter.java 文件中 监听Activity 出现错误 startActivity(intent)
publicclassTextViewAdapterextendsBaseAdapter{privateList<TextViewModel>model;privateL...
public class TextViewAdapter extends BaseAdapter {
private List<TextViewModel> model;
private LayoutInflater mInflater;
private Context context;
public TextViewAdapter(Context context,List<TextViewModel>model)
{
this.model = model;
this.mInflater = LayoutInflater.from(context);
this.context = context;
}
@Override
public int getCount()
{
return model.size();
}
@Override
public Object getItem(int position)
{
return position;
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position,View convertView,ViewGroup parent)
{
Holder holder = new Holder();
if(convertView == null)
{
Log.i("===adapter===","View is null,create!!!");
convertView = mInflater.inflate(R.layout.simple_list_item1,null);
holder.textView = (TextView)convertView.findViewById(R.id.textView);
convertView.setTag(holder);
}else{
Log.i("===adapter===","View not null");
holder = (Holder) convertView.getTag();
}
// OnClickListener Tv = new OnClickListener()
// {
//
// @Override
// public void onClick(View v) {
// // TODO Auto-generated method stub
//
// }};
OnClickListener Tv = new OnClickListener()
{
@Override
public void onClick(View v) {
switch(v.getId())
{
case 1:
UmengUpdateAgent.forceUpdate(context);
break;
case 2:
Intent in1 = new Intent(context,IpActivity.class);
startActivity(in1);
break;
case 3:
Intent intent = new Intent(context,About.class);
startActivity(intent);
break;
default:
break;
}
}
};
TextViewModel textView = model.get(position);
holder.textView.setOnClickListener(Tv);
holder.textView.setText(textView.getText());
textView.setId(textView.getId());
return convertView;
}
public static class Holder{
TextView textView;
}
} 展开
private List<TextViewModel> model;
private LayoutInflater mInflater;
private Context context;
public TextViewAdapter(Context context,List<TextViewModel>model)
{
this.model = model;
this.mInflater = LayoutInflater.from(context);
this.context = context;
}
@Override
public int getCount()
{
return model.size();
}
@Override
public Object getItem(int position)
{
return position;
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position,View convertView,ViewGroup parent)
{
Holder holder = new Holder();
if(convertView == null)
{
Log.i("===adapter===","View is null,create!!!");
convertView = mInflater.inflate(R.layout.simple_list_item1,null);
holder.textView = (TextView)convertView.findViewById(R.id.textView);
convertView.setTag(holder);
}else{
Log.i("===adapter===","View not null");
holder = (Holder) convertView.getTag();
}
// OnClickListener Tv = new OnClickListener()
// {
//
// @Override
// public void onClick(View v) {
// // TODO Auto-generated method stub
//
// }};
OnClickListener Tv = new OnClickListener()
{
@Override
public void onClick(View v) {
switch(v.getId())
{
case 1:
UmengUpdateAgent.forceUpdate(context);
break;
case 2:
Intent in1 = new Intent(context,IpActivity.class);
startActivity(in1);
break;
case 3:
Intent intent = new Intent(context,About.class);
startActivity(intent);
break;
default:
break;
}
}
};
TextViewModel textView = model.get(position);
holder.textView.setOnClickListener(Tv);
holder.textView.setText(textView.getText());
textView.setId(textView.getId());
return convertView;
}
public static class Holder{
TextView textView;
}
} 展开
1个回答
展开全部
case 2:
Intent in1 = new Intent(context,IpActivity.class);
startActivity(in1);
break;
case 3:
Intent intent = new Intent(context,About.class);
startActivity(intent);
break;
这里的context换成APPLICATION全局上下文,写一个Application类继承application类
Intent in1 = new Intent(context,IpActivity.class);
startActivity(in1);
break;
case 3:
Intent intent = new Intent(context,About.class);
startActivity(intent);
break;
这里的context换成APPLICATION全局上下文,写一个Application类继承application类
追问
我试过了但是无法点击呀??
追答
holder.textView.setOnClickListener(Tv); 改成holder.textView.setOnClickListener(new Tv());
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询