I am making an application, and I want to change an image inside some layout from another layout. Is that possible? Here is the case, the details activity contains spinner which has the items important and draft, and there is a save button which saves data in db. The main activity has a list view, each row consists of image view and text view. The xml file of the details activity is not the same as the xml file of the row layout which contains the image view. The image in the image view should change based on the value of the spinner in the saved note. I understand that the value of spinner can be passed by intents, and based on that we can set the image. But I am getting error due to referencing the id of the image view which is not in the xml file of the main activity. I am using the row xml only as argument in the loader, which is in the main activity, where the main activity xml file is different from the row xml file. In other words, there is no activity that uses row xml file as its layout, it is only used as a parameter in the loader. So, is changing the image possible in this case?
Here is the code I used in the details fragment in order to send the spinner value to a fragment in another activity:
if (isUpdateQuery) {
long id = getActivity().getIntent().getLongExtra("id", 0);
int updated = getActivity().getContentResolver().update(NotesContract.NotesTable.buildUriWithId(id), values, null, null);
Intent intent = new Intent(getActivity(), NotesList.class);
intent.putExtra("category", category);
startActivity(intent);
}//en inner if
And, this is the code I used in the fragment to get the spinner value and update the image:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View temp = inflater.inflate(R.layout.notes_row, container, false);
ImageView imageView = (ImageView) temp.findViewById(R.id.icon);
if (getActivity().getIntent().getStringExtra("category")!=null && getActivity().getIntent().getStringExtra("category").equalsIgnoreCase("important")){
imageView.setImageResource(R.drawable.staricon);
}
mSimpleCursorAdapter=new SimpleCursorAdapter(getActivity(),R.layout.notes_row,null, from, to,0);
View rootView = inflater.inflate(R.layout.fragment_todo_list, container, false);
getLoaderManager().initLoader(LOADER_ID, null, this); //once this is done onCreateLoader will be called.
final ListView listView = (ListView) rootView.findViewById(R.id.notes_list); //findViewById must be called using the rootView because we are inside a fragment.
if(mSimpleCursorAdapter.getCount()==0) {
TextView text= (TextView) rootView.findViewById(R.id.empty_list);
text.setVisibility(View.VISIBLE);
}
if (getActivity().findViewById (R.id.fragment_container)!=null){
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);}//end if.
listView.setAdapter(mSimpleCursorAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Cursor cursor = mSimpleCursorAdapter.getCursor();
if (cursor != null && cursor.moveToPosition(position)) {
String category= cursor.getString(1);
String summary= cursor.getString(2);
String description=cursor.getString(3);
long id= cursor.getLong(cursor.getColumnIndex(NotesContract.NotesTable._ID));
int locationId= cursor.getInt(cursor.getColumnIndex(NotesContract.NotesTable.COLUMN_LOCATION));
String [] retrievedData= {category, summary, description};
if (getActivity().findViewById (R.id.fragment_container)!=null){
//two pane layout:
listView.setItemChecked(position, true);
listView.setBackgroundColor(Color.BLUE);
Bundle args = new Bundle();
args.putStringArray("data",retrievedData);
/*args.putInt("update", 1);*/
args.putLong("id", id);
args.putInt("locationId", locationId);
mCallback.onlistElementClicked(args );/*this is available in the parent activity*/
}
else {
// one pane layout:
Intent intent = new Intent(getActivity(), NotesDetails.class);
intent.putExtra(Intent.EXTRA_TEXT, retrievedData);
/*intent.putExtra("update", 1); */ //to indicate that the query should be update not insert.
intent.putExtra("id", id);
intent.putExtra("locationId", locationId); //whether it is 0 or 1
startActivity(intent);
}
}//end outer cursor if.
}
});
return rootView;
}
I am getting a NullPointerException when trying to find the image by ID. As can be seen in the second code section, I have first temporarily inflate the view that contains the image in order to be able to get the image by id and update it, then I inflate the original layout of the fragment (rootView), and I returned rootView. Is that correct? Notes: The activity that I sent the intent to has a different layout from row layout, and the fragment inside that activity has its on layout also.
Any help is appreciated.
Thank you