Simple ListView without using android layout

1. Create a new project by going to File ⇒ New Android Application. Fill all the details  As Application Name:SimpleListView,Project Name:SimpleListView,Package Name:com.test.simplelistview  and click on Next button to finish .

2.Open MainActivity.java class and paste following code.

import android.os.Bundle;
import android.app.ListActivity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String[] items=new String[] {“sunday”,”monaday”,”tuesday”,”wednesday”,”thrusday”,”friday”,”saturday”};

final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,items);

setListAdapter(adapter);

ListView listView = getListView();

listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub

String filename = adapter.getItem(position).toString();

// ListView Clicked item value

//   String  filename= (String) listView.getItemAtPosition(position);
//   remove final from ArrayAdapter to use.

Toast.makeText(getApplicationContext(), “Slected File=” + filename+”;”+ ” Selected Position=”+position, Toast.LENGTH_LONG).show();
}
});
}

}

3. Finally run your project by right clicking on your project folder ⇒ Run As ⇒ 1 Android Application.

ListViewListView

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s