-2

part of the code

        package com.example.pass;

 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;




    public class Permission extends ActionBarActivity implements OnClickListener, OnKeyListener{
 EditText Date;
 EditText Time;
 EditText userName;
 EditText userSurname;
 Button btnOK;
 Button Cancel;
 EditText Name;
 String Surname;
 Button clear;
 ListView list;
 ArrayList<String> Items;
 ArrayAdapter<String> aa;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_permission);
    list = (ListView)findViewById(R.id.listView1);


    Items = new ArrayList<String>();
    aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Items);
    list.setAdapter(aa);
       // Include dialog.xml file

     // Set dialog title
    final Dialog dialog = new Dialog(Permission.this);
    dialog.setContentView(R.layout.dialog_permission);
    dialog.setTitle("Custom Dialog");

    clear = (Button)findViewById(R.id.clear);
    Button add = (Button) findViewById(R.id.add);



    // Create custom dialog object          
    // set values for custom dialog components - text, image and button
    userName = (EditText) dialog.findViewById(R.id.dialog_username);
    userSurname = (EditText)dialog.findViewById(R.id.dialog_usersurname);
    Time = (EditText)dialog.findViewById(R.id.dialog_time2);
    Date = (EditText)dialog.findViewById(R.id.dialog_date2);    
    btnOK = (Button) dialog.findViewById(R.id.OK);
    Cancel = (Button) dialog.findViewById(R.id.Cancel);
        btnOK.setOnClickListener(this);
        userName.setOnKeyListener(this);


    add.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {


           Date.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    //To show current date in the datepicker
                    Calendar mcurrentDate=Calendar.getInstance();
                    int mYear = mcurrentDate.get(Calendar.YEAR);
                    int mMonth = mcurrentDate.get(Calendar.MONTH);
                    int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

                    DatePickerDialog mDatePicker=new DatePickerDialog(Permission.this, new OnDateSetListener() {                  
                        public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
                            // TODO Auto-generated method stub                      
                            /*      Your code   to get date and time    */
                            Date.setText(selectedyear+"-"+(selectedmonth+1)+"-"+selectedday);
                        }
                    },mYear, mMonth, mDay);
                    mDatePicker.setTitle("Select date");                
                    mDatePicker.show();  }
            });


           Time.setOnClickListener(new OnClickListener() {
               @Override
               public void onClick(View v) {
                   // TODO Auto-generated method stub
                   //To show current date in the datepicker

                   Calendar mcurrentTime=Calendar.getInstance();
                   int mHour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
                   int mMinute = mcurrentTime.get(Calendar.MINUTE);


                   TimePickerDialog mTimePicker=new TimePickerDialog(Permission.this, new OnTimeSetListener() {                  
                       public void onTimeSet(TimePicker timepicker, int selectedhour, int selectedminute ) {
                           // TODO Auto-generated method stub                      
                           /*      Your code   to get date and time    */
                          if (selectedminute<10){
                              Time.setText(selectedhour+":"+"0"+(selectedminute)+""+"val");
                          }
                          else
                        Time.setText(selectedhour+":"+(selectedminute));
                       }
                   },mHour, mMinute, false);
                   mTimePicker.setTitle("Select date");                
                   mTimePicker.show();  }
           });



           Cancel.setOnClickListener(new OnClickListener() {

               @Override
               public void onClick(View v) {
                   // TODO Auto-generated method stub
                   //To show current date in the datepicker
                   dialog.dismiss();
                }
           });


        //  OK.setOnClickListener(new OnClickListener() {

            /*   @Override
               public void onClick(View v) {
                   // TODO Auto-generated method stub
                this.addItem(this.userName.getText().toString());
                   //CONVERTING THE TEXT IN TO STRING...


                   System.out.println(Name);
                }
           });*/


           clear.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

                }
           });

           dialog.show();
        }


});

}       



private void addItem(String item){
    if(item.length()>0){
        this.Items.add(item);
        this.aa.notifyDataSetChanged();
        this.userName.setText("");
    }
}

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.permission, 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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}





@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    if(event.getAction()==KeyEvent.ACTION_DOWN&&keyCode==KeyEvent.KEYCODE_DPAD_CENTER){
        this.addItem(this.userName.getText().toString());
        this.addItem(this.userSurname.getText().toString());
    }
    return false;
}





@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v == this.btnOK){
        this.addItem(this.userName.getText().toString());
        this.addItem(this.userSurname.getText().toString());
        }
    }

   }

Everytime i try to change to custom layout everything crashes, i dont know how to do it not losing dialog window, because text i am reading from is in my custom dialog. I know that maybe creating new adapter class can solve the problem but i want to try another way.

Ido Naveh
  • 2,442
  • 3
  • 26
  • 57
TheDude
  • 135
  • 4
  • 17
  • You have to write your own ListAdapter. Write a class that overrides BaseAdapter and go from there. I'd suggest doing some research to find guides on the subject. – Anubian Noob Jul 01 '15 at 23:48
  • hmmm.. i am new to android :((( – TheDude Jul 01 '15 at 23:51
  • Exactly as Anubian Noob said, there are tonnes of guides out there, and is definitely worth learning - recyclerView is actually a newer method of reusing views, and I'd suggest possibly looking into that. Everybody was new to Android at some stage - doing research, and trying is the only way to learn. If people posted just code for you to copy/paste it doesn't accomplish anything - have a good go at it, if you get stuck post your code and I'm sure people will be willing to help. – Mark Jul 01 '15 at 23:51
  • Here are the first two results from googling "android custom listadapter example": [Vogella's "Using lists in Android (ListView)"](http://www.vogella.com/tutorials/AndroidListView/article.html) and [Android Example's "How To Create A Custom Listview"](http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92). – Anubian Noob Jul 01 '15 at 23:59
  • http://stackoverflow.com/a/8166802/1576416 – Amrut Bidri Jul 02 '15 at 05:40

1 Answers1

7

Use CTRL + Left click on android.R.layout.simple_list_item_1 to open it.
Copy the contents to your own XML layout file, and edit it as your wish.
Change

android.R.layout.simple_list_item_1

To

R.layout.your_layout
Ido Naveh
  • 2,442
  • 3
  • 26
  • 57
  • Yep its ok, but u know maybe how to attribute multiply textviews in one row? I am planning to write Name,Surname,Date,Time. Now only what i get Name in one row and surname in 2nd row and ect for date and time. – TheDude Jul 02 '15 at 09:37
  • That is because it's a LinearLayout view, change the orientation, if you just want them listed after eachother. If not use a RelativeLayout and put the TextViews exactly as you want. – ASamsig Jul 02 '15 at 09:42
  • But what if i want that header will be name, and after header in the same row will be surname, date and time will be at the right side of name and surname, how manage to do that? – TheDude Jul 02 '15 at 10:12