我想打電話從適配器外類方法,如如何從適配器調用外部活動方法?
new MainActivity().openPainRecordDialog(context,dbHelper);
它工作正常。
但是當我做這樣的
Boolean status=new MainActivity().openPainRecordDialog(context,dbHelper);
if(status)
check();
檢查()是適配器類的方法。
和
openPainRecordDialog(final Context context, final DbHelper dbHelper)
{
//some unrelated data.
if (logged_in && isNetworkConnected()){
}
}
public boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null;
}
錯誤我越來越 不適用於活動系統服務的onCreate() 之前在這行代碼ConnectivityManager釐米=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
問題
- 這是調用活動方法的正確途徑。 2.爲什麼我得到這個錯誤。
編輯
我試圖通過接口來做到這一點。 但最終得到錯誤。
'INT self.anotherclassfunction.SimpleAdapter $ AdapterCallback.onMethodCallback()' 上的空對象引用
適配器類
public class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.ViewHolder> {
String[] goals;
Context context;
private AdapterCallback mAdapterCallback;
public SimpleAdapter(Context context, String[] goals)
{
super();
this.context=context;
this.goals=goals;
}
public SimpleAdapter(Context context) {
try {
this.mAdapterCallback = ((AdapterCallback) context);
} catch (ClassCastException e) {
throw new ClassCastException("Activity must implement AdapterCallback.");
}
}
@Override
public SimpleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.simple_item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final SimpleAdapter.ViewHolder holder, int position) {
holder.textView.setText(goals[position]);
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
**int result=mAdapterCallback.onMethodCallback();** //this is the line where I am getting error
Toast.makeText(context,Integer.toString(result), Toast.LENGTH_SHORT).show();
} catch (ClassCastException exception) {
// do something
Log.i("In the catch","Yes");
}
}
});
}
@Override
public int getItemCount() {
return goals.length;
}
public class ViewHolder extends RecyclerView.ViewHolder {
Button textView;
public ViewHolder(View itemView) {
super(itemView);
textView=(Button) itemView.findViewById(R.id.text);
}
}
public static interface AdapterCallback {
int onMethodCallback();
}
}
適配器調用類
public class OtherClass extends AppCompatActivity {
RecyclerView recyclerView;
SimpleAdapter simpleAdapter;
String[] action_name={"Swimming","Yoga","SWD","IFT","Follow Diet Plan", "Diagnostic Tests","Record Temperature","Record Blood Pressure"," Record Sugar Level","Record Weight"};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xtra);
recyclerView=(RecyclerView) findViewById(R.id.recylerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
simpleAdapter=new SimpleAdapter(this, action_name);
recyclerView.setAdapter(simpleAdapter);
}
}
我試圖調用的方法的活動:
public class MainActivity extends AppCompatActivity implements SimpleAdapter.AdapterCallback {
private SimpleAdapter mAdapterCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
this.mAdapterCallback=new SimpleAdapter(MainActivity.this);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public int onMethodCallback() {
// do something
return 2;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void test(View v)
{
startActivity(new Intent(MainActivity.this,OtherClass.class));
finish();
}
public int sampleFunction()
{
return 2;
}
}
要在適配器到底該怎麼做? –
'新的MainActivity()'你永遠不應該調用Activity的派生類的構造函數... alsked bazillion times ... – Selvin
在你的適配器中使用像這樣'ConnectivityManager cm =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); ' –