如何在Activity类中实现Singleton类(How to implement Singleton class in Activity Class)

编程入门 行业动态 更新时间:2024-10-27 04:24:46
如何在Activity类中实现Singleton类(How to implement Singleton class in Activity Class)

我想在整个应用程序中提供单一培训。 让我们从主要活动列表视图中说出每个单一的培训。 但我没有从Singleton类中获取值。 我的代码中有什么问题。 我从昨天开始一直在努力,想不通为什么? 感谢您的帮助。

这是Singleton类:

public class CurrentTraining { private Training training; //Training is my model class. private static CurrentTraining instance; private CurrentTraining() { } public static CurrentTraining getInstance() { if (instance == null) instance = new CurrentTraining(); return instance; } public Training getTraining() { return training; } public Training setTraining(Training training) { return this.training = training; }

}

我的主要活动课程:

public class MainActivity extends ListActivity implements OnItemClickListener { private Portfolio portfolio; private ListAdapter trainingListAdapter; private Dialog dialog; Training currentTraining; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); currentTraining = CurrentTraining.getInstance().getTraining(); portfolio = new Portfolio(this); trainingListAdapter = new ArrayAdapter<Training>(this, android.R.layout.simple_list_item_1, portfolio.getTrainingArrayList()); setListAdapter(trainingListAdapter); ((ListView) findViewById(android.R.id.list)) .setOnItemClickListener(this); } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Log.i ("DEBUG" , currentTraining.getTitle()); CurrentTraining.getInstance().setTraining(currentTraining); Toast.makeText(getApplicationContext(), "You clicked on position : " + arg2 + " and ID : " + currentTraining.getId(), Toast.LENGTH_LONG).show(); }

OnClick到listView它因为NullPointer异常而崩溃。

I want to make my a single Training available in whole application. Let's say each single Training from main activity listview. But I am not getting the values from Singleton class. What is the problem in my code. I have been trying since yesterday and couldn't figure out why?? Thanks for your help in advance.

Here is Singleton class:

public class CurrentTraining { private Training training; //Training is my model class. private static CurrentTraining instance; private CurrentTraining() { } public static CurrentTraining getInstance() { if (instance == null) instance = new CurrentTraining(); return instance; } public Training getTraining() { return training; } public Training setTraining(Training training) { return this.training = training; }

}

My Main activity class:

public class MainActivity extends ListActivity implements OnItemClickListener { private Portfolio portfolio; private ListAdapter trainingListAdapter; private Dialog dialog; Training currentTraining; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); currentTraining = CurrentTraining.getInstance().getTraining(); portfolio = new Portfolio(this); trainingListAdapter = new ArrayAdapter<Training>(this, android.R.layout.simple_list_item_1, portfolio.getTrainingArrayList()); setListAdapter(trainingListAdapter); ((ListView) findViewById(android.R.id.list)) .setOnItemClickListener(this); } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Log.i ("DEBUG" , currentTraining.getTitle()); CurrentTraining.getInstance().setTraining(currentTraining); Toast.makeText(getApplicationContext(), "You clicked on position : " + arg2 + " and ID : " + currentTraining.getId(), Toast.LENGTH_LONG).show(); }

OnClick to the listView it crashes because of NullPointer Exception.

最满意答案

Log.i ("DEBUG" , currentTraining.getTitle()); CurrentTraining.getInstance().setTraining(currentTraining);

没有设定价值,如何获得价值?

@Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { currentTraining = (Training)arg0.getAdapter().getItem(arg2); Log.i ("DEBUG" , currentTraining.getTitle()); CurrentTraining.getInstance().setTraining(currentTraining); Toast.makeText(getApplicationContext(), "You clicked on position : " + arg2 + " and ID : " + currentTraining.getId(), Toast.LENGTH_LONG).show(); } Log.i ("DEBUG" , currentTraining.getTitle()); CurrentTraining.getInstance().setTraining(currentTraining);

not ever set value, how to get the value?

@Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { currentTraining = (Training)arg0.getAdapter().getItem(arg2); Log.i ("DEBUG" , currentTraining.getTitle()); CurrentTraining.getInstance().setTraining(currentTraining); Toast.makeText(getApplicationContext(), "You clicked on position : " + arg2 + " and ID : " + currentTraining.getId(), Toast.LENGTH_LONG).show(); }

更多推荐

本文发布于:2023-07-08 19:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1080000.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类中   如何在   Activity   Singleton   class

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!