单击保存后如何使Gridview填充?

时间:2016-10-27 07:26:10

标签: android gridview

我正在开发一个联系人应用程序,现在我一直试图从阵列中获取这些可绘制内容,在马赛克创建屏幕中单击保存马赛克按钮后,将其上传到主屏幕上的Gridview。

mosaicListScreen(主屏幕)上的浮动操作按钮(红色加按钮)指向MosaicCreationScreen。用户假设上传图像并输入马赛克名称,然后使用保存马赛克按钮进行保存,如图像中所示here

目前,在我专注于上传图片并让用户创建他们自己独特的马赛克(群组)之前,我使用一些drawable来测试Gridview更新,这些drawable在列表中列出,如下面的代码所示。

发生的问题是,一旦用户点击主屏幕上的浮动操作按钮,它就会使用MosaicCreation屏幕数组中列出的drawables更新gridview,然后转到MosaicCreationScreen,以及保存马赛克时单击MosaicCreationScreen上的按钮,意图按照预期进入主屏幕,除了gridview上没有任何内容。

因此它就像它应该在步骤中发生的事情相反。

这是我的两个屏幕的代码:

    public class mosaicsListScreen extends AppCompatActivity {

    public static mosaicsListScreen theScreen; //this variable is used in the MosaicCreationScreen to point to this screen to find the GridView by id
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        theScreen = this;
        setContentView(R.layout.activity_mosaics_list_screen);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.createMosaicButton);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(getApplicationContext(),MosaicCreationScreen.class);
                startActivity(intent);
                finish();
            }
        });
    }
}

这里是MosaicCreationScreen的代码(用户从mosaicListScreen(主屏幕)点击浮动操作按钮后打开的代码)

    public class MosaicCreationScreen extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mosaic_creation_screen);

        final GridView mosaicList = (GridView) mosaicsListScreen.theScreen.findViewById(R.id.mosaicList);
        mosaicList.setAdapter(new ImageAdapter(this)); //this line of code displays the mosaics on mosaicListScreen

        Button saveNewMosaicButton = (Button) findViewById(R.id.saveNewMosaicButton);
        saveNewMosaicButton.setOnClickListener(new AdapterView.OnClickListener() {
            @Override
            public void onClick(View view) {

                //mThumbIds.notify();

                Intent intent = new Intent(getApplicationContext(), mosaicsListScreen.class);
                startActivity(intent);
                finish();

                //mosaicList.setAdapter(new ImageAdapter(this)); //this displays the mosaics on mosaicListScreen, it logically should go here, however "this" causes an error saying ImageAdapter (android.content.Context) in ImageAdapter cannot be applied to (anonymous android.view.View.OnClickListener)
                Toast.makeText(mosaicsListScreen.theScreen, "Mosaic Created!", Toast.LENGTH_SHORT).show();
            }

        });

/*        mosaicList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(mosaicsListScreen.theScreen, "", Toast.LENGTH_SHORT).show();
            }
        });*/
    }

    public class ImageAdapter extends BaseAdapter {
        private Context mContext;

        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(mContext);
            imageView.setImageResource(mThumbIds[position]);
            return imageView;
        }

        //this array holds the drawables that would appear on the Gridview
        private Integer[] mThumbIds = {
                R.drawable.family,
                R.drawable.project
        };
    }
}

以下是布局的XML:

content_mosaics_list_screen.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="codesages.mosaic.mosaicsListScreen"
    tools:showIn="@layout/activity_mosaics_list_screen">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/create_a_mosaic_or_pick_from_the_mosaics_created"
        android:id="@+id/textView4"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textSize="20sp" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/deleteMosaicButton"
        android:src="@android:drawable/ic_menu_delete"
        android:clickable="true"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:contentDescription="" />

    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/textView4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="14dp"
        android:id="@+id/mosaicList"
        android:layout_above="@+id/textView7"
        android:numColumns="auto_fit" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="@string/holdMosaictoDeleteLabel"
        android:id="@+id/textView7"
        android:layout_marginBottom="16dp"
        android:layout_above="@+id/deleteMosaicButton"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

activity_mosaics_list_screen.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="codesages.mosaic.mosaicsListScreen">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/createMosaicButton"
        android:layout_width="56dp"
        android:layout_height="66dp"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_input_add" />

    <include layout="@layout/content_mosaics_list_screen" />

</android.support.design.widget.CoordinatorLayout>

activity_mosaic_creation_screen.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="codesages.mosaic.MosaicCreationScreen"
    android:focusable="true">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/mosaicNametextField"
        android:hint="Mosaic Name"
        android:layout_marginTop="81dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save New Mosaic"
        android:id="@+id/saveNewMosaicButton"
        android:layout_marginTop="48dp"
        android:layout_below="@+id/uploadMosaicImageButton"
        android:layout_centerHorizontal="true"
        android:enabled="true"
        android:clickable="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Upload Mosaic Image"
        android:id="@+id/uploadMosaicImageButton"
        android:layout_marginTop="68dp"
        android:layout_below="@+id/mosaicNametextField"
        android:layout_centerHorizontal="true"
        android:enabled="true"
        android:clickable="true" />
</RelativeLayout>

mosaicList.setAdapter(new ImageAdapter(this)); 那似乎是创造马赛克的东西。如果我对此发表评论,我将不会在gridview中看到任何内容。

但是,我相信应该在saveNewMosaicButton onClick中,但是我收到的错误是&#34;说ImageAdapter中的ImageAdapter(android.content.Context)无法应用于(匿名的android.view.View。 OnClickListener)&#34;

HERE是所需结果的图像示例。然而,现在发生的事情就像我所说的那样,只要点击浮动动作按钮,就会创建马赛克,然后它会将你带到创建屏幕,我们点击保存马赛克,它实际上会删除马赛克......垃圾图标的一个工作现在还不能正常运作。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

目前,你有

public static mosaicsListScreen theScreen; 

在您用于填充第一个Activity中的ListView的第一个Activity中。这是一种危险的方法,因为此变量引用的Activity实例可能会被销毁,例如,如果您在第二个Activity(例如下载图像)中使用了大量内存,但是如果用户以某种方式触发配置更改。

当您在开始第二个finish()后呼叫Activity时,您甚至会告诉系统第一个Activity可能会被销毁。你没有获得NPE的唯一原因是系统不会立即销毁完成的Activity,但一旦这样做是好主意。

总而言之,您需要一种方法将信息从一个Activity安全地传输到另一个Intent。在您的情况下,我认为您想发送所选图像的Uri(或者现在,发送所选绘图的资源ID)。两者都可以通过使用startActivityForResult()额外内容来完成。

基本上,有两种选择:

  • 使用onActivityResult()并覆盖Activity以获取第一个Activity
  • 所需的信息
  • 只要您获得结果,就可以从第二个Activity开始第一个getIntent(),并在第一个Activity中使用onCreate()(例如在ListView中)检查结果

无论您做什么,始终可以访问他们所属的Activity中的Activity等UI元素!

如果您选择第二个选项,则您的第一个public class mosaicsListScreen extends AppCompatActivity { public static final String THUMB_IDS = "someuniquestring"; private GridView mosaicList; private ArrayList<Integer> mThumbIds; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mosaics_list_screen); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.createMosaicButton); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(getApplicationContext(),MosaicCreationScreen.class); startActivity(intent); finish(); } }); fillThumbIds(); mosaicList = (GridView) findViewById(R.id.mosaicList); // Note: Adapter code in this Activity mosaicList.setAdapter(new ImageAdapter(this)); } private void fillThumbIds() { mThumbIds = new ArrayList(); // somehow get older thumb ids if necessary (from database?) // and add to ArrayList like this: mThumbIds.add(R.drawable.family); mThumbIds.add(R.drawable.project); // assuming we transmit resource id's: use an int array with the Intent int[] newThumbIds = getIntent().getIntArrayExtra(THUMB_IDS); if (newThumbIds != null) { // loop through the array to add new thumb ids for (int i = 0; i < newThumbIds.length; i++) { mThumbIds.add(newThumbIds[i]); } } } // Adapter code goes here // Note: thumbIds no longer as array but as ArrayList! } 可能如下所示:

Activity

在第二个Intent中,您将所选的缩略图ID添加为saveNewMosaicButton.setOnClickListener(new AdapterView.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(getApplicationContext(), mosaicsListScreen.class); // if 'myNewThumbs' is the int array with the new thumb ids intent.putExtra(mosaicsListScreen.THUMB_IDS, myNewThumbs); startActivity(intent); finish(); } }); ,如下所示:

{{1}}