1

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

Dania
  • 67
  • 3
  • 12

1 Answers1

1

If you want to use the different xml other than linked to the activity, than you need to inflate the other xml in this activity. Use the code below as per your code:

public View getView(int position, View convertView, ViewGroup parent) {
  View view;
view = Inflater.inflate(R.layout.another_xml, parent, false);

  /* Get the widget with id name which is defined in the another_xml of the row */
  ImageView imageView = (ImageView) view.findViewById(R.id.imageView);

  /* Populate the another_xml with info from the item */
  name.setText(myObject.getName());

  /* Return the generated view */
  return view;
}

Hope this helps you and I do hope that I have understood your question properly.

keshav kowshik
  • 2,354
  • 4
  • 22
  • 45
  • Thank you Kesh, yes you got my point regarding the question, I have used some similar code in the list fragment, but I am getting an error, I added the code to the question, can you please tell me what mistake I am doing? thanks a lot – Dania Mar 17 '15 at 17:27
  • you have two inflater.inflate statements, are you trying to inflate two xml's? – keshav kowshik Mar 17 '15 at 17:32
  • In those two xml's in which do you have the Image View? – keshav kowshik Mar 17 '15 at 17:33
  • I inflated the view of the fragment first by using fragment_todo_list, this layout doesn't contain the row layout of the list view it only contains the list view. Then, I needed to separately inflate the xml file of each row in the list, so that I can get the id of the image view and set the image. I am only using row layout as parameter in the simplecursoradapter, that's why I get the image view id, set the image, then pass the xml file in the loader, but I am getting an error, what is my mistake? Thank you – Dania Mar 17 '15 at 17:40
  • Ok. Since you are using simple cursor adapter I beleive you are fetching data from database. You are telling ImageView is your custom row for your list view right? If yes then you need not set the Image separately to your list view it will taken automatically from drawable you have define those in an int[]. Am I speaking sense? are you getting it? – keshav kowshik Mar 17 '15 at 17:46
  • What is your from and to? – keshav kowshik Mar 17 '15 at 17:47
  • Yes I got your point, but I have set a default image in the xml file using drawable, and I need it to change dynamically according to the spinner value, that is why I inflate the row view, then tried to get the image by id, but that is not working, how can I solve this issue? – Dania Mar 17 '15 at 17:49
  • from is the columns from which the adapter should fetch the data, to are the views in which these data should be organized in the row, here are to and from, private String [] from = {NotesContract.NotesTable.COLUMN_SUMMARY, NotesContract.NotesTable.COLUMN_DATE}; private int [] to = {R.id.label, R.id.date}; date and label are in the row xml file, and I am getting no error here, but I don't know why when I get the id to change the image I get an error.. – Dania Mar 17 '15 at 17:52
  • Simple reason for your null pointer is row is null. you can use Log message and check the value of row. – keshav kowshik Mar 17 '15 at 17:59
  • Can you move the same code to onActivityCreated method and check if it works? onCreateView has the chances of being called before your other activity's onCreate gets called. I am not sure this will solve your issue, but just a check. – keshav kowshik Mar 17 '15 at 18:03
  • did you try by using log messages? – keshav kowshik Mar 17 '15 at 18:23
  • Are you sure the id for Image is R.id.image? is that correct? – keshav kowshik Mar 17 '15 at 18:25
  • Sorry for late reply. Still having same problem? – keshav kowshik Mar 18 '15 at 04:55
  • It's okay, thanks for response, yes. I will update the code please take a look at it. Thank you. – Dania Mar 18 '15 at 15:23