Skip to content Skip to sidebar Skip to footer

How To Dynamically Add Items To Gridview Android Studio (java)

Hello I want to have an Add function that allows me to input items to my GridView For Background: I have a standard GridView and an XML activity (which contains 2 TextView) that I

Solution 1:

To pass data between Activities to need to do a few things:

First, when the user presses your "Add" button, you want to start the second activity in a way that allows it to return a result. this means, that instead of using startActivity you need to use startActivityForResult.

This method takes an intent and an int. Use the same intent you used in startActivity. The int should be a code that helps you identify where a result came from, when a result comes. For this, define some constant in your ActivityMain class:

privatestaticfinalint ADD_RESULT_CODE = 123;

Now, your button's click listener should looks something like this:

addButton.setOnClickListener(new OnClickListener() {  
            @Override  
            publicvoidonClick(View arg0) {  
                Intent intent=new Intent(MainActivity.this, NewFolder.class);  
                startActivityForResult(intent, ADD_RESULT_CODE);
            }  
});  

Now for returning the result. First, you shouldn't go back to your main activity by starting another intent. Instead, you should use finish() (which is a method defined in AppCompatActivity, you can use to finish your activity), this will return the user to the last place he was before this activity - ActivityMain.

And to return some data, too, you can use this code:

Intent intent=new Intent();  
intent.putExtra("title",title);  
intent.putExtra("desc",desc);  
setResult(Activity.RESULT_OK, intent); 

where title and desc are the variables you want to pass.

in your case it should look something like this:

publicclassNewFolderextendsAppCompatActivity {

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_folder_view);

    Buttonadd= (Button) findViewById(R.id.add);

    

    //If the user clicks the add button - it will save the contents to the Word Class
    add.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View view) {

            //make TextView variables and cast the contents to a string and save it to a String variableTextViewname= (TextView) findViewById(R.id.new_folder);
            Stringtitle= (String) name.getText();

            TextViewdescText= (TextView) findViewById(R.id.desc);
            Stringdesc= (String) descText.getText();

            //Save it to the Word class
            ArrayList<WordFolder> word = newArrayList<>();
            word.add(newWordFolder(title, desc));

            Intent intent=newIntent();  
            intent.putExtra("title",title);  
            intent.putExtra("desc",desc);  
            setResult(Activity.RESULT_OK, intent); 

            //goes back to the MainActivity
            finish();
        }
    });
}

You should probably also take care of the case where the user changed his mind and wants to cancel adding an item. in this case you should:

setResult(Activity.RESULT_CANCELLED); 
finish();
 

In your ActivityMain you will have the result code, and if its Activity.RESULT_OK you'll know you should add a new item, but if its Activity.RESULT_CANCELLED you'll know that the user changed their mind

Now all that's left is receiving the data in ActivityMain, and doing whatever you want to do with it (like adding it to the grid view).

To do this you need to override a method called onActivityResult inside ActivityMain:

// Call Back method  to get the Message form other Activity  @OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data)  
   {  
             super.onActivityResult(requestCode, resultCode, data);  
             // check the result code to know where the result came from//and check that the result code is OKif(resultCode == Activity.RESULT_OK && requestCode == ADD_RESULT_CODE )  
             {  
                  Stringtitle= data.getStringExtra("title");  
                  Stringdesc= data.getStringExtra("desc");  
                  //... now, do whatever you want with these variables in ActivityMain.
             }  
 }  

Post a Comment for "How To Dynamically Add Items To Gridview Android Studio (java)"