单击自定义列表视图按钮时启动活动

时间:2013-11-27 06:43:29

标签: android listview

我正在开发一个调用Web服务并将结果加载到列表视图的应用程序。实际上我的列表视图有两个按钮和一个图像视图。我使用自定义列表适配器类扩展BaseAdapter。现在我只使用Toast消息来识别列表项[按钮和列表背景]上的点击事件。当我按下列表视图中的按钮时,我想开始一项新活动。我可以这样做吗?我试图在onclick监听器中调用start activity方法。但它不起作用..

这是我的适配器类

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;
}
}

堆栈跟踪:

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) 

3 个答案:

答案 0 :(得分:3)

请改用它。

Intent zoom=new Intent(parent.getContext(), Profile.class);   
parent.getContext().startActivity(zoom);

答案 1 :(得分:3)

更改构造函数

 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);

}

并删除第一个构造函数

public NewsRowAdapter (Context ctx) {
mContext = ctx;

}

答案 2 :(得分:0)

请检查您的活动是否在AndridManifest.xml中声明