如何动态添加项目到 GridView Android Studio (Java)

How to dynamically add items to GridView Android Studio (Java)

你好,我想要一个添加功能,允许我将项目输入到我的 GridView

对于背景:我有一个标准的 GridView 和一个 XML activity(其中包含 2 个 TextView),我想将其转换为我的 GridView。我还有一个自定义 ArrayAdapter class 和自定义 Word 对象(采用 2 个字符串变量)来帮助我执行此操作。

我的问题:我想要一个将我带到另一个 XML-Layout/class 的添加按钮,理想情况下它输入一个项目,因此当用户返回 MainActivity 时,GridView 将与之前的一起更新我目前硬编码 atm 的信息。前一句目前无效

Custom ArrayAdapter 和 'WordFolder' 是我的自定义 String 对象,它有 2 个 getter

    //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。它很空,因为我不知道该做什么

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 class 中,我制作了一些 TextView 变量并将字符串保存到我的 ArrayList<> 对象中,但到目前为止它没有用,因为它没有与 ActivityMain 中之前的 ArrayList<> 交互,这使得有意义,因为它是一个全新的对象。我考虑过让 ArrayList 成为一个全局变量,atm 它对我来说没有意义,我现在迷路了。

示例代码将不胜感激,但正在寻找下一步该做什么的方向感。如有必要,我可以提供其他代码。谢谢

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

首先,当用户按下您的“添加”按钮时,您希望以允许其return 结果的方式启动第二个 activity。这意味着,您需要使用 startActivityForResult.

而不是使用 startActivity

此方法接受一个意图和一个整数。 使用与 startActivity 中相同的意图。 int 应该是一个代码,可以帮助您识别结果的来源和结果的时间。为此,在 ActivityMain class:

中定义一些常量
private static final int ADD_RESULT_CODE = 123;

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

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

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

还有return一些数据,你也可以使用这个代码:

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

其中 title 和 desc 是您要传递的变量。

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

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

您可能还应该处理用户改变主意并想要取消添加项目的情况。在这种情况下你应该:

setResult(Activity.RESULT_CANCELLED); 
finish();
 

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

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

为此,您需要在 ActivityMain:

中覆盖一个名为 onActivityResult 的方法
// 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.
             }  
 }