Skip to main content

Android: Simple ListView using ArrayAdapter

Activity:


public class SimpleListView extends ActionBarActivity implements OnItemClickListener {

ListView simpleListView;
String[] versions={"Cupcake (1.5)","Doughnut (1.6)","Eclair (2.0–2.1)",
"Froyo (2.2–2.2.3)","Gingerbread (2.3–2.3.7)","Honeycomb (3.0–3.2.6)",
"Ice Cream Sandwich (4.0–4.0.4)","Jelly Bean (4.1–4.3)","KitKat (4.4–4.4.3)"
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_character_count);
simpleListView=(ListView)findViewById(R.id.listView1);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,android.R.id.text1,versions);
simpleListView.setAdapter(adapter);
simpleListView.setOnItemClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.character_count, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

return super.onOptionsItemSelected(item);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String msg="Item :"+parent.getItemAtPosition(position);
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();

}

}

Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sachinsandbhor.SimpleListView.SimpleListView"
    tools:ignore="MergeRootFrame" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >
    </ListView>

</RelativeLayout>

Here is the sample Code

ScreenShot:






Comments

Popular posts from this blog

Get domain name from URL in android

Sample Code: String url="http://sports.in.msn.com/football-world-cup-2014/world-cup-animals-5"; String hostName=getDomainName(url); private CharSequence getDomainName(String shareURL) throws URISyntaxException {    URI uri = new URI(shareURL);    String domain = uri.getHost();     return domain; } above method return host name from given URL. You can get more from URL using below methods. uri.getProtocol(); uri.getAuthority(); uri.getHost(); uri.getPort(); uri.getPath(); uri.getQuery(); uri.getFile(); Output: protocol = http authority = sports.in.msn.com host = sports.in.msn.com port = -1 path = /football-world-cup-2014/world-cup-animals-5 query = null filename = /football-world-cup-2014/world-cup-animals-5

How to check internet connectivity in Android app using BroadcastReceiver

So let's get started with How to check internet connectivity in Android using BroadcastReceiver. For this post I'm using Kotlin language. Well lots of people are using simple method to get network connectivity using ConnectivityManager. But using that approach we will get network state only when we are calling that method. So how would I know that network state changed. This will achieved using BroadcastReceiver.  What is BroadcastReceiver? BroadcastReceiver uses Publish-Subscribe pattern. Android app can send and receive messages from Android system and other apps.  Android system sends broadcast when various system events occurred like device charging, network state changes. Apps can register to receive specific broadcasts. When a broadcast is sent, the system automatically routes broadcasts to apps that have subscribed to receive that particular type of broadcast. So for this scenario we need to add some changes in AndroidManifest.xml file - ...

WeekView in Android

Hello Friends,    Have you Searching for Android calender with WeekView.  Today I am sharing android tutorial for android custom calendar WeekView. WeekView.java public class WeekView extends ActionBarActivity implements OnItemClickListener, OnClickListener { private GridView mGrid; private GregorianCalendar mCalendar; private Date[] mWeek; private TextView mMonthText; private RelativeLayout mArrowRight; private RelativeLayout mArrowLeft; private CalendarAdapter mAdapter; private SimpleDateFormat mFormatMonth = new SimpleDateFormat("MMMM"); private SimpleDateFormat mFormatDay = new SimpleDateFormat("d"); private SimpleDateFormat mFormatYear = new SimpleDateFormat("yyyy"); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_week_view); mGrid=(GridView)findViewById(R.id.gridview); mGrid.s...