我一直在尝试将数据存储到ListView
作为单独的项目,但它只是无法正常工作。如果有人了解Android Studio和我的问题可以帮助我,我会非常乐于助人。我认为我在MainActivity.java中的组织可能是它不起作用的原因。 MainActivity.java具有ListView
,而NewTask.java
是用户输入数据的位置,例如任务名称及其截止日期。我使用过startActivityForResult()
,但我仍然遇到问题。请帮忙。
MainActivity.java
package com.example.shaan.todoer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Things still left to do:
//Prevent Overriding of Items
//Activate Delete Tasks on Hard Click of Items
//Fit Name, Date, and Priority on One Line of List
//Attempt to be able to sort items in Listview
Button firstButton = (Button) findViewById(R.id.add_task_group);
firstButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, CalendarViewActivity.class));
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, list);
ListView listViewToDo = (ListView) findViewById(R.id.listViewToDo);
listViewToDo.setAdapter(adapter);
Intent i = new Intent(this, NewTask.class);
startActivityForResult(i, 1);
if(requestCode == 1) {
if(resultCode == RESULT_OK) {
String str = data.getStringExtra("type_task_name");
String str1 = data.getStringExtra("text_date");
int seek = data.getIntExtra("seekBar", 0);
adapter.add(str);
}
adapter.notifyDataSetChanged();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
NewTask.java
package com.example.shaan.todoer;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class NewTask extends Activity implements OnClickListener {
Button set_date;
private int calYear, calMonth, calDay;
private SeekBar slider;
private TextView sliderLevel;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_task);
initialize();
Button cancel_new_task = (Button) findViewById(R.id.cancel_new_task);
cancel_new_task.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(NewTask.this, MainActivity.class));
}
});
sliderLevel.setText("Priority Level: " + slider.getProgress() + "/" + slider.getMax());
slider.setOnClickListener(this);
slider.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int work = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
work = progress;
Toast.makeText(getApplicationContext(), "Priority Level is Changing", Toast.LENGTH_SHORT);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(getApplicationContext(), "Started tracking Slider", Toast.LENGTH_SHORT);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
sliderLevel.setText("Priority Level: " + work + "/" + slider.getMax());
Toast.makeText(getApplicationContext(), "Stopped tracking Slider", Toast.LENGTH_SHORT);
}
});
}
private void initialize() {
slider = (SeekBar) findViewById(R.id.seekBar);
sliderLevel = (TextView) findViewById(R.id.textView7);
set_date = (Button) findViewById(R.id.set_date);
text_date = (EditText) findViewById(R.id.text_date);
set_date.setOnClickListener(this);
text_date.setOnClickListener(this);
create_task = (Button) findViewById(R.id.create_task);
type_task_name = (EditText) findViewById(R.id.type_task_name);
seekBar = (SeekBar) findViewById(R.id.seekBar);
text_date = (EditText) findViewById(R.id.text_date);
}
Button create_task;
EditText type_task_name;
SeekBar seekBar;
EditText text_date;
@Override
public void onClick(View v) {
if (v == set_date) {
final Calendar cal = Calendar.getInstance();
calYear = cal.get(Calendar.YEAR);
calMonth = cal.get(Calendar.MONTH);
calDay = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog date = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker look, int year, int month, int day) {
text_date.setText((month + 1) + "/" + (day) + "/" + year);
}
}, calYear, calMonth, calDay);
date.show();
}
}
public void buttonAddClick(View v) {
startActivity(new Intent(NewTask.this, MainActivity.class));
Intent i = new Intent();
i.putExtra("type_task_name", type_task_name.getText().toString());
i.putExtra("text_date", text_date.getText().toString());
i.putExtra("seekBar", seekBar.getProgress());
setResult(RESULT_OK, i);
finish();
}
}
答案 0 :(得分:0)
你不需要在onActivityResults中获取数据,因为当你返回到你的活动时它会被调用如果你的活动在堆栈中是新的,那么你可以在onCreate方法中得到它的bumndle数据,所以这个下面的代码 -
NewTask.java
public void buttonAddClick(View v) {
Intent i = new Intent(NewTask.this, MainActivity.class);
i.putExtra("type_task_name", type_task_name.getText().toString());
i.putExtra("text_date", text_date.getText().toString());
i.putExtra("seekBar", seekBar.getProgress());
startActivity(i);
}
MainActivity.java中的
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// your data from which Activity you called it.
Bundle data = getIntent().getExtras();
if (data != null) {
String str = data.getString("type_task_name");
String str1 = data.getString("text_date");
int value = data.getInt("seekBar");
}
// now do whatever you want to do with this data