2013-11-27 130 views
0

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

这里是我的适配器类别

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) 
+1

你是否看到烤面包 – Raghunandan

+0

是......烤面包的信息是完美的显示 –

+0

嘿,你可以检查你的应用程序的显示,你调用的这个活动是否有一个条目,我认真地看到代码没有问题? – Techfist

回答

3

只要用这个代替。

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

非常感谢...这是工作....你是男人:) –

+0

嗨...再次我可以移动到下一个活动,我想去根据你的指导。现在我正在努力与对话框。我已经尝试了很多方法..因为我在一个适配器类中,我不能这样做..你可以建议我一种方法来实现它吗? –

+0

你想要什么,实现与对话框,究竟什么是问题所在。 – Techfist

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; 

}

0

,如果你的活动是在AndridMani声明请检查fest.xml

相关问题