1. Are you ready for the Galaxy S20? Here is everything we know so far!

Productivity App(Help)

Discussion in 'Android Development' started by EmbededPower, Jul 27, 2017.

  1. EmbededPower

    EmbededPower Newbie
    Thread Starter

    So to learn android development I've decided to create a simple productivity app. I've decided to start with a calendar. I have it where when you select a date it displays that with a text block. What I am trying to do now is to have it so you can create in event(appointment, upcoming birthday,etc) I've looked every where but people are giving mixed answers and some of them are old. Any ideas on how I can do this and if it can be done with the built in Calendarview?

     


  2. Best Answer:
    Post #19 by Deleted User, Jul 28, 2017 (3 points)

    1. Download the Forums for Android™ app!


      Download

       
  3. Deleted User

    Deleted User Guest

  4. Deleted User

    Deleted User Guest

  5. EmbededPower

    EmbededPower Newbie
    Thread Starter

    So which one would you recommend?
     
  6. Deleted User

    Deleted User Guest

    Sorry I have no experience of any of them, just providing some resources for you to look into.
     
  7. EmbededPower

    EmbededPower Newbie
    Thread Starter

    So what if I change it up and have the events be shown in a list view and highlighted on the calendarview. Is that something I can do or no
     
  8. Deleted User

    Deleted User Guest

    Using a ListView to display the dates would work.
    I'm surprised at the limited functionality of the standard CalendarView. All it appears to do is allow you to select dates.
     
  9. EmbededPower

    EmbededPower Newbie
    Thread Starter

    Yeah its weird. Not sure why google wont allow you to add events to it. I figured the ListView way would probably be better. Can I make it so when you click on a date in the CalendarView it will take you to that event in the listview?
     
  10. Deleted User

    Deleted User Guest

    Yes that's possible, you can add a date listener callback to the CalendarView which will tell you when the selected date changed.
     
  11. EmbededPower

    EmbededPower Newbie
    Thread Starter

    Ok so i've been looking around and found https://stackoverflow.com/questions/22144891/how-to-add-listview-items-on-button-click-using-adapter On the Activity part I am stuck at:

    Code (Text):
    1. public void onItemClick(AdapterView<?> a, View v, int position,
    2.                     long id) {
    Here is my code:
    Code (Text):
    1. package com.example.johnathan.myapplication;
    2.  
    3. import android.support.v7.app.AppCompatActivity;
    4. import android.os.Bundle;
    5. import android.view.View;
    6. import android.widget.AdapterView;
    7. import android.widget.ArrayAdapter;
    8. import android.widget.Button;
    9. import android.widget.EditText;
    10. import android.widget.ListView;
    11. import android.widget.Toast;
    12.  
    13. import java.util.ArrayList;
    14.  
    15. public class MainActivity extends AppCompatActivity {
    16.     EditText editText;
    17.     EditText editText2;
    18.     Button addButton;
    19.     ListView listView;
    20.     ArrayList<String> listItems;
    21.     ArrayAdapter<String> adapter;
    22.  
    23.     @Override
    24.     protected void onCreate(Bundle savedInstanceState) {
    25.         super.onCreate(savedInstanceState);
    26.         setContentView(R.layout.activity_main);
    27.         editText = (EditText) findViewById(R.id.editText);
    28.         editText = (EditText) findViewById(R.id.editText2);
    29.         addButton = (Button) findViewById(R.id.addItem);
    30.         listView = (ListView) findViewById(R.id.listView);
    31.         listItems = new ArrayList<String>();
    32.         listItems.add("First Item - added on Activity Create");
    33.         adapter = new ArrayAdapter<String>(this,
    34.                 android.R.layout.simple_list_item_1, listItems);
    35.         listView.setAdapter(adapter);
    36.         addButton.setOnClickListener(new View.OnClickListener() {
    37.             @Override
    38.             public void onClick(View view) {
    39.                 listItems.add(editText.getText().toString());
    40.                 listItems.add(editText2.getText().toString());
    41.                 adapter.notifyDataSetChanged();
    42.             }
    43.         });
    44.         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    45.             @Override
    46.             public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    47.                 long id) {
    48.                     Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_LONG)
    49.                             .show();
    50.                 }
    51.                 }
    52.             }
    53.         });
    54. }
    55.  
    Im not sure why it looks different when I am using the "OnItemClickListener"

    Any help would be appreciated. Thanks
     
  12. Deleted User

    Deleted User Guest

    What do you mean by "looks different"? The only difference is the parameter names used in the method definition. It's functionally the same code.
     
  13. EmbededPower

    EmbededPower Newbie
    Thread Starter

    It's not liking the "long id) {"
     
  14. Deleted User

    Deleted User Guest

    What's the actual error?
     
  15. EmbededPower

    EmbededPower Newbie
    Thread Starter

    I've uploaded a screenshot for you.
     

    Attached Files:

  16. Deleted User

    Deleted User Guest

    You have too many close brackets in this block

    Code (Text):
    1.  
    2. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    3.             @Override
    4.             public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    5.                 long id) {
    6.                     Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_LONG)
    7.                             .show();
    8.                 }
    9.                 }
    10.             }
    11.         });
    12.  
    There are 3 "{" but 4 "}" Remove the one at line 9 above.
     
  17. EmbededPower

    EmbededPower Newbie
    Thread Starter

    So i removed that one and it's the same thing :/
     
  18. Deleted User

    Deleted User Guest

    Post up your current code. I'll paste it into Android Studio and tell you what your compile problem is.
     
  19. EmbededPower

    EmbededPower Newbie
    Thread Starter

    MainActivity.java
    Code (Text):
    1. package com.example.johnathan.myapplication;
    2.  
    3. import android.support.v7.app.AppCompatActivity;
    4. import android.os.Bundle;
    5. import android.view.View;
    6. import android.widget.AdapterView;
    7. import android.widget.ArrayAdapter;
    8. import android.widget.Button;
    9. import android.widget.EditText;
    10. import android.widget.ListView;
    11. import android.widget.Toast;
    12.  
    13. import java.util.ArrayList;
    14.  
    15. public class MainActivity extends AppCompatActivity {
    16.     EditText editText;
    17.     EditText editText2;
    18.     Button addButton;
    19.     ListView listView;
    20.     ArrayList<String> listItems;
    21.     ArrayAdapter<String> adapter;
    22.  
    23.     @Override
    24.     protected void onCreate(Bundle savedInstanceState) {
    25.         super.onCreate(savedInstanceState);
    26.         setContentView(R.layout.activity_main);
    27.         editText = (EditText) findViewById(R.id.editText);
    28.         editText = (EditText) findViewById(R.id.editText2);
    29.         addButton = (Button) findViewById(R.id.addItem);
    30.         listView = (ListView) findViewById(R.id.listView);
    31.         listItems = new ArrayList<String>();
    32.         listItems.add("First Item - added on Activity Create");
    33.         adapter = new ArrayAdapter<String>(this,
    34.                 android.R.layout.simple_list_item_1, listItems);
    35.         listView.setAdapter(adapter);
    36.         addButton.setOnClickListener(new View.OnClickListener() {
    37.             @Override
    38.             public void onClick(View view) {
    39.                 listItems.add(editText.getText().toString());
    40.                 listItems.add(editText2.getText().toString());
    41.                 adapter.notifyDataSetChanged();
    42.             }
    43.         });
    44.         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    45.             @Override
    46.             public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    47.                 long id) {
    48.                     Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_LONG)
    49.                             .show();
    50.                 }
    51.  
    52.             }
    53.         });
    54.  
    55.     }
    activity_main.xml
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     xmlns:app="http://schemas.android.com/apk/res-auto"
    4.     xmlns:tools="http://schemas.android.com/tools"
    5.     android:layout_width="match_parent"
    6.     android:layout_height="match_parent"
    7.     tools:context="com.example.johnathan.myapplication.MainActivity">
    8.  
    9.     <EditText
    10.         android:id="@+id/editText"
    11.         android:layout_width="wrap_content"
    12.         android:layout_height="wrap_content"
    13.         android:ems="10"
    14.         android:inputType="textPersonName"
    15.         android:text="Name"
    16.         tools:layout_editor_absoluteY="0dp"
    17.         tools:layout_editor_absoluteX="0dp" />
    18.  
    19.     <EditText
    20.         android:id="@+id/editText2"
    21.         android:layout_width="wrap_content"
    22.         android:layout_height="wrap_content"
    23.         android:ems="10"
    24.         android:inputType="textPersonName"
    25.         android:text="Name"
    26.         tools:layout_editor_absoluteY="43dp"
    27.         tools:layout_editor_absoluteX="0dp" />
    28.  
    29.     <Button
    30.         android:id="@+id/addItem"
    31.         android:layout_width="wrap_content"
    32.         android:layout_height="wrap_content"
    33.         android:text="Button"
    34.         tools:layout_editor_absoluteX="215dp"
    35.         tools:layout_editor_absoluteY="106dp" />
    36.  
    37.     <ListView
    38.         android:id="@+id/listView"
    39.         android:layout_width="294dp"
    40.         android:layout_height="209dp"
    41.         tools:layout_editor_absoluteX="9dp"
    42.         tools:layout_editor_absoluteY="187dp" />
    43.  
    44. </android.support.constraint.ConstraintLayout>
    45.  
    _main.xml
     
  20. Deleted User

    Deleted User Guest

    I corrected the syntax errors. Study the differences between your original code, and understand why it didn't work. Parentheses must match up.

    Code (Text):
    1.  
    2. import android.support.v7.app.AppCompatActivity;
    3. import android.os.Bundle;
    4. import android.view.View;
    5. import android.widget.AdapterView;
    6. import android.widget.ArrayAdapter;
    7. import android.widget.Button;
    8. import android.widget.EditText;
    9. import android.widget.ListView;
    10. import android.widget.Toast;
    11.  
    12. import java.util.ArrayList;
    13.  
    14. public class MainActivity extends AppCompatActivity {
    15.     EditText editText;
    16.     EditText editText2;
    17.     Button addButton;
    18.     ListView listView;
    19.     ArrayList<String> listItems;
    20.     ArrayAdapter<String> adapter;
    21.  
    22.     @Override
    23.     protected void onCreate(Bundle savedInstanceState) {
    24.         super.onCreate(savedInstanceState);
    25.         setContentView(R.layout.activity_main);
    26.         editText = (EditText) findViewById(R.id.editText);
    27.         editText = (EditText) findViewById(R.id.editText2);
    28.         addButton = (Button) findViewById(R.id.addItem);
    29.         listView = (ListView) findViewById(R.id.listView);
    30.         listItems = new ArrayList<String>();
    31.         listItems.add("First Item - added on Activity Create");
    32.         adapter = new ArrayAdapter<String>(this,
    33.                 android.R.layout.simple_list_item_1, listItems);
    34.         listView.setAdapter(adapter);
    35.         addButton.setOnClickListener(new View.OnClickListener() {
    36.             @Override
    37.             public void onClick(View view) {
    38.                 listItems.add(editText.getText().toString());
    39.                 listItems.add(editText2.getText().toString());
    40.                 adapter.notifyDataSetChanged();
    41.             }
    42.         });
    43.         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    44.             @Override
    45.             public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    46.                 Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_LONG)
    47.                         .show();
    48.             }
    49.  
    50.         });
    51.  
    52.     }
    53. }
    54.  
     
  21. EmbededPower

    EmbededPower Newbie
    Thread Starter

    So i basically didn't need the long id part at all.
     
  22. EmbededPower

    EmbededPower Newbie
    Thread Starter

    So it now runs but when I try to add something, i cant even select the second text field and when I hit the add button it crashes
     
  23. Deleted User

    Deleted User Guest

  24. Deleted User

    Deleted User Guest

    Question: What's wrong with this line?

    editText = (EditText) findViewById(R.id.editText2);
     
    sweetndreemy73 likes this.
Loading...

Share This Page

Loading...