Reputation: 163
i am developing a app that calls a web service and load the results to a list view. actually my list view has two buttons and one image view. i have use a custom list adapter class which is extends BaseAdapter. for now i just use Toast messages to identify the click events on the list items [ buttons and background of the list]. i want to start a new activity when i press a button in a list view..how can i do that ? i tried to call start activity method in onclick listener. but it didn't work..
here is my Adapter Class
public class NewsRowAdapter extends BaseAdapter {
private Context mContext;
public NewsRowAdapter (Context ctx) {
mContext = ctx;
}
private Activity activity;
private static LayoutInflater inflater=null;
private ArrayList<HashMap<String, String>> data;
int resource;
//String response;
//Context context;
//Initialize adapter
public NewsRowAdapter(Activity act, int resource,ArrayList<HashMap<String, String>> d) {
super();
this.resource=resource;
this.data = d;
this.activity = act;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View vi = convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.row,null);
TextView firstname = (TextView) vi.findViewById(R.id.fname);
TextView lastname = (TextView) vi.findViewById(R.id.lname);
TextView startTime = (TextView) vi.findViewById(R.id.stime);
TextView endTime = (TextView) vi.findViewById(R.id.etime);
TextView date = (TextView) vi.findViewById(R.id.blank);
ImageView img = (ImageView) vi.findViewById(R.id.list_image);
HashMap<String, String> song = new HashMap<String, String>();
song =data.get(position);
firstname.setText(song.get(MainActivity.TAG_PROP_FNAME));
lastname.setText(song.get(MainActivity.TAG_PROP_LNAME));
startTime.setText(song.get(MainActivity.TAG_STIME));
endTime.setText(song.get(MainActivity.TAG_ETIME));
date.setText(song.get(MainActivity.TAG_DATE));
//imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), img);
Button accept = (Button) vi.findViewById(R.id.button1);
accept.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final int x = (int) getItemId(position);
Toast.makeText(parent.getContext(),"you clicked "+ x , Toast.LENGTH_SHORT).show();
//Intent zoom=new Intent(mContext, Profile.class);
//mContext.startActivity(zoom);
//v.getContext().startActivity(zoom);
}
});
vi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(parent.getContext(), "view clicked: " , Toast.LENGTH_SHORT).show();
Intent intent = new Intent(mContext,Profile.class);
mContext.startActivity(intent);
}
});
return vi;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int possision) {
// TODO Auto-generated method stub
return possision;
}
@Override
public long getItemId(int possision) {
// TODO Auto-generated method stub
return possision;
}
}
StackTrace:
11-27 11:50:20.812: E/AndroidRuntime(3974): FATAL EXCEPTION: main
11-27 11:50:20.812: E/AndroidRuntime(3974): java.lang.NullPointerException
11-27 11:50:20.812: E/AndroidRuntime(3974): at android.content.ComponentName.<init>(ComponentName.java:75)
11-27 11:50:20.812: E/AndroidRuntime(3974): at android.content.Intent.<init>(Intent.java:3301)
11-27 11:50:20.812: E/AndroidRuntime(3974): at com.jsonlist.jsonlist.NewsRowAdapter$2.onClick(NewsRowAdapter.java:103)
Upvotes: 1
Views: 8588
Reputation: 2246
change constructor
public NewsRowAdapter(Context ctx, int resource,ArrayList<HashMap<String, String>> d) {
super();
this.resource=resource;
this.data = d;
this.mContext = ctx;
inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
and delete first constructor
public NewsRowAdapter (Context ctx) {
mContext = ctx;
}
Upvotes: 3
Reputation: 4344
Just use this instead.
Intent zoom=new Intent(parent.getContext(), Profile.class);
parent.getContext().startActivity(zoom);
Upvotes: 3