如何将项目动态添加到GridView Android Studio(Java) [英] How to dynamically add items to GridView Android Studio (Java)

查看:66
本文介绍了如何将项目动态添加到GridView Android Studio(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想拥有一个Add函数,该函数允许我将项目输入到GridView中

Hello I want to have an Add function that allows me to input items to my GridView

对于背景:我有一个标准的GridView和一个XML活动(包含2个TextView),我想将其转换为GridView.我也有一个自定义ArrayAdapter类和一个自定义Word对象(带有2个Strings变量),可以帮助我做到这一点.

For Background: I have a standard GridView and an XML activity (which contains 2 TextView) that I want to convert to my GridView. I also have a custom ArrayAdapter class and custom Word object (takes 2 Strings variables) that helps me do this.

我的问题:我想要一个添加按钮,该按钮将我带到另一个XML布局/类,并且理想情况下,它输入单个项目,因此当用户返回MainActivity时,GridView将与以前的信息一起更新我目前对atm进行了硬编码.前一句话目前无法使用

My problem: I want to have an Add button that takes me to another XML-Layout/class and IDEALLY it input a single item and so when the user goes back to MainActivity the GridView would be updated along with the previous information that I currently hard-coded atm. This previous sentence doesn't work currently

自定义ArrayAdapter和'WordFolder'是我的自定义String对象,具有2个getters

Custom ArrayAdapter and 'WordFolder' is my custom String object that has 2 getters

    //constructor - it takes the context and the list of words
    WordAdapter(Context context, ArrayList<WordFolder> word){
        super(context, 0, word);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View listItemView = convertView;
        if(listItemView == null){
            listItemView = LayoutInflater.from(getContext()).inflate(R.layout.folder_view, parent, false);
        }

        //Getting the current word
        WordFolder currentWord = getItem(position);
        //making the 2 text view to match our word_folder.xml
        TextView title = (TextView) listItemView.findViewById(R.id.title);

        title.setText(currentWord.getTitle());

        TextView desc = (TextView) listItemView.findViewById(R.id.desc);

        desc.setText(currentWord.getTitleDesc());

        return listItemView;
    }
}

这是我的NewFolder代码.将contentview设置为其他XML.因为我迷失了要做的事

Here is my NewFolder code. Which sets contentview to a different XML. it's pretty empty since I'm lost on what to do

public class NewFolder extends AppCompatActivity {

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

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

        

        //If the user clicks the add button - it will save the contents to the Word Class
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

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

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

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


                //goes back to the MainActivity
                Intent intent = new Intent(NewFolder.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }

在我的WordFolder类中,我做了一些TextView变量,并将字符串保存到我的ArrayList<>中.对象,但是到目前为止它是没有用的,因为它不与先前的ArrayList<>交互.在ActivityMain中有意义,因为它是一个全新的对象.我考虑过将ArrayList设置为全局变量,这对我来说没有意义,我现在迷路了.

In my WordFolder class I made some TextView variables and save the strings to my ArrayList<> object but so far it's been useless since it doesn't interact with the previous ArrayList<> in ActivityMain which makes sense because its an entirely new object. I thought about making the ArrayList a global variable which atm it doesn't make sense to me and I'm currently lost.

示例代码将是有意义的,但是希望对下一步的工作方向有所了解.如果需要,我可以提供其他代码.谢谢

Sample code would be appreciative but looking for a sense of direction on what to do next. I can provide other code if necessary. Thank you

推荐答案

要在Activity之间传递数据,需要做一些事情:

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

首先,当用户按下您的添加"时,按钮,您要以允许其返回结果的方式启动第二个活动.这意味着,除了使用 startActivity 之外,您还需要使用 startActivityForResult .

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.

此方法具有一个意图和一个int.使用与 startActivity 中相同的意图.int应该是可以帮助您确定结果来自何处,何时来自结果的代码.为此,请在您的 ActivityMain 类中定义一些常量:

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:

private static final int ADD_RESULT_CODE = 123;

现在,您按钮的点击侦听器应如下所示:

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

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

现在用于返回结果.首先,您不应该通过启动另一个意图来返回到您的主要活动.相反,您应该使用 finish()(这是 AppCompatActivity 中定义的方法,您可以用来 finish 您的活动),这将返回用户到此活动之前到达的最后一个位置- ActivityMain .

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

title和desc是您要传递的变量.

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

在您的情况下,它应该看起来像这样:

in your case it should look something like this:

public class NewFolder extends AppCompatActivity {

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

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

    

    //If the user clicks the add button - it will save the contents to the Word Class
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

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

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

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

            Intent intent=new Intent();  
            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();
 

在您的 ActivityMain 中,您将得到结果代码,如果它的 Activity.RESULT_OK ,您将知道应该添加一个新项目,但是如果它的Activity.RESULT_CANCELLED ,您会知道用户改变了主意

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

现在剩下的就是在ActivityMain中接收数据,并做任何您想做的事情(例如将其添加到网格视图中).

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).

为此,您需要覆盖 ActivityMain 中的称为 onActivityResult 的方法:

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

// Call Back method  to get the Message form other Activity  
@Override  
   protected void onActivityResult(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 OK
             if(resultCode == Activity.RESULT_OK && requestCode == ADD_RESULT_CODE )  
             {  
                  String title = data.getStringExtra("title");  
                  String desc = data.getStringExtra("desc");  
                  //... now, do whatever you want with these variables in ActivityMain.
             }  
 }  

这篇关于如何将项目动态添加到GridView Android Studio(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆