2013-11-27 181 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

相關問題