I am trying to enter data dynamically in the Listview. I am using a second class file as dialog to get the data and passing the data through Intent. The data passed is in the form of string while the data in the arrayadapter that is to be displayed in the Listview is in the form of Arraylist. Please help me to solve this.
Thanks in advance.
I am attaching the code below
MainActivity.class
package com.example.listview;
import java.util.ArrayList;
import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.text.method.DialerKeyListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView lv = (ListView) findViewById(R.id.listView1);
final Button btn = (Button) findViewById(R.id.button1);
final EditText input = (EditText) findViewById(R.id.editText1);
btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// array.add(input.getText().toString());
// adapter.notifyDataSetChanged();
Intent i = new Intent(MainActivity.this,adddialog.class);
startActivity(i);
}
});
ArrayList<String> array = new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_list_item_1,array);
lv.setAdapter(adapter);
Intent i2 = getIntent();
String n = i2.getStringExtra("name");
array.add(n);
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;
}
}
Dialog for adding
Dialog.class
package com.example.listview;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class adddialog extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.adddialog);
final EditText in = (EditText) findViewById(R.id.editText1);
final Button btn1 = (Button) findViewById(R.id.enter);
btn1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
Intent i1 = new Intent(adddialog.this,MainActivity.class);
i1.putExtra("name", in.getText().toString());
startActivity(i1);
}
});
}
}
array.add(<String-Value-here>);See docs.oracle.com/javase/6/docs/api/java/util/…final;