So,I'm doing 2 fragments in a activity and 1 fragment uses a listview+array adapter. but when i try to create a instance of the adapter it is null. i saw olther people with the same problem and what worked for them didn't work for me. ALSO THIS IS THE FIRST APP THAT I MADE ON MY OWN SO DON'T GET MAD IF I AM DUMB
MyFragment.onCreateView
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView =inflater.inflate(R.layout.fragment_todo, container, false);
ListView listView = rootView.findViewById(R.id.list_view);
ArrayList<Task> arrayList = new ArrayList<>();
arrayList.add(new Task("this is a placeholder todo",Task.PRIORITY_LOW, new Date()));
arrayList.add(new Task("this is a placeholder todo",Task.PRIORITY_MEDIUM, new Date()));
arrayList.add(new Task("this is a placeholder todo",Task.PRIORITY_HIGH, new Date()));
listView.setAdapter(new HomeActivity.TaskAdapter(getActivity(), arrayList,rootView));
ViewModel viewModel = ViewModelProviders.of(this).get(ViewModel.class);
viewModel.getTasks().observe(this, new Observer<List<Task>>() {
@Override
public void onChanged(@Nullable List<Task> taskEntries) {
//TODO SET THE ADAPTER
}
});
return rootView;
}
MyMainActivity wich contains the adapter aswell :
public class HomeActivity extends FragmentActivity {
static int colorGreen ;
static int colorOrange;
static int colorRed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
colorGreen=HomeActivity.this.getResources().getColor(R.color.colorGreen);
colorOrange=HomeActivity.this.getResources().getColor(R.color.colorOrange);
colorRed=HomeActivity.this.getResources().getColor(R.color.colorPrimaryDark);
//FAB
FloatingActionButton fab = 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();
}
});
initializeAds();
DataBase Db = DataBase.getInstance(getApplicationContext());
ViewPager vp = findViewById(R.id.view_pager);
CustomFragmentPageAdapter myAdapter = new CustomFragmentPageAdapter(getSupportFragmentManager(),this);
vp.setAdapter(myAdapter);
TabLayout mTabLayout = findViewById(R.id.tabLayout_Home);
mTabLayout.setupWithViewPager(vp);
}
@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_home, 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 static class TaskAdapter extends ArrayAdapter<Task> {
private View rootView;
public TaskAdapter(Activity context, ArrayList<Task> tasks ,View view) {
super(context, 0, tasks );
rootView=view;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = rootView;
}
Task currentTask = getItem(position);
TextView description = convertView.findViewById(R.id.description_todo);
if (currentTask != null) {
description.setText(currentTask.getDescription());
}
TextView date = convertView.findViewById(R.id.date_todo);
if (currentTask != null) {
date.setText(currentTask.getUpdatedAt().toString());
}
ConstraintLayout constraintLayout = convertView.findViewById(R.id.constraint_layout_todo);
if (currentTask != null) {
switch (currentTask.getPriority()){
case Task.PRIORITY_LOW:constraintLayout.setBackgroundColor(colorGreen);
case Task.PRIORITY_MEDIUM:constraintLayout.setBackgroundColor(colorOrange);
case Task.PRIORITY_HIGH:constraintLayout.setBackgroundColor(colorRed);
}
}
return convertView;
}
}