Skip to content

Instantly share code, notes, and snippets.

@AbdulRehmanNazar
Last active January 20, 2017 15:08
Show Gist options
  • Save AbdulRehmanNazar/ab698af38f4b290e38fc0dca015c158c to your computer and use it in GitHub Desktop.
Save AbdulRehmanNazar/ab698af38f4b290e38fc0dca015c158c to your computer and use it in GitHub Desktop.
ListView Sample Gist
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
package com.gist.listview;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {
ListView listView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
String[] values = new String[] { "USA",
"UK",
"CANADA",
"FRANCE",
"JAPAN",
"CHINA",
"SINGAPORE"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int itemPosition = position;
String itemValue = (String) listView.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment