1

I have been attempting to store data into a ListView as separate items but it just isn't working. If someone who understands Android Studio and my issue could help me I would be so helpful. I think my organization in MainActivity.java might be why it doesn't work. MainActivity.java has the ListView while NewTask.java is where the user inputs data such as the name of a task and its due date. I have used startActivityForResult() but I am still having an issue. Please help.

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();
}
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
coltsrock
  • 41
  • 6
  • Try **http://stackoverflow.com/questions/12293884/how-can-i-send-back-data-using-finish** link. – NehaK Jun 13 '15 at 16:48
  • Sorry but your link didn't help me out. For some reason when I input all of my data and click a button it does not send it to a listview despite me not seeing anything wrong with my code @NKushwah – coltsrock Jun 13 '15 at 23:47

1 Answers1

0

You don't need to get data in onActivityResults because it calles when you returns back to your activity If your activity is new in stack then you can get it's bumndle data in onCreate Method so for this put follwing code-

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

}

in 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
NehaK
  • 2,639
  • 1
  • 15
  • 31