How to get data from text file onListItemClick in android? - android

I am currently working on an app to make mods for minecraft. My app has a simple file manager where I want to get the file data and put it in another activity in an EditText, when the user selects a file. I don't know how to get the data and send it to an EditText in another activity.
EDIT:
This is my OpenScript.class which I'm trying to push the data inside the file in a EditText on another activity but I have no clue how to do that.
public class OpenScript extends ListActivity {
private List<String> items =null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scripts_list);
getFiles(new File("/sdcard/ChatoGuy1/ModPE Scripter/Scripts").listFiles());
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
try{
Intent i = new Intent(this, ScriptWriter.class);
i.putExtra("code", /* ? */);
startActivityForResult(i, 1);
}
catch(Exception e){
}
}
private void getFiles(File[] files) {
items = new ArrayList<String>();
for (File file : files) {
items.add(file.getPath());
}
ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.file_list_row, items);
setListAdapter(fileList);
}
}
Second Activity:
public class ScriptWriter extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.code_editor);
final EditText editText = (EditText) findViewById(R.id.codeEditor);
final Button codeSave = (Button) findViewById(R.id.bCodeSave);
//get file
Intent intent = getIntent();
String test = intent.getExtras().getString("code");
//read file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(test));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
}
catch (IOException e) {
Toast.makeText(this, "File does not exist.", Toast.LENGTH_LONG).show();
}
editText.setText(text);
}
}

How to get data from text file onListItemClick in android?
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
try{
TextView tv = v.findViewById(<id_of_infliated_textView_in_your_adapter_class>)
String path - tv.getText().toString();
Intent i = new Intent(this, ScriptWriter.class);
i.putExtra("code", path );
startActivityForResult(i, 1);
}
catch(Exception e){
}
}
The above code works when you use the custom adapter (creating your own adapter class and infiliating layout file).
In your case it is quite simple.
How could I go about doing that? I can't seem to understand on how to
send the URI to my second activity. I know how to read a file from a
given path but not when user selects a file
To get the file path
As you already declare your Array list globally, and using this method in your code.
private void getFiles(File[] files) {
items = new ArrayList<String>();
for (File file : files) {
items.add(file.getPath());
}
ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.file_list_row, items);
setListAdapter(fileList);
}
make one more method saying "getFileAtPosition"
private String getFileAtPosition(int position){
return items.get(position);
}
And call the above function in your onListItemClick method
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
try{
Intent i = new Intent(this, ScriptWriter.class);
i.putExtra("code",getFileAtPosition(position));
startActivityForResult(i, 1);
}
catch(Exception e){
}
}

When the user selects a file on Activity A, you want to open a file and read it's contents and send the contents to Activity B where it will be displayed in an EditText widget. Right?
There's no need to send the file contents to Activity B in an intent. Just send the file URI as part of the intent to Activity B and then do the file read operation there and populate your EditText. That's much cleaner.
HTH.

Related

Android onitemclicklistener issues

Dont know if I am even doing this right as I am new to android programming but I have set an OnItemClickListener to respond to the users listitem selection by beginning a new intent.
When the user selects whatever article in the listview they should see the corresponding txt file in the new activity.
So in the new activity I have tried to find a way to open the corresponding file in the subfolder of the Assets Folder ....
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
Intent i = getIntent();
int position = i.getExtras().getInt("position");
TextView news = (TextView) findViewById(R.id.txtView);
AssetManager as = getAssets();
InputStream is;
try{
is = as.open(""); <----- !!!
int bytes = is.available();
byte[] b = new byte[bytes];
is.read(b);
is.close();
String s = new String (b);
news.setText(s);
}catch (IOException e){
e.printStackTrace();
}
}
.... however i will only succeed in opening a single txt file.
How can I implement this activity to respond to the OnItemClickListener from the previous activity as shown here...
ls = (ListView) findViewById(R.id.bArt);
ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int position, long id) {
Intent i = new Intent (getApplicationContext(), New_Activity.class );
i.putExtra("id", position);
startActivity(i);
}
});
... to open the correct .txt file from the Assets Folder
I have been stuck on this for a long time now so my Appreciation points (no cash value) to the functional answer will be unending
Much Obliged
Change this
int position = i.getExtras().getInt("id");
Your Id name must same when ever you passing some data between Activity.
Instead of store the position, store the file name inside the intent. Yon can retrieve it with AdapterView.getItemAtPosition
ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int position, long id) {
Intent i = new Intent (getApplicationContext(), New_Activity.class );
i.putExtra("id", av.getItemAtPosition(position));
startActivity(i);
}
});
In the other activity retrieve the file name and pass this to the AssetsManager
String fileName = i.getStringExtra("id");
InputStream is;
try{
is = as.open(fileName); <----- !!!
// other code
}

Replacing, updating, and saving text file for ListView Android

I am trying to update my ListView text file by replacing the word I just edited from returning from a Second Activity. Currently the output I am getting is a Toast of the word I just edited in the variable name in my onActivityResult().
How my app works.
Tap on item in ListView
Opens to Activity 2 with tapped item in EditText
edit the item and press save to return to first activity
edited item returns in variable name and is displayed in popup (toast)
I want to replace the item I just edited with the old item and save it so then when I reopen the app, the updated/edited item is in place of the old item.
I feel as though I have the parts to complete this but I am just starting on android development so I taking quite a while to figure this out. I was wondering if someone could lead me in the right direction.
Here is my Activity:
public class ToDoActivity extends Activity {
private ArrayList<String> todoItems;
private ArrayAdapter<String> todoAdapter; // declare array adapter which will translate the piece of data to teh view
private ListView lvItems; // attach to list view
private EditText etNewItem;
private final int REQUEST_CODE = 20;
//private Intent i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do);
etNewItem = (EditText) findViewById(R.id.etNewItem);
lvItems = (ListView) findViewById(R.id.lvItems); // now we have access to ListView
//populateArrayItems(); // call function
readItems(); // read items from file
todoAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems); //create adapter
lvItems.setAdapter(todoAdapter); // populate listview using the adapter
//todoAdapter.add("item 4");
setupListViewListener();
setupEditItemListener();
//onActivityResult(REQUEST_CODE, RESULT_OK, );
}
private void launchEditItem(String item) {
Intent i = new Intent(this, EditItemActivity.class);
i.putExtra("itemOnList", item); // list item into edit text
startActivityForResult(i, REQUEST_CODE);
//startActivity(i);
}
private void setupEditItemListener() { // on click, run this function to display edit page
lvItems.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View item, int pos, long id) {
String text = (String) lvItems.getItemAtPosition(pos);
launchEditItem(text);
}
});
}
private void setupListViewListener() {
lvItems.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapter, View item, int pos, long id) {
todoItems.remove(pos);
todoAdapter.notifyDataSetChanged(); // has adapter look back at the array list and refresh it's data and repopulate the view
writeItems();
return true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.to_do, menu);
return true;
}
public void onAddedItem(View v) {
String itemText = etNewItem.getText().toString();
todoAdapter.add(itemText); // add to adapter
etNewItem.setText(""); //clear edit text
writeItems(); //each time to add item, you want to write to file to memorize
}
private void readItems() {
File filesDir = getFilesDir(); //return path where files can be created for android
File todoFile = new File(filesDir, "todo.txt");
try {
todoItems = new ArrayList<String>(FileUtils.readLines(todoFile)); //populate with read
}catch (IOException e) { // if files doesn't exist
todoItems = new ArrayList<String>();
}
}
private void writeItems() {
File filesDir = getFilesDir(); //return path where files can be created for android
File todoFile = new File(filesDir, "todo.txt");
try {
FileUtils.writeLines(todoFile, todoItems); // pass todoItems to todoFile
} catch (IOException e) {
e.printStackTrace();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
String name = data.getExtras().getString("EditedItem");
Toast.makeText(this, name, Toast.LENGTH_LONG).show();
//writeItems();
}
}
}
If needed, I'll post my second Activity as well but I don't think it is necessary. However ask and you shall receive!
The solution you came up with is a bit basic and really limits you if it goes about resolving problem you mentioned. What I mean is that the Adapter you're using for your ListView consists only of simple ArrayList<String>, which prevents you from knowing which element is name you're looking for.
Better solution would be to create your own Adapter where each element would have a special key or something, but I'm afraid it's a bit too complicated for you for now. Keep in mind that it's possible and often very useful to create custom Adapters though.
What I thought could be possible in your case is a little hack maybe, but it works ONLY if you're totally sure that name is always stored at n-th position, let's say at 10th position.
Then you can do this:
private final int NAME_POSITION = 10;
Now that you have this position, you should find 10th line in your file, erase it and store new value. I won't be writing this code, because it's not really related to this question. There is a lot of questions about file reading/writing in Java on Stackoverflow that you should easily find the solution if you don't know how to do it yet. Basically you have to put this in this place:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
String name = data.getExtras().getString("EditedItem");
Toast.makeText(this, name, Toast.LENGTH_LONG).show();
// Write your name to file now
}
}
The second approach would be to forget about files and use SharedPreferences for the updated name:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
String name = data.getExtras().getString("EditedItem");
Toast.makeText(this, name, Toast.LENGTH_LONG).show();
SharedPrefrences sp = getSharedPreferences("MySavedValues", 0); // Open SharedPreferences with name MySavedValues
Editor editor = sp.edit();
editor.putString("Name", name); // Store name with key "Name". This key will be then used to retrieve data.
editor.commit();
}
}
and in onCreate():
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do);
etNewItem = (EditText) findViewById(R.id.etNewItem);
lvItems = (ListView) findViewById(R.id.lvItems); // now we have access to ListView
//populateArrayItems(); // call function
readItems(); // read items from file
SharedPreferences sp = getSharedPreferences("MySavedValues", 0); // Open SharedPreferences with name MySavedValues
String name = sp.getString("Name", ""); // If there isn't any string stored with key "Name", it will return empty string
if(!name.isEmpty()) {
todoItems.set(NAME_POSITION, name);
}
todoAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems); //create adapter
lvItems.setAdapter(todoAdapter); // populate listview using the adapter
//todoAdapter.add("item 4");
setupListViewListener();
setupEditItemListener();
//onActivityResult(REQUEST_CODE, RESULT_OK, );
}
I haven't tested it, so you have to do it yourself. Also if you find my answer a bit complicated, feel free to ask about anything.

Passing values to Activities with putExtra()

I am trying to pass a value on my ListView to my second activity using Intents. I am not sure how to pass the text value to the second activity my Intent leads to. Right now my Intent is able to connect to the second activity on tap but it doesn't pass the string of the tapped value.
I feel that I need to pass something into my launchEditItem() but I am not sure what. These are the two functions I am dealing with right now.
private void launchEditItem() {
Intent i = new Intent(this, EditItemActivity.class);
i.putExtra("itemOnList", ); // list item into edit text
startActivity(i);
}
private void setupEditItemListener() { // on click, run this function to display edit page
lvItems.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View item, int pos, long id) {
launchEditItem();
}
});
}
I'm not sure what value to place into the i.putExtra(), but I think I need to pass an argument into the launchEditItem().
This is what is currently in my second Activity:
public class EditItemActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_item);
Intent i = getIntent();
String ItemToEdit = i.getStringExtra("itemOnList");
// place into EditText using ItemToEdit
}
I'm also not sure how to place this String value (ItemToEdit) into an EditText box. I'm new to android dev so I'm learning as much as I can thank you!
* EDIT *
I guess I'm a bit too vague in my question. Here is the entire code of the small app I am working on
public class ToDoActivity extends Activity {
private ArrayList<String> todoItems;
private ArrayAdapter<String> todoAdapter; // declare array adapter which will translate the piece of data to teh view
private ListView lvItems; // attach to list view
private EditText etNewItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do);
etNewItem = (EditText) findViewById(R.id.etNewItem);
lvItems = (ListView) findViewById(R.id.lvItems); // now we have access to ListView
//populateArrayItems(); // call function
readItems(); // read items from file
todoAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems); //create adapter
lvItems.setAdapter(todoAdapter); // populate listview using the adapter
//todoAdapter.add("item 4");
setupListViewListener();
setupEditItemListener();
}
private void launchEditItem() {
Intent i = new Intent(this, EditItemActivity.class);
i.putExtra("itemOnList", ); // list item into edit text
startActivity(i);
}
private void setupEditItemListener() { // on click, run this function to display edit page
lvItems.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View item, int pos, long id) {
launchEditItem();
}
});
}
private void setupListViewListener() {
lvItems.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapter, View item, int pos, long id) {
todoItems.remove(pos);
todoAdapter.notifyDataSetChanged(); // has adapter look back at the array list and refresh it's data and repopulate the view
writeItems();
return true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.to_do, menu);
return true;
}
public void onAddedItem(View v) {
String itemText = etNewItem.getText().toString();
todoAdapter.add(itemText); // add to adapter
etNewItem.setText(""); //clear edit text
writeItems(); //each time to add item, you want to write to file to memorize
}
private void readItems() {
File filesDir = getFilesDir(); //return path where files can be created for android
File todoFile = new File(filesDir, "todo.txt");
try {
todoItems = new ArrayList<String>(FileUtils.readLines(todoFile)); //populate with read
}catch (IOException e) { // if files doesn't exist
todoItems = new ArrayList<String>();
}
}
private void writeItems() {
File filesDir = getFilesDir(); //return path where files can be created for android
File todoFile = new File(filesDir, "todo.txt");
try {
FileUtils.writeLines(todoFile, todoItems); // pass todoItems to todoFile
} catch (IOException e) {
e.printStackTrace();
}
}
}
String[] link_list;
int currenttrack=0;
link_list=new String[]{
"W-TE_Ys4iwM",//1
"oozgmH3ZP14",//2
"o_v9MY_FMcw",//3
"36mCEZzzQ3o",//4
}
First activity
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
currenttrack=arg2;
Intent videoIntent=new Intent(MainActivity.this,VideoView.class);
videoIntent.putExtra("filename", link_list[currenttrack]);
startActivity(videoIntent);
}
});
In your Second Activity
// getting intent data
// get intent data
Intent i = getIntent();
Bundle extras = i.getExtras();
filename = extras.getString("filename");
Log.e("File Name", filename);
and your done :)
In your current Activity, create a new Intent:
Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);
Then in the new Activity, retrieve those values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
I am not sure if i understood where is the value. Well if the value is in EditText do something like:
private void launchEditItem(String text) {
Intent i = new Intent(this, EditItemActivity.class);
i.putExtra("itemOnList", text); // list item into edit text
startActivity(i);
}
private void setupEditItemListener() { // on click, run this function to display edit page
lvItems.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View item, int pos, long id) {
EditText editView = (EditText) item.findById(R.id.ItemToEdit);
String text = editView != null ? editView.getText().toString() : "";
launchEditItem(text);
}
});
}
1 - I have some doubt with:
i.putExtra("itemOnList", );
You'd better pass a value:
i.putExtra("itemOnList", "something");
2 - To valorize a control, you must obtain a reference to it first. Something like:
EditText edt =
(EditText) findViewById(R.id.activity_edit_item_my_EditText); // or whatever id you assigned to it (it MUST HAVE AN ID)
Do it AFTER setContentView().
Then you can set it's text like:
edt.setText(ItemToEdit); // Now it should contain "something"
Just as simple as that
[EDIT]
If you aren't sure what to pass so is to pass in the "tapped" value in the ListView into the putExtra, modify your listview click handler code:
list.setOnItemClickListener
(
new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
// TODO Auto-generated method stub
Intent videoIntent = new Intent(MainActivity.this, VideoView.class);
videoIntent.putExtra("filename", (((TextView) v).getText().toString());
startActivity(videoIntent);
}
}
);
It should work immediately.
01: Current Activity
String pass_value ="value";
Intent intent = new Intent(getApplicationContext(),NewActivity.class);
intent.putExtra("var_name",pass_value);
startActivity(intent);
02: New Activity
String value = getIntent().getExtras().getString("var_name");

How to view an image from a list of images Android?

I am working on an application that saves images to a directory and then allows you to view those images. Currently I am able to save images and then view a list of the images within that directory. I want to be able to open an image in an imageview when the user selects the image file from the list. Here is the code I currently have for the list:
public class Test extends ListActivity {
private List<String> fileList = new ArrayList<String>();
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
String root = Environment.getExternalStorageDirectory().toString();
File mapfiles= new File(root + "/Maps");
ListDir(mapfiles);
}
void ListDir(File f) {
File[] files = f.listFiles();
fileList.clear();
for (File file : files) {
//fileList.add(file.getPath());
fileList.add(file.getName());
}
ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fileList);
setListAdapter(directoryList);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
String item=fileList.get(position).toString();
Toast.makeText(Test.this, "You clicked on: "+item, Toast.LENGTH_SHORT).show();
}
}
I used toast just to verify that I could get something to happen when clicking on an item in the list, and that is working. The code I tried to use within the onItemListClick is:
File selected = new File(fileList.get(position));
if(selected.isDirectory()){
ListDir(selected);
} else {
Uri selectedUri = Uri.fromFile(selected);
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, selectedUri);
startActivity(notificationIntent);
}
That causes the app to crash and I'm not sure why. My question is what is incorrect in the above code? Or simply how can I view an image by selecting it from my list?
Thanks.

Android adding a custom extended listactivity to layout in code

I have a simple linearlayout with a textbox a button and a listview, I'm doing some URL data getting when something is entered in the textbox and the button is pressed I want to parse the results and display in the listview.
What I can't work out is how to instatiate the listview from within my extende activity class and add it to the layout? I think I'm barking up the wrong tree !
public class HelloAndroid extends Activity{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyList L;
L = new MyList();
//setContentView(L.getListView());
EditText edittext = (EditText)findViewById(R.id.editText1);
edittext.setText("");
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
EditText edittext = (EditText)findViewById(R.id.editText1);
executeHttpGet(edittext.getText().toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void executeHttpGet(String vrm) throws Exception {
BufferedReader in = null;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("http://xxx/vrmtest.asp?vrm="+vrm));
HttpResponse response = client.execute(request);
in = new BufferedReader
(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String page = sb.toString();
System.out.println(page);
Context context = getApplicationContext();
CharSequence text = page;
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class MyList extends ListActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Create an array of Strings, that will be put to our ListActivity
String[] names = new String[] { "Linux", "Windows7", "Eclipse", "Suse",
"Ubuntu", "Solaris", "Android", "iPhone"};
// Create an ArrayAdapter, that will actually make the Strings above
// appear in the ListView
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, names));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG)
.show();
}
}
}
An explanation would suffice rather than code but an example would be nice.
As long as your main.xml layout file has a ListView in it, you're on the right path. Set its #+id to something arbitrary and reference it in your code with:
ListView lv = (ListView) findViewById(R.id.arbitrary_id);
You don't necessarily need a ListActivity to use a ListView. Simply parse the data that you receive back from the server, put it in an array, and use lv.setAdapter(your_array_adapter) to fill the ListView with your data.
You can go further and specify your ItemClickListener by:
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
}
};
Alternatively, you can create the ListView programmatically using
ListView lv = new ListView(this);
and adding it to a view in your layout with
some_container_view_in_main.addView(lv);
Then you would set the ArrayAdapter and OnItemClickListener the same as above.
You shouldn't create an Activity object yourself. Android will do it for you. Read this article, it will help you: http://developer.android.com/guide/topics/intents/intents-filters.html
If you want to add a ListView to your layout, just add it in the layout xml file. You can't add one activity to another until parent activity extends ActivityGroup. But that's not your case.
If what you want is to add the list to your layout, then use ListView instead of ListActivity.
you can then add the list as this.addView (myList);
Please follow the example here: http://developer.android.com/resources/tutorials/views/hello-listview.html

Categories

Resources