我使用从SQLite数据库检索的项目填充Scrollview,并使用下面的代码为每一行动态生成OnClickListener。我希望OnClickListener中的代码将控制权返回给使用Intent调用它的Activity(MealActivity),并传回所单击项目的id。
我已尝试使用下面的第二个代码段,但我收到了编译错误:"方法startActivity(Intent,int)未定义类型OnClickListenerSelectPresetItem"。
如何从OnClickListener调用原始Activity?
public class SelectPresetItemActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_preset_item);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
readRecords();
}
public void readRecords() {
LinearLayout linearLayoutRecords = (LinearLayout) findViewById(R.id.linearLayoutRecords);
linearLayoutRecords.removeAllViews();
List<preset_item> preset_item = new TableControllerPresetItem(this).read();
if (preset_item.size() > 0) {
for (preset_item obj : preset_item) {
long id = obj.id;
String PresetDesc = obj.preset_desc;
int PresetMinutes = obj.preset_minutes;
String textViewContents = PresetDesc + " - "
+ PresetMinutes + " minutes";
TextView textViewItem = new TextView(this);
textViewItem.setPadding(0, 10, 0, 10);
textViewItem.setText(textViewContents);
textViewItem.setTag(Long.toString(id));
textViewItem.setOnClickListener(new OnClickListenerSelectPresetItem());
linearLayoutRecords.addView(textViewItem);
}
}
else {
TextView locationItem = new TextView(this);
locationItem.setPadding(8, 8, 8, 8);
locationItem.setText("No records yet.");
linearLayoutRecords.addView(locationItem);
}
}listener
The method startActivity(Intent, int) is undefined for the type OnClickListenerSelectPresetItem
public class SelectPresetItemActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_preset_item);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
readRecords();
}
public void readRecords() {
LinearLayout linearLayoutRecords = (LinearLayout) findViewById(R.id.linearLayoutRecords);
linearLayoutRecords.removeAllViews();
List<preset_item> preset_item = new TableControllerPresetItem(this).read();
if (preset_item.size() > 0) {
for (preset_item obj : preset_item) {
long id = obj.id;
String PresetDesc = obj.preset_desc;
int PresetMinutes = obj.preset_minutes;
String textViewContents = PresetDesc + " - "
+ PresetMinutes + " minutes";
TextView textViewItem = new TextView(this);
textViewItem.setPadding(0, 10, 0, 10);
textViewItem.setText(textViewContents);
textViewItem.setTag(Long.toString(id));
textViewItem.setOnClickListener(new OnClickListenerSelectPresetItem());
linearLayoutRecords.addView(textViewItem);
}
}
else {
TextView locationItem = new TextView(this);
locationItem.setPadding(8, 8, 8, 8);
locationItem.setText("No records yet.");
linearLayoutRecords.addView(locationItem);
}
}
这是OnCLickListener代码:
public class OnClickListenerSelectPresetItem implements OnClickListener {
public final static String EXTRA_MEAL_ID = "com.ian.mealtimer.MEAL_ID";
@Override
public void onClick(View view) {
Long selectedMealId = Long.valueOf(view.getTag().toString());
Intent myIntent = new Intent(view.getContext(),
MealActivity.class);
long mealId = -1;
myIntent.putExtra(EXTRA_MEAL_ID, selectedMealId);
startActivity(myIntent, 0);
}
}
答案 0 :(得分:3)
你应该使用: startActivity (Intent intent)
有一个不同的方法,它有一个整数参数: startActivityForResult (Intent intent, int requestCode)
上下文也可以启动活动,因此您必须获取上下文。您可以轻松地从您的视图中获取它:
@Override
public void onClick(View view) {
Long selectedMealId = Long.valueOf(view.getTag().toString());
Intent myIntent = new Intent(view.getContext(),
MealActivity.class);
long mealId = -1;
myIntent.putExtra(EXTRA_MEAL_ID, selectedMealId);
view.getContext().startActivity(myIntent);
}
答案 1 :(得分:1)
startActivity是Context类的一种方法。 OnClickListener无法知道应该在哪个上下文中调用它。将Context作为参数传递给OnClickListener的构造函数并将其保存在: 在OnClickListenerSelectPresetItem类中:
private Context context;
public OnClickListenerSelectPresetItem(Context context){
this.context=context;
}
然后在此上下文中调用startActivity
this.context.startActivity(myIntent, 0);
通过传递活动
来构建它们textViewItem.setOnClickListener(new OnClickListenerSelectPresetItem(this));
答案 2 :(得分:0)
如何将活动传递给OnClickListenerSelectPresetItem的构造函数 然后像下面那样使用它:
public class OnClickListenerSelectPresetItem implements OnClickListener {
public final static String EXTRA_MEAL_ID = "com.ian.mealtimer.MEAL_ID";
private SelectPresetItemActivity selectPresetItemActivity;
public OnClickListenerSelectPresetItem(SelectPresetItemActivity selectPresetItemActivity){
this.selectPresetItemActivity = selectPresetItemActivity;
}
@Override
public void onClick(View view) {
Long selectedMealId = Long.valueOf(view.getTag().toString());
Intent myIntent = new Intent(view.getContext(),
MealActivity.class);
long mealId = -1;
myIntent.putExtra(EXTRA_MEAL_ID, selectedMealId);
selectPresetItemActivity.startActivity(myIntent, 0);
}
}