Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: I having problems adding the value into the list as viewlist after input name and submit ok Hi guys, i'm currently developing an counter app however i was having trouble Working with ListView in Android. Customize ListView, Add item via a Button Clic

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I having problems adding the value into the list as viewlist after input name and submit ok Hi guys, i'm currently developing an counter app however i was having trouble Working with ListView in Android. Customize ListView, Add item via a Button Clic

    Hey, i'm currently developing an counter app however i was having trouble Working with ListView in Android. Customize ListView, Add item via a Button Click. And also clickable each button in each row. here is my code note spinner 1 (sub view1) is works only for testing purpose spinner 2 (subview2) is where i'm having trouble adding the value into the list with a counter while creating. after that it then allows browsing of a list to retrieve and earlier count and optionally continue that count and prompts for count name on start of a new tally.

    package com.example.itallyassigment;
     
    //import android.media.MediaPlayer;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
     
    import android.media.AudioManager;
    import android.media.SoundPool;
    import android.os.Build;
    import android.os.Bundle;
    import android.annotation.TargetApi;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.text.Editable;
    import android.text.InputFilter;
    import android.text.InputType;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.Spinner;
    import android.widget.TextView;
    import android.widget.Toast;
     
    public class Itallyassigment extends Activity implements OnClickListener {
     
     
    	// to play while the user clicks the button
    	// (http://www.youtube.com/watch?v=KFw765mBcak)
    	// heard the soundpool method was popular than mediaplyer method so i may
    	// choose either mediaplayer or soundpool just to test the playback.
    	// media player method - use for music player or video games etc
    	// Setting up the Mediplayer
    	// MediaPlayer incrementButtonClick;
    	// MediaPlayer decrementButtonClick;
    	// MediaPlayer refreshButtonClick;
    	// Setting up the SoundPool
    	// Soundpool method- use it for sound effects
    	// (http://www.newyyz.com/ntgsite/2012/05/android-using-the-mediaplayer-for-online-streams/)
    	SoundPool soundPool;
    	int incrementButtonClick = -1;
    	int decrementButtonClick = -1;
    	int refreshButtonClick = -1;
     
    	// Setting Max value
    	public int MAX_VALUE = 9999;
    	// Setting min value
    	public int MIN_VALUE = 0;
    	// setting default value as min value where the numbers cannot reach to
    	// negative number
    	public int DEFAULT_VALUE = MIN_VALUE;
    	// Initial the number value
    	public int counterValue = DEFAULT_VALUE;
    	// To convert int into string to output the value
    	//String strcounter;
    	public TextView text;
     
    	public ArrayAdapter<String> dataAdapter;
     
    	 private Spinner spinner1;
     
    	 public Dialog source;
     
    	// This function is called when the application starts
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		// Alternative way to create the methods
    		init();
    		openCount();
    		addItemsOnSpinner1();
     
    	}
     
    	public void addItemsOnSpinner1() {
     
    		spinner1 = (Spinner) findViewById(R.id.spinner1);
    		List<String> list = new ArrayList<String>();
    		list.add("list 1");
    		list.add("list 2");
    		list.add("list 3");
    		dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list);
    		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    		spinner1.setAdapter(dataAdapter);
    	  }
     
    	// This function reads the stored value from a file on the device
    	//http://www.mybringback.com/tutorial-series/12637/android-internal-storage-with-fileoutputstream/
    	//http://www.kodejava.org/examples/214.html
    	private void openCount() {
    		// TODO Auto-generated method stub
    		try {
        		// Open a file and data stream
            	FileInputStream out = openFileInput("count.bin");
            	DataInputStream data = new DataInputStream(out);
     
            	// Read the binary value from the stream and store in the integer
            	counterValue = data.readInt();
     
            	// Remember to close the streams
            	data.close();
            	out.close();
        	}
        	catch (FileNotFoundException err) {
        		// Ignore this error, since is should just mean that the program is run for the first time
        	}
        	catch (IOException err) {
        		showError(err);
        	}
    	}
     
    	public void init() {
    		// TODO Auto-generated method stub
    		// Connect interface elements to properties
    		Button increments = (Button) findViewById(R.id.increments);
    		Button decrement = (Button) findViewById(R.id.decrement);
    		Button reset = (Button) findViewById(R.id.reset);
    		text = (TextView) findViewById(R.id.textView1);
    		text.setText(Integer.toString(counterValue));
     
     
    		// Setup ClickListeners
    		increments.setOnClickListener(this);
    		decrement.setOnClickListener(this);
    		reset.setOnClickListener(this);
     
     
    		// set up the button sound and clicklisteners
    		setVolumeControlStream(AudioManager.STREAM_MUSIC);
    		soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
    		incrementButtonClick = soundPool.load(this, R.raw.increment_sound, 1);
    		decrementButtonClick = soundPool.load(this, R.raw.decrement_sound, 1);
    		refreshButtonClick = soundPool.load(this, R.raw.refresh_sound, 1);
     
    		// Mediaplayer method and clicklisteners
    		// incrementButtonClick = MediaPlayer.create(this,R.raw.increment_sound);
    		// decrementButtonClick = MediaPlayer.create(this,R.raw.decrement_sound);
    		// refreshButtonClick = MediaPlayer.create(this,R.raw.refresh_sound);
     
    	}
     
     
    	// Begin method to view for + and - button to able to perform function when
    	// the user touches
    	public void onClick(View v) {
    		switch (v.getId()) {
    		// Increase counter by one, update value in user interface and on disk
    		case R.id.increments:
    			if (counterValue < MAX_VALUE) {
    				counterValue++;
    				//strcounter = Integer.toString(counterValue);
    				//text.setText(String.valueOf(strcounter));
    				text.setText(Integer.toString(counterValue));
    				// Starting the sound
    				//Mediaplayer method
    				// incrementButtonClick.start();
    				saveCount();
    				soundPool.play(incrementButtonClick, 1, 1, 0, 0, 1);
    			}
    			break;
    		case R.id.decrement:
    			// Decrease counter by one, update value in user interface and on
    			// disk
    			if (counterValue > MIN_VALUE) {
    				counterValue--;
    				//strcounter = Integer.toString(counterValue);
    				//text.setText(String.valueOf(strcounter));
    				text.setText(Integer.toString(counterValue));
    				// Starting the sound
    				//mediaplayer method
    				// decrementButtonClick.start();
    				saveCount();
    				soundPool.play(decrementButtonClick, 1, 1, 0, 0, 1);
    			}
    			break;
    		case R.id.reset:
    			counterValue = 0;
    			//strcounter = Integer.toString(counterValue);
    			text.setText(Integer.toString(counterValue));
    			// starting the sound
    			//mediaplayer method
    			// refreshButtonClick.start();
    			soundPool.play(refreshButtonClick, 1, 1, 0, 0, 1);
    		}
     
    	}
     
    	// This function stores the current value in a file on the device
    	//http://www.mybringback.com/tutorial-series/12637/android-internal-storage-with-fileoutputstream/
        void saveCount() {
        	try {
        		// Open a file and data stream
            	FileOutputStream out = openFileOutput("count.bin", MODE_PRIVATE);
            	DataOutputStream data = new DataOutputStream(out);
     
            	// Write the binary value of the integer to the stream
            	data.writeInt(counterValue);
     
            	// Remember to close the streams
            	data.close();
            	out.close();
        	}
        	catch (FileNotFoundException err) {
        		showError(err);
        	}
        	catch (IOException err) {
        		showError(err);
        	}
        }
     
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		//getMenuInflater().inflate(R.menu.main, menu);
    		// Add two commands to the menu. We only care about the unique id, 0 or 1, and the text for the items
        	menu.add(Menu.NONE, 0, Menu.NONE, R.string.menu_add);
        	menu.add(Menu.NONE, 1, Menu.NONE, R.string.menu_edit);
        	menu.add(Menu.NONE, 2, Menu.NONE, R.string.menu_delete);
        	return true;
    	}
     
    	// This function is called when the user has chosen a menu item created in
    	// onCreateOptionsMenu
    	@Override
    	public boolean onOptionsItemSelected(MenuItem item) {
    		if (item.getItemId() == 0) {
    			AlertDialog.Builder addDialogBuilder = new AlertDialog.Builder(this);
    			addDialogBuilder.setTitle(getResources().getText(
    					R.string.dialog_add_title));
     
    			Context context = addDialogBuilder.getContext();
    			LinearLayout layout = new LinearLayout(context);
    			layout.setOrientation(LinearLayout.VERTICAL);
     
    			// Name input label
    			TextView nameInputLabel = new TextView(this);
    			nameInputLabel.setText(getResources().getText(
    					R.string.dialog_edit_name));
    			layout.addView(nameInputLabel);
     
     
     
    			// Name input
    			 EditText nameInput = (EditText) source.findViewById(R.id.spinner2);
    			nameInput.setInputType(InputType.TYPE_CLASS_TEXT);
    			nameInput.setText("");
    			layout.addView(nameInput);
     
     
    			addDialogBuilder.setView(layout).setPositiveButton("Add",
    					new DialogInterface.OnClickListener() {
    						public void onClick(DialogInterface dialog,
    								int whichButton) {						
    							// Do something with value!
     
    							String name = nameInput.getText().toString();
                                if (name.equals("")) {
                                    Toast.makeText(getBaseContext(),
                                            getResources().getText(R.string.toast_no_name_message),
                                            Toast.LENGTH_SHORT).show();
                                }
     
     
                                dataAdapter.add(name);
                                //spinner1 = (Spinner) findViewById(R.id.spinner1);
     
    							}
    						}
    					);
    			addDialogBuilder.setNegativeButton("Cancel",
    					new DialogInterface.OnClickListener() {
    						public void onClick(DialogInterface dialog,
    								int whichButton) {
    							// Canceled.
    						}
    					});
     
    			addDialogBuilder.show();
     
    		}
    		if (item.getItemId() == 1) {
    			AlertDialog.Builder addDialogBuilder = new AlertDialog.Builder(this);
    			addDialogBuilder.setTitle(getResources().getText(
    					R.string.dialog_edit_title));
     
    			Context context = addDialogBuilder.getContext();
    			LinearLayout layout = new LinearLayout(context);
    			layout.setOrientation(LinearLayout.VERTICAL);
     
    			// Name input label
    			TextView nameInputLabel = new TextView(this);
    			nameInputLabel.setText(getResources().getText(
    					R.string.dialog_edit_name));
    			layout.addView(nameInputLabel);
     
    			// Name input
    			final EditText nameInput = new EditText(this);
    			nameInput.setInputType(InputType.TYPE_CLASS_TEXT);
    			nameInput.setText("");
    			layout.addView(nameInput);
     
     
    			addDialogBuilder.setView(layout).setPositiveButton("Edit",
    					new DialogInterface.OnClickListener() {
    						public void onClick(DialogInterface dialog,
    								int whichButton) {						
    							// Do something with value!
    							String name = nameInput.getText().toString();
                                if (name.equals("")) {
                                    Toast.makeText(getBaseContext(),
                                            getResources().getText(R.string.toast_no_name_message),
                                            Toast.LENGTH_SHORT).show();
                                }
     
     
    							}
    						}
    					);
    			addDialogBuilder.setNegativeButton("Cancel",
    					new DialogInterface.OnClickListener() {
    						public void onClick(DialogInterface dialog,
    								int whichButton) {
    							// Canceled.
    						}
    					});
     
    			addDialogBuilder.show();
    		}
     
    		return true;
     
    	}
     
     
    	// This function is called to show the error message of an exception in an alert dialog
        void showError(Exception err) {
        	String msg = getString(R.string.file_error, err.getLocalizedMessage());
    		new AlertDialog.Builder(this).setMessage(msg).show();
        }
     
    }
    Untitled.png


  2. #2
    Member coderxx0's Avatar
    Join Date
    Feb 2013
    Location
    England, UK
    Posts
    61
    My Mood
    Cool
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: I having problems adding the value into the list as viewlist after input name and submit ok Hi guys, i'm currently developing an counter app however i was having trouble Working with ListView in Android. Customize ListView, Add item via a Button

    DOUBLE POST

    CLICK!

Similar Threads

  1. Replies: 4
    Last Post: March 21st, 2013, 08:52 AM
  2. Replies: 0
    Last Post: March 20th, 2013, 07:07 PM
  3. Replies: 0
    Last Post: March 20th, 2013, 07:02 PM
  4. Problem developing app for Android
    By jume28 in forum JDBC & Databases
    Replies: 1
    Last Post: June 4th, 2012, 05:26 PM