I'm having 2 activities in my project where 1st activity has a list view and a button and 2nd activity has EditText and other info and a Button. i want to to pass data from 2nd activity when the user enters details to 1st activity's ListView. Here when I'm entering the data it is not getting displayed in the ListView.. what is the problem ?
Main Activity :-
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ListView lv = (ListView) findViewById(R.id.theListView);
if(requestCode==1) {
if (resultCode == RESULT_OK) {
String det_rec = data.getStringExtra("Details");
ArrayList<String> strArr = new ArrayList<String>();
for(int i=0;i<det_rec.length();i++) {
strArr.add("Row :" + i);
}
ListAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, strArr);
lv.setAdapter(adapter);
}
if (resultCode == RESULT_CANCELED) {
return;
}
}
// ListView lv = (ListView) findViewById(R.id.theListView);
// String det_rec = data.getStringExtra("Details");
// ArrayList<String> strArr = new ArrayList<String>();
// for(int i=0;i<det_rec.length();i++){
// strArr.add("Row :"+i);
//}
//ListAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, strArr);
// lv.setAdapter(adapter);
}
}
Second Activity :-
public void add_usr_tsk_btn(View view){
EditText et = (EditText)findViewById(R.id.task_name_edit_txt);
String detls = String.valueOf(et.getText());
Intent goback = getIntent();
goback.putExtra("Details",detls);
setResult(RESULT_OK,goback);
finish();
}
public void cancl_btn(View view) {
Intent goBack = getIntent();
finish();
}
}
activity main :-
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="left"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/theListView">
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Task"
android:id="#+id/button1"
android:background="#drawable/addtask"
android:onClick="onAddCLick"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/shwmap"
android:text="Show Map"
android:id="#+id/button2"
android:onClick="onMapbtnClck"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:id="#+id/button3"
android:onClick="onSetngClck"
/>
</LinearLayout>
second activity :-
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="left">
<EditText
android:layout_width="364dp"
android:layout_height="wrap_content"
android:id="#+id/task_name_edit_txt"
android:hint="Enter your Details" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Set Location"
android:id="#+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Set_loc_btn"
android:onClick="set_usr_loc"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Add_task_btn"
android:onClick="add_usr_tsk_btn"
android:id="#+id/add_task"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cancel_btn"
android:onClick="cancl_btn"
android:id="#+id/cancel_btn"/>
</LinearLayout>
</LinearLayout>
you pass your data with following code:
goback.putExtra("Details",detls);
and you got that with following code:
String det_rec = data.getStringExtra("details");
as you see Details is not same as details, change one of them ,
as you use onActivityResult you must check requestCode and resultCode, for using that field you can read
This
i think you need read creating list tutorial, you can start with This, because i this implementation not worked at all:
ListAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Integer.parseInt(det_rec));
you must pass one ArrayList and in your code ArrayList<String> to your adapter instead of Integer.parseInt(det_rec)
Related
I have a little problem in my android app. I want to put a word in an AutoCompleteTextView and then jump to a specific Activity(depend on the word that the user gave).
The problem is that, when i give a specific word, the program does not correspond with the correct answer as expected but returns a wrong toast message. However with the log that i put i see the correct answer. I think that the solution is silly but i stuck and need to solve this.
the code:
Activity.java
public class paralActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paral);
final String [] temp = {"one","two","three"};
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
actv.clearListSelection();
final RelativeLayout myRelative = (RelativeLayout) findViewById(R.id.find);
myRelative.setVisibility(View.INVISIBLE);
ImageView myImage = (ImageView) findViewById(R.id.aktoploika);
myImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(paralActivity.this, topParalActivity.class);
startActivity(intent);
}
});
ImageView myOtherImage = (ImageView) findViewById(R.id.aeroporika);
myOtherImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myRelative.setVisibility(View.VISIBLE);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(paralActivity.this, android.R.layout.select_dialog_item, temp);
//Getting the instance of AutoCompleteTextView
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
actv.setThreshold(1);//will start working from first character
actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
actv.setTextColor(Color.RED);
ImageView findBeach = (ImageView) findViewById(R.id.find_beach);
findBeach.setOnClickListener(new View.OnClickListener() {
#Override
** public void onClick(View view) {
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
String choice = actv.getText().toString();
Log.i("Answer",choice);
if (choice == "one"){
Intent firstIntent = new Intent(paralActivity.this, nauagioActivity.class);
startActivity(firstIntent);
}else if (choice == temp[1]){
Intent secondIntent = new Intent(paralActivity.this, gerakasActivity.class);
startActivity(secondIntent);
}else if (choice == temp[2]){
Intent thirdIntent = new Intent(paralActivity.this, limnionasActivity.class);
startActivity(thirdIntent);
}else{
Toast.makeText(paralActivity.this,"wrong",Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}
To help u understand, in this activity a have 2 imageViews. When the user presses the second, a relativeLayout appears (with an AutoCompleteTextView and a button inside). After the user writes the word when presses the button it must go to the specific activity. I declared a String Array (temp[3]) with three words inside and 3 activities for each of the words.
The problem starts in the last onclick method that i put ** . Every time i put a correct word from the Array i take the Toast message but in the log i see the correct.
here is Activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_paral"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/main"
android:scaleX="2"
tools:context="com.example.billy.zakynthosapp.paralActivity">
<TextView
android:id="#+id/categories"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="#+id/welcome"
android:text="#string/categories"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:textSize="11sp"
android:textStyle="bold"
android:textColor="#color/welcome"
android:textAllCaps="true"/>
<ImageView
android:id="#+id/aktoploika"
android:layout_centerHorizontal="true"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_below="#id/categories"
android:layout_marginTop="35dp"
android:scaleType="centerCrop"
android:src="#drawable/par1" />
<TextView
android:id="#+id/aktoploika_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/aktoploika"
android:layout_alignTop="#+id/aktoploika"
android:layout_alignRight="#+id/aktoploika"
android:layout_alignBottom="#+id/aktoploika"
android:layout_margin="1dp"
android:gravity="center"
android:text="#string/paralies"
android:textSize="22sp"
android:textColor="#color/categories"
android:textStyle="bold"/>
<ImageView
android:id="#+id/aeroporika"
android:layout_centerHorizontal="true"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_below="#id/aktoploika"
android:layout_marginTop="35dp"
android:scaleType="centerCrop"
android:src="#drawable/par2" />
<TextView
android:id="#+id/aeroporika_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/aeroporika"
android:layout_alignTop="#+id/aeroporika"
android:layout_alignRight="#+id/aeroporika"
android:layout_alignBottom="#+id/aeroporika"
android:layout_margin="1dp"
android:gravity="center"
android:text="#string/search"
android:textSize="22sp"
android:textColor="#color/categories"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/aeroporika"
android:layout_marginTop="50dp"
android:layout_centerInParent="true"
android:background="#color/categories"
android:scaleX="0.5">
<TextView
android:id="#+id/textView_1"
android:layout_width="wrap_content"
android:gravity="center_vertical"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:text="#string/find_paral"
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/find_beach"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginLeft="200dp"
android:src="#drawable/find"
android:onClick="find"/>
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView_1"
android:layout_marginTop="10dp"
android:ems="10"
android:text="">
<requestFocus />
</AutoCompleteTextView>
</RelativeLayout>
Can anyone help me with this?
Thanks a lot!
Try choice.equals("one") instead of choice == "one"
I have a main Activity that calls a popup window:
AddProduct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent popup = new Intent(SingleVoucher.this, PopUp.class);
if (additionalProduct!=null) {
popup.putExtra("additionalamount", additionalQuantity);
popup.putExtra("additionalprice", additionalPrice);
popup.putExtra("additionalproduct", additionalProduct);
}
startActivity(popup);
}
});
the popup:
public class PopUp extends Activity {
private OnSubmitListener mListener;
#Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.popup_window);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
WindowManager.LayoutParams windowManager = getWindow().getAttributes();
windowManager.dimAmount = 0.75f;
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
TextView SubmitAdditional = (TextView) findViewById(R.id.SubmitAdditional);
final EditText product = (EditText) findViewById(R.id.adittionalTextEdit);
final EditText qty = (EditText) findViewById(R.id.additionalQuantityTE);
final EditText price = (EditText) findViewById(R.id.adittionalPriceTE);
final Intent intent = getIntent();
final Context context = this;
SubmitAdditional.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String additionalProduct = product.getText().toString();
String additionalQuantity = qty.getText().toString();
String additionalPrice = price.getText().toString();
if (additionalProduct!=null && additionalPrice.isEmpty()==false && additionalQuantity.isEmpty()==false) {
intent.putExtra("additionalamount", additionalQuantity);
intent.putExtra("additionalprice", additionalPrice);
intent.putExtra("additionalproduct", additionalProduct);
}
else{
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Missing Details");
alert.setMessage("Some of the required fields are missing. please try again");
alert.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
}
}
});
getWindow().setLayout((int) (width * .8), (int) (height * .3));
}
}
and the XML of the poup window is:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/BlueGray">
<TextView
android:id="#+id/AdditionalTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="5dp"
android:layout_alignParentTop="true"
android:paddingTop="2dp"
android:text="Product Name"
android:textSize="18dp"
android:textColor="#color/White">
</TextView>
<EditText
android:id="#+id/adittionalTextEdit"
android:layout_width="260dp"
android:layout_height="50dp"
android:layout_below="#+id/AdditionalTitle"
android:background="#color/White"
android:textSize="12dp"
android:textColor="#color/Black"
android:layout_centerHorizontal="true"
android:gravity="top">
</EditText>
<TextView
android:id="#+id/additionalQuantityTV"
android:layout_marginLeft="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/adittionalTextEdit"
android:text="Quantity"
android:textColor="#color/White"
android:textSize="15dp"
android:layout_marginTop="5dp">
</TextView>
<EditText
android:id="#+id/additionalQuantityTE"
android:layout_width="100dp"
android:layout_height="20dp"
android:layout_toRightOf="#+id/additionalQuantityTV"
android:layout_below="#+id/adittionalTextEdit"
android:background="#color/White"
android:layout_marginTop="5dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="14dp"
android:layout_marginLeft="38dp"
android:numeric="integer"
android:textSize="14dp">
</EditText>
<TextView
android:id="#+id/adittionalPriceTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_below="#+id/additionalQuantityTV"
android:text="Overall price"
android:textColor="#color/White"
android:textSize="15dp"
android:layout_marginTop="5dp">
</TextView>
<EditText
android:id="#+id/adittionalPriceTE"
android:layout_width="100dp"
android:layout_height="20dp"
android:layout_toRightOf="#+id/adittionalPriceTV"
android:layout_below="#+id/additionalQuantityTE"
android:background="#color/White"
android:layout_marginTop="5dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="14dp"
android:layout_marginLeft="10dp"
android:numeric="decimal"
android:textSize="14dp">
</EditText>
<TextView
android:id="#+id/popupClose"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_below="#+id/adittionalPriceTE"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="14dp"
android:textColor="#color/White">
</TextView>
<TextView
android:id="#+id/SubmitAdditional"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_below="#+id/adittionalPriceTE"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="14dp"
android:textColor="#color/White">
</TextView>
</RelativeLayout>
I want to get the values from the EditText and send them back to the parent.
How can I send the data back to the view who called the popup?
You can use startActivityForResult and setResult methods to achieve this.
In MainActivity start the popup activity like this
startActivityForResult(popup,1);
In popup activity use the setResult method to pass the value back to main activity.
Intent intent = new Intent();
intent.putExtra("data",data); // data is the value you need in parent
setResult(100,data);
In MainActivity use the onActivityResult method to get the data
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { value = data.getBundleExtra("data");
You will have to startActivityForResult()
then return result from PopUp Activity using setResult()
see this good link for statActivityForResult
Try to use startActivityForResult in order to show the popup.
Here is an example: http://developer.android.com/training/basics/intents/result.html
Then in the main activity you should override onActivityResult in order to have access to the info you sent from popup.
how can i add ListView item from my add activity to main activity's ListView Statically without database?
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listviewitemclick();
Button btn=(Button)findViewById(R.id.add);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent(getApplicationContext(), Add.class);
startActivity(intent);
}
});
}
private void listviewitemclick(){
ListView lv=(ListView)findViewById(R.id.listView1);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long id) {
// TODO Auto-generated method stub
Intent intent=new Intent(getApplicationContext(), UpdateDel.class);
startActivity(intent);
}
});
}}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Shopping App"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp" >
</ListView>
<Button
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/listView1"
android:layout_marginLeft="16dp"
android:text="add"
/>
Add.java
public class Add extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
}
public void adduser(View v){
Intent i=new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
}
activity_add.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Add" >
<EditText
android:id="#+id/usret"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:ems="10"
android:hint="UserName" />
<requestFocus />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/usret"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:ems="10"
android:hint="Contact" >
</EditText>
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:hint="Address" />
<EditText
android:id="#+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText3"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp"
android:ems="10"
android:hint="Total Purchase" />
<Button
android:id="#+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:text="Add"
android:onClick="addtask"/>
this is the code i couldn't find how to add the editbox value in listitem can u plz help me
Start your Second Activity as startActivityForResult and use setResult() method for sending data back from Activity2 to Activity1. In activity1 you will need to Override onActivityResult for Updating TextView with EditText data from Activity2
for example :
Start Activity2 from Acivity1 as:
Intent i = new Intent(this, Activity2.class);
startActivityForResult(i, 1);
in Activity2 use setResult for sending data back :
Intent intent = new Intent();
intent.putExtra("edittextvalue","value_here");
setResult(RESULT_OK, intent);
finish();
and in First Activity receive data as 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();
}
}
You can use startActivityForResult(). If you don't want to use that, I will tell you the harder way.
I am assuming that the first time listview is empty.
Now, if you are going to MainActivity from add user, then you just need to pass editText's data with putEXtra method. Below is the code snippet:
String textOfEt1 = editTextReffernce1.getText().toString();
String textOfEt2 = editTextReffernce1.getText().toString();
Intent i = new Intent(this, MainActivity.class);
i.putExtra("key1", textOfEt1); //set key value pair
i.putExtra("key2", textOfEt2);
startActivity(i);
Read about Intent class and its methods and you will get idea what putExtra method is.
Now come to your MainActivity:
In onCreate method:
if(getIntent().getExtras() != null)
{
Bundle bd = getintent().getExtras();
String name1 = bd.getString("key1"); //get value by key
String name2 = bd.getString("key");
String[] strArray = {name1, name2};
ArrayAdapter ad = new ArrayAdapter(this, android.R.layout.simple_list_item_1, strArray);
yourListViewRefference.setAdapter(ad);
}
i am asking you for help. As you see from the picture it is a result that i should have, but, at the moment i have information orinted on my left corner. what i am doing wrog?
MainActivity.java
public class MainActivity extends ListActivity {
/** Items entered by the user is stored in this ArrayList variable */
ArrayList<String> list = new ArrayList<String>();
/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter<String> adapter;
TextView mTvSDate;
TextView mTvSName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.SecondElementActivity");
startActivityForResult(intent, 1);
}
};
ImageButton addBtn = (ImageButton) findViewById(R.id.addBtn);
addBtn.setOnClickListener(listener);
Log.d("Suceess1","Sucess1");
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Defining the ArrayAdapter to set items to ListView
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
// Getting reference to TextView tv_sage of the layout file activity_student
// mTvSDate = (TextView) findViewById(R.id.editDate);
// Getting reference to TextView tv_sname of the layout file activity_student
mTvSName = (TextView)findViewById(R.id.editName);
Log.d("Suceess5","Sucess5");
// Fetching data from a parcelable object passed from MainActivity
NoteElement drug = getIntent().getParcelableExtra("drug");
// MyAdapter adapter = new MyAdapter(this, generateData());
Log.d("Suceess6","Sucess6");
list.add(mTvSName.getText().toString());
mTvSName.setText(drug.mSName);
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.main, menu);
return true;
}
}
SecondElementActivity.java
public class SecondElementActivity extends Activity{
EditText mEtSDate;
EditText mEtSName;
Button btnSave;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_element);
// Getting a reference to EditText et_sname of the layout activity_main
mEtSName = (EditText)findViewById(R.id.editName);
// Getting a reference to EditText et_sage of the layout activity_main
mEtSDate = (EditText)findViewById(R.id.editDate);
// Getting a reference to Button btn_ok of the layout activity_main
btnSave = (Button)findViewById(R.id.btnSave);
// Setting onClick event listener for the "OK" button
btnSave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Creating an instance of NoteElement class with user input data
NoteElement drug = new NoteElement(
mEtSDate.getText().toString(),
mEtSName.getText().toString());
// Creating an intent to open the activity MainActivity
Intent intent = new Intent(getBaseContext(), MainActivity.class);
// Passing data as a parecelable object to MainActivity
intent.putExtra("drug",drug);
// Opening the activity
startActivity(intent);
}
});
}
}
Parcelable class NoteElement.java
public class NoteElement implements Parcelable{
String mSDate;
String mSName;
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
/**
* Storing the NoteElement data to Parcel object
**/
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mSDate);
dest.writeString(mSName);
}
/**
* A constructor that initializes the NoteElement object
**/
public NoteElement(String sDate, String sName){
this.mSDate = sDate;
this.mSName = sName;
}
/**
* Retrieving NoteElement data from Parcel object
* This constructor is invoked by the method createFromParcel(Parcel source) of
* the object CREATOR
**/
private NoteElement(Parcel in){
this.mSDate = in.readString();
this.mSName = in.readString();
}
public static final Parcelable.Creator<NoteElement> CREATOR = new Parcelable.Creator<NoteElement>() {
#Override
public NoteElement createFromParcel(Parcel source) {
return new NoteElement(source);
}
#Override
public NoteElement[] newArray(int size) {
return new NoteElement[size];
}
};
}
And my activity_main have such a .xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_action_new"
android:contentDescription="#string/desc"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/addBtn"
android:layout_alignParentLeft="true"
android:layout_marginLeft="53dp"
android:text="#string/mainTxt"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="32sp" />
<!-- Student version -->
<TextView
android:id="#+id/tv_sname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true" />
<TextView
android:id="#+id/tv_sdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true" />
<!-- List -->
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="20dp"
android:layout_toLeftOf="#+id/addBtn" />
</RelativeLayout>
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_action_new"
android:contentDescription="#string/desc"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/addBtn"
android:layout_alignParentLeft="true"
android:layout_marginLeft="53dp"
android:text="#string/mainTxt"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="32sp" />
<!-- Date -->
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_below="#+id/textView1"
android:layout_marginRight="36dp"
android:layout_marginTop="14dp"
android:layout_toLeftOf="#+id/editDate"
android:text="#string/date" />
<EditText
android:id="#+id/editDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editName"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="date"
android:hint="#string/str_hnt_date" />
<!-- Name -->
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignBottom="#+id/editName"
android:layout_alignLeft="#+id/date"
android:text="#string/name" />
<EditText
android:id="#+id/editName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/date"
android:ems="10"
android:hint="#string/str_hnt_name" />
<!-- Dosage -->
<TextView
android:id="#+id/dosage"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/editText3"
android:layout_alignBottom="#+id/editText3"
android:layout_alignLeft="#+id/notes"
android:text="#string/dosage" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editName"
android:layout_below="#+id/editName"
android:ems="10"
android:hint="#string/str_hnt_dosage" />
<!-- Notes -->
<TextView
android:id="#+id/notes"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/editText4"
android:layout_alignBottom="#+id/editText4"
android:layout_alignRight="#+id/name"
android:text="#string/notes" />
<EditText
android:id="#+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText3"
android:layout_below="#+id/editText3"
android:ems="10"
android:hint="#string/str_hnt_notes"/>
<!-- buttons: Save and Selete-->
<Button
android:id="#+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText4"
android:layout_below="#+id/notes"
android:layout_marginTop="20dp"
android:text="#string/btnSave" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btnSave"
android:layout_marginLeft="20dp"
android:layout_toLeftOf="#+id/addBtn"
android:layout_toRightOf="#+id/btnSave"
android:text="#string/btnDelete" />
</RelativeLayout>
Okay so you have two activities total right?
Here's the deal: Look at your image. Activity 1 starts activity 2 right? And you're expecting to get information from your activity 2 back to activity 1, correct? Alright.
First thing to learn:
startActivity(intent);
This method states that you will just initiate an activity but expect nothing back from it. So even if you want to send information back to activity 1 THROUGH activitiy 2 it will not work. Instead you must do this:
startActivityForResult(intent, 1);
The second parameter is an integer that can help you differentiate between different activity calls, it is not important for you right now.
Now, because you say "ForResult" in your method above, in your MainActivity now you must implement this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// here you will work with the code.
// data is your intent data sent from activity 2
// where you say this in your own code:
// Passing data as a parecelable object to MainActivity
// intent.putExtra("drug",drug);
}
Now the last thing to note:
In SecondElementActivity.java where you have this:
// Opening the activity
startActivity(intent);
It is wrong. You know why? because you're saying that you want to start a new activity. But in Android you already have a parent for this activity, which is activity 1. So your activity 1 called 2, when you end activity 2 it will go back to 1. So, replace that line for this:
setResult(RESULT_OK, intent);
finish();
EDIT:
Also I don't know if this is correct, I don't do it this way, so here is my fix:
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.SecondElementActivity");
startActivity(intent);
}
};
When you say you want to make a new intent you should pass two parameters:
Intent intent = new Intent(this, SecondElementActivity.class);
The second parameter is the name of the class you want to call.
You can try like this :
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher"
android:contentDescription="#string/hello_world"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/addBtn"
android:layout_centerHorizontal="true"
android:text="mainTxt"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="32sp" />
<!-- List -->
<ListView
android:id="#+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="20dp" />
</RelativeLayout>
Follow This tutorial for populating ListView just change adapter and row.xml from that tutorial
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tv_sdate "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingLeft="5dp"
android:textSize="12sp" >
</TextView>
<TextView
android:id="#+id/tv_sname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/tv_sdate "
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textSize="12sp" >
</TextView>
</RelativeLayout>
http://hmkcode.com/android-custom-listview-items-row/
I´m new in Android Development and want to build a little application.
This is how the Layout looks like.
This is my layout code (cant´t post picture because I´m a new user):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/newList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/listName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/help_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/help_add"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editVoc"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btn_done" />
<Button
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btn_add" />
</LinearLayout>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
And this is my java code now.
public class AddItemsToList extends Activity implements OnClickListener{
VocabularySet vocabularySet = new VocabularySet();
private TextView t;
private Button btn_add;
private Button btn_done;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.add_items_to_list);
vocabularySet.listName = NewList.listName;
t = (TextView) findViewById(R.id.listName);
t.setText(vocabularySet.listName);
btn_add = (Button)findViewById(R.id.add);
btn_add.setOnClickListener(this);
btn_done = (Button)findViewById(R.id.done);
btn_done.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.add:
EditText word = (EditText)findViewById(R.id.editVoc);
vocabularySet.addListElement(word.getText().toString());
/* setListAdapter(new ArrayAdapter<String>(this, R.id.listView1, vocabularySet.words));
ListView lv = getListView();
lv.setTextFilterEnabled(true);*/
//ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.id.listView1, vocabularySet.words);
break;
case R.id.done:
new AlertDialog.Builder(this).setMessage(string.error_listname_missing).setNeutralButton(string.error_ok, null).show();
break;
}
}
}
I tried a few things like setting List Adapter etc. but nothing worked for me, how can i add Words in this empty List (ListView)?