如何解决如何将项目动态添加到GridView Android StudioJava
你好,我想有一个Add函数,它允许我将项目输入到GridView
对于背景:我有一个标准的GridView和一个XML活动(其中包含2个TextView),我想将其转换为GridView。我也有一个自定义ArrayAdapter类和一个自定义Word对象(带有2个Strings变量),可以帮助我做到这一点。
我的问题:我想要一个添加按钮,该按钮将我带到另一个XML布局/类,并且理想情况下,它输入单个项目,因此,当用户返回MainActivity时,GridView将与先前的信息一起更新我目前对atm进行了硬编码。前一句目前无法使用
自定义ArrayAdapter和'WordFolder'是我的自定义String对象,具有2个getters
//constructor - it takes the context and the list of words
WordAdapter(Context context,ArrayList<WordFolder> word){
super(context,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类中,我做了一些TextView变量并将字符串保存到我的ArrayList 对象,但是到目前为止,它没有用,因为它不与ActivityMain中的先前ArrayList 交互,这是有意义的,因为它是全新的目的。我考虑过将ArrayList设置为全局变量,这对我来说没有意义,我现在迷路了。
示例代码将是有意义的,但是希望对下一步的工作方向有所了解。如果需要,我可以提供其他代码。谢谢
解决方法
要在Activity之间传递数据,需要做一些事情:
首先,当用户按下“添加”按钮时,您要以允许其返回结果的方式启动第二个活动。这意味着,除了使用startActivity
之外,您还需要使用startActivityForResult
。
此方法具有一个意图和一个int。
使用与startActivity
中相同的意图。
int应该是可以帮助您确定结果来自何处,何时到达的代码。为此,请在您的ActivityMain
类中定义一些常量:
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);
}
});
现在用于返回结果。
首先,您不应该通过启动另一个意图返回到您的主要活动。
相反,您应该使用finish()
(这是AppCompatActivity
中定义的方法,您可以使用finish
进行活动),这会将用户返回到该活动之前的最后一个位置-ActivityMain
。
也可以使用以下代码来返回一些数据:
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中接收数据,并做任何您想做的事情(例如将其添加到网格视图中)。
为此,您需要在onActivityResult
内覆盖一个名为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.
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。