【问题标题】:Changing ImageView from a different activity从不同的活动更改 ImageView
【发布时间】:2015-03-17 16:51:00
【问题描述】:

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

这是我在详细信息片段中使用的代码,用于将微调器值发送到另一个活动中的片段:

 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 布局。

感谢任何帮助。

谢谢

【问题讨论】:

  • 太宽泛的问题,我建议你用代码 sn-p 和你得到的错误将它们分成块
  • 好的,我会发布一些代码

标签: android xml android-layout android-inflate


【解决方案1】:

如果您想使用链接到活动之外的其他 xml,则需要在此活动中扩展其他 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;
}

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

【讨论】:

  • 谢谢 Kesh,是的,你明白我的意思,我在列表片段中使用了一些类似的代码,但我收到错误,我将代码添加到问题中,你能告诉我我在做什么错误?非常感谢
  • 你有两个 inflater.inflate 语句,你是要充气两个 xml 吗?
  • 在这两个 xml 中你有 Image View 吗?
  • 我首先使用fragment_todo_list对片段的视图进行了膨胀,这个布局不包含列表视图的行布局,它只包含列表视图。然后,我需要单独对列表中每一行的xml文件进行inflate,这样我就可以获取图像视图的id并设置图像。我只在 simplecursoraapter 中使用行布局作为参数,这就是为什么我获取图像视图 id,设置图像,然后在加载器中传递 xml 文件,但是我得到一个错误,我的错误是什么?谢谢
  • 好的。由于您使用的是简单的游标适配器,我相信您正在从数据库中获取数据。您是在告诉 ImageView 是您的列表视图的自定义行吗?如果是,那么您不需要将图像单独设置到列表视图中,它将自动从您已在 int[] 中定义的可绘制对象中获取。我说话有道理吗?你明白了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多