[英]Adding to an array from a separate class
I have made a listView that is populated with an array. 我做了一个listView,其中填充了一个数组。 I can add data to this listview by entering in data in a textbox called
txtinputs
and then click a button called btnAdds
this all works fine when I have it all in a single class. 通过在名为
txtinputs
的文本框中输入数据,然后单击名为btnAdds
的按钮,可以将数据添加到此列表视图中。 However what I want to do is have this on 2 separate screens using 2 separate classes, the txtinputs
and the btnAdds
in a separate class than the listView. 但是,我想做的是在2个单独的屏幕上使用2个单独的类(在
txtinputs
和btnAdds
上)将其txtinputs
与listView分开的单独类中。 When the data is entered into the txtinputs
and the btnAdds
is selected it will add that data into the listView on a seperate class. 当将数据输入到
txtinputs
并选择了btnAdds
,它将把该数据添加到单独类的listView中。 I want the listView in a class called ListDeadlines and my textBox and Button in a class called AddDeadline. 我想要一个名为ListDeadlines的类中的listView,而想要一个名为AddDeadline的类中的textBox和Button。 I currently add the data into the listView like this.
我目前这样将数据添加到listView中。
public ArrayList<String> arrayList;
public ArrayAdapter<String> adapter;
public EditText txtInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_deadlines);
final ListView listView = (ListView)findViewById(R.id.listv);
String[] items= {"HCI","ITM","Presentation"};
arrayList=new ArrayList<>(Arrays.asList(items));
adapter=new ArrayAdapter<String>(this,R.layout.list_item,R.id.txtitem,arrayList);
listView.setAdapter(adapter);
txtInput=(EditText)findViewById(R.id.txtinputs);
Button btAdd=(Button)findViewById(R.id.btnAdds);
btAdd.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String newItem=txtInput.getText().toString();
arrayList.add(newItem);
adapter.notifyDataSetChanged();;
}
});
}
I have tried to do it like this in my AddDeadline
class like this however I get a runtime on the button click 我试图在这样的
AddDeadline
类中这样做,但是单击按钮后得到了运行时
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_deadline);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final ListDeadlines lst= new ListDeadlines();
lst.txtInput=(EditText)findViewById(R.id.txtinput);
Button btAdd=(Button)findViewById(R.id.btnAdd);
btAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newItem = lst.txtInput.getText().toString();
lst.arrayList.add(newItem);
lst.adapter.notifyDataSetChanged();
}
});
}
In your ListDeadLinesActivity
when a user hit the "+" button you want to start AddDeadlineActivity
for a result. 在您的
ListDeadLinesActivity
当用户点击“+”按钮,你要开始AddDeadlineActivity
的结果。
You can put this code inside the onClick
of that "+" button. 您可以将此代码放入该“ +”按钮的
onClick
内。
Intent intent = new Intent(this, AddDeadlineActivity.class);
startActivityForResult(intent, ADD_DEADLINE_REQUEST);
ADD_DEADLINE_REQUEST
is the request code, you can declare a static int. ADD_DEADLINE_REQUEST
是请求代码,可以声明一个静态int。
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
btAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent returnIntent = getIntent();
returnIntent.putExtra("result", yourEditText.getText().toString());
setResult(RESULT_OK,returnIntent);
finish();
}
});
}
In your ListDeadLinesActivity
override onActivityResult
method, 在您的
ListDeadLinesActivity
覆盖onActivityResult
方法中,
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ADD_DEADLINE_REQUEST){
if (resultCode == RESULT_OK) {
String item = data.getStringExtra("result");
arrayList.add(item);
adapter.notifyDataSetChanged();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.