我想從我的適配器活動開始一個新的活動到另一個活動通過按鈕。即時通訊新的Android工作室,我有搜索整個一些主題,我找到了一些解決方案,但仍然沒有修復我的代碼,他們不斷地告訴不同的錯誤。請幫幫我 。如何通過按鈕從ArrayAdapter啓動新的活動?
我想通過btn_reserve
將其移動到ReserveActivity.java
但是當我運行上面的代碼,它表示
Error:(54, 28) error: no suitable constructor found for Intent(<anonymous OnClickListener>,Class<ReserveActivity>) constructor Intent.Intent(String,Uri) is not applicable (argument mismatch; <anonymous OnClickListener> cannot be converted to String)
構造Intent.Intent(上下文,類)是不適用 (自變量不匹配;不能被轉換爲上下文)
所以我的繼承人代碼
public class mainmodeladapter extends ArrayAdapter<String> {
private Activity context;
private final String[] itemname;
private final Integer[] imgid;
private Context mContext;
public mainmodeladapter(Activity context, String[] itemname, Integer[] imgid) {
super(context, R.layout.modelmain, itemname);
this.context=context;
this.itemname=itemname;
this.imgid=imgid;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.modelmain, null,true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.lapangantxt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView extratxt = (TextView) rowView.findViewById(R.id.hargatxt);
txtTitle.setText(itemname[position]);
imageView.setImageResource(imgid[position]);
extratxt.setText("Rp 150.000/Jam");
return rowView;
Button btn_reserve = (Button) rowView.findViewById(R.id.btn_reserve);
btn_reserve.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(this, ReserveActivity.class);
context.startActivity(i);
}
});
};
}
IVE搜索在另一個線程,它說,我需要把context.startActivity(i)
因爲它的適配器,怎麼當我context.
從startActivity(i);
刪除,它開始顯示紅色亮點
這樣做rowView.findViewById(R.id.btn_reserve);
如果我刪除rowView.
的findviewById();
將開始有紅色的亮點,我不知道爲什麼。我錯了「btn」代碼嗎?它應該在另一個活動中嗎? 請幫助我。謝謝
I =新意圖(此,ReserveActivity.class)''this'意味着當前類和當前類是'OnClickListener'。您不能將偵聽器傳遞給「Intent」構造函數的第一個參數。這是編譯時錯誤,我不知道爲什麼你的編譯器無法找到它。無論如何,你應該讀取錯誤之前發佈到SO,消息是非常清楚的 – Beloo
ive將它改爲'(context,ReserveActivity.class)'miracely,它的工作原理,但我不知道爲什麼當我按下「保留」按鈕它崩潰應用程序。無論如何確認如何知道崩潰從何而來?即時通訊真的新調試 –