ListActivity is an important element of android apps. ListActivity manages all data in linear format.. on user screen.
Here is simple example based on how to use ListActivity.
It is a demo, which express how to add data in list with every hit on Add button & remove last entry data with Remove button hit.
1) Create an android project in eclipse.
2) extends your MainActivity with ListActivity.
3) design your main.xml file, in such way that ,
At top you will having one edit text ,so you put data on it.. & two Button, Add & Remove. & below of it.. one ListView to show data .
4) so,your main.xml will be look like ,follow
here, activtiy_dynamic_list.xml is mine xml file..
******************************************************
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:id="@+id/editText"/>
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="25"
android:text="Add data" />
<Button
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="25"
android:text="Delete data" />
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false" />
</LinearLayout>
******************************************************
******************************************************************
package com.examples.dynamiclist;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class DynamicList extends ListActivity {
MyDataAdapter adapters;
ArrayList<String> st;
Button add, remove;
TextView tv;
EditText editText;
ArrayAdapter<String> adapter;
String obj;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dynamic_list);
add = (Button) findViewById(R.id.add);
remove = (Button) findViewById(R.id.clear);
editText = (EditText) findViewById(R.id.editText);
st = new ArrayList<String>();
st.add("India");
st.add("USA");
// adapter = new ArrayAdapter<String>(this, R.layout.myrow,
// R.id.myrowdata, st);
adapters = new MyDataAdapter(this, R.layout.myrow, R.id.myrowdata, st);
setListAdapter(adapters);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
obj = editText.getText().toString();
st.add(obj);
adapters.notifyDataSetChanged();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_dynamic_list, menu);
return true;
}
public class MyDataAdapter extends ArrayAdapter<String> {
public MyDataAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
super(context, resource, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.myrow, null);
tv = (TextView) view.findViewById(R.id.myrowdata);
tv.setText(st.get(position));
remove.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//st.clear();
st.remove(position);
adapters.notifyDataSetChanged();
}
});
return view;
}
}
}
******************************************************************
& output will be look like,
Fig 1. user screen
Fig 2. Adding Data
Fig 3. showing data in list
Fig 4. removing data
These,is simple demo for dynamic view with ListActivity
Regards :) ,Happy Coding