Created
June 23, 2013 17:25
-
-
Save cofearabi/5845790 to your computer and use it in GitHub Desktop.
Android sample of Listview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.listviewsample2; | |
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[] { "192.168.1.1", | |
"192.168.1.2", | |
"192.168.1.3", | |
"192.168.1.4", | |
"192.168.1.5", | |
"192.168.1.6", | |
"192.168.1.7" | |
}; | |
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