List View activity || android studios tutorial || Mr.dark techy

List View activity || android studios tutorial || Mr.dark techy

Code:

MainActivity

LOCATION: java/MainActivity.java

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

public class MyAndroidAppActivity extends Activity {
ListView listView ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Get ListView object from xml
listView = (ListView) findViewById(R.id.list);

// Defined Array values to show in ListView
String[] values = new String[] { "Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View"
};

// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data

ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);

// Assign adapter to ListView
listView.setAdapter(adapter);

// ListView Item Click Listener
listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView parent, View view,int position, long id) {

// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);

// Show Alert
Toast.makeText(getApplicationContext(),
itemValue , Toast.LENGTH_LONG)
.show();

}
});
}
}


XML:

activity_main

LOCATION: res/layout/activity_main.xml

<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>



List View activity || android studios tutorial || Mr.dark techy List View activity || android studios tutorial || Mr.dark techy Reviewed by Salman Shaikh on April 12, 2020 Rating: 5

No comments:

Powered by Blogger.