Dar*_*wda 2 android android-intent baseadapter android-activity
我使用以下代码生成自定义ListView.一切都运行正常,但是当用户点击列表视图时,我无法转到另一个类,因为Intents不能在非Activity类中使用.有没有办法为下面的代码启动一个活动???
public class CustomAdapter extends BaseAdapter {
String[] result;
Context context;
int[] imageId;
private static LayoutInflater inflater = null;
public CustomAdapter(MainActivityList mainActivity, String[] prgmNameList,
int[] prgmImages) {
// TODO Auto-generated constructor stub
result = prgmNameList;
context = mainActivity;
imageId = prgmImages;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder {
TextView tv;
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.program_list, null);
holder.tv = (TextView) rowView.findViewById(R.id.textViewlist);
holder.img = (ImageView) rowView.findViewById(R.id.imageViewlist);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(position)
{
case 0:
{
//Intent is not working here
//Here i need some help
}
}
}
});
return rowView;
}
}
Run Code Online (Sandbox Code Playgroud)
我被困在项目的这一部分,请帮帮我..
Rag*_*dan 12
使用活动上下文
Intent intent = new Intent(context,AnotherActivity.class);
context.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10764 次 |
| 最近记录: |