从不同的 activity 更改 ImageView

Changing ImageView from a different activity

我正在制作一个应用程序,我想从另一个布局更改某个布局内的图像。那可能吗?情况就是这样,详细信息 activity 包含具有重要项目和草稿的微调器,还有一个将数据保存在数据库中的保存按钮。主要 activity 有一个列表视图,每一行由图像视图和文本视图组成。详细信息 activity 的 xml 文件与包含图像视图的行布局的 xml 文件不同。图像视图中的图像应根据已保存笔记中微调器的值进行更改。我知道微调器的值可以通过意图传递,并基于它我们可以设置图像。但是由于引用了不在主 activity 的 xml 文件中的图像视图的 ID,我得到了错误。我仅将行 xml 用作加载程序中的参数,它位于主 activity 中,其中主 activity xml 文件与行 [=35] 不同=] 文件。换句话说,没有使用行 xml 文件作为其布局的 activity,它仅用作加载程序中的参数。那么,在这种情况下是否可以更改图像?

这是我在详细信息片段中使用的代码,目的是将微调器值发送到另一个 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

而且,这是我在片段中用来获取微调器值和更新图像的代码:

 @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;
    }

尝试按 ID 查找图像时出现 NullPointerException。从第二个代码部分可以看出,我首先临时膨胀包含图像的视图,以便能够通过 id 获取图像并更新它,然后我膨胀片段的原始布局 (rootView),并且我返回了 rootView。那是对的吗? 注意:我将意图发送到的 activity 具有与行布局不同的布局,activity 中的片段也具有其 on 布局。

感谢任何帮助。

谢谢

如果您想使用与 activity 链接不同的 xml,那么您需要在此 activity 中膨胀另一个 xml。根据您的代码使用以下代码:

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;
}

希望这对您有所帮助,我希望我已经正确理解了您的问题。