Good day all, I am working on an application where I have multiple Activities (Activities A, B, B) that inflate this fragment (TheListFragment).
This is a visual representation of what it looks like:
Currently, I am able to change the descriptions, images, etc from the Fragment itself, but that is not what I would like to do. My goal is to have the respective activities all be able to change the fragment and use the fragment as a reusable listview. Here is some of my code:
public class TheListFragment extends Fragment {
CustomAdapter.Holder holder;
//The listview
ListView listView;
//When the view is created
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Inflate the add new game layout view
View view = inflater.inflate(R.layout.list_view_holder, container, false);
//Setup the listview and reference it to the listview id
listView = (ListView) view.findViewById(R.id.data_list_view);
//Set the custom adapter to the listview
listView.setAdapter(new CustomAdapter(context);
return view;
}
//This custom adapter is used to fill the respective data into the listview
class CustomAdapter extends BaseAdapter {
Context context;
public CustomAdapter(Context context)) {
this.context = context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return all_passed_data.size();
//List of data. removed it for space purposes
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
//Holds the respective fields
public class Holder {
//UI Components
TextView textview;
ImageView imageView;
LinearLayout linearLayout;
}
//Custom view
public View getView(final int position, View convertView, ViewGroup parent) {
//A holder object to reference the textviews and imageviews
holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.custom_list_view, null);
holder.imageView = (ImageView) rowView.findViewById(R.id.custom_list_view_image);
holder.textview = (TextView) rowView.findViewById(R.id.custom_list_view_game_name);
holder.linearLayout = (LinearLayout) rowView.findViewById(R.id.left_layout);
return rowView;
}
}
}
I am trying to set/ alter the listView contents (The holder class items) from within the activity that is inflating the fragment itself. IE, something along the lines of this:
public class ActivityA extends Activity{
FragmentManager manager;
ListFragment fragment;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_fragment_holder);
manager = getFragmentManager();
ListFragment fragment = (ListFragment) manager.findFragmentById(R.id.the_list_fragment);
//These will not work at the moment, they read as null pointers
//This will be changed dynamically depending on the activity. Could be checkbox, imageview, etc.
fragment.holder.linearLayout.addView(holder.checkBox);
fragment.holder.textview.setText("I Want to set the text here");
//Make it do something when clicked
fragment.holder.textview.setOnClickListener(this);
//Use third party library picasso to set the image
fragment.Picasso.with(context).load("http://www.google.com/somepicture").into(holder.imageview);
}
}
But I am having no luck figuring out how to accomplish this. I have read through these links: Call fragment events from activity , Android: Reusing same View object in different Activities (the case is about ad banners) , How to access UI elements created by a List Adapter from an Activity? . Do I need to implement an interface that accesses the data somehow? If so, how? Or some other means I am missing?