如何从另一个活动中的用户输入中获取要在列表视图上显示的项目?

时间:2015-06-12 04:54:51

标签: java android listview android-listview

出于某种原因,当我在newtask.java中输入数据并单击创建任务时,它不会在列表视图上为MainActivity创建项目。

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;

ListView listViewToDo;



protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    listViewToDo = (ListView) findViewById(R.id.listViewToDo);
    Intent i = new Intent(this, NewTask.class);

    startActivityForResult(i, 1);

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, list);
    listViewToDo.setAdapter(adapter);



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

            list.add(str + " | " + str1 + " | " + seek); //The add method does not work. It says, "Cannot Resolve Method (java.lang.String)
        }
        adapter.notifyDataSetChanged();
        }
    }

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

    listViewToDo = (ListView) findViewById(R.id.listViewToDo);
    //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));
        }
    });
}

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

    Button create_task = (Button) findViewById(R.id.create_task);
    create_task.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(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();
        }
    });

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


}

我是Android的新手。

1 个答案:

答案 0 :(得分:1)

以startActivityForResult开始第二个活动,并使用setResult()方法将数据从Activity2发送回Activity1。在activity1中,您需要覆盖onActivityResult以使用Activity2中的EditText数据更新TextView

例如:

从Acivity1启动Activity2为:

Intent i = new Intent(this,  Activity2.class);
startActivityForResult(i, 1);
Activity2中的

使用setResult来发回数据:

Intent intent = new Intent();
intent.putExtra("edittextvalue","value_here");
setResult(RESULT_OK, intent);
finish();

并在First Activity中接收数据为onActivityResult:

onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 1) {
     if(resultCode == RESULT_OK){
      String stredittext=data.getStringExtra("edittextvalue");

//list.add(stredittext); for Adding a data to listview  and  notifydataset changed by listview.notifydatasetchanged();

}
}