android listview example
Introduction:
We will present a thorough overview of Android ListViews in this post, covering every facet of their implementation, functionality, and customization. ListViews, which enable developers to show a list of data items in a scrollable layout, are a crucial component of Android UI design. You will learn everything you need to know about ListViews with the help of this guide, including how to construct and fill them with data, how to handle user interactions, and how to alter their appearance to match the style of your app.
Creating a ListView
The first step in implementing a ListView is to define it in your app's layout XML file. You can do this by adding a <ListView> element to your layout, specifying its width, height, and other attributes. Once you have defined the ListView in your layout, you can reference it in your code using the findViewById() method, and then populate it with data using an adapter.
Populating a ListView with Data
You must develop an adapter that ties the data to the list items in order to fill a ListView with data. You can utilise a variety of adapters, such as ArrayAdapter, SimpleAdapter, and CursorAdapter. The ArrayAdapter, the most basic sort of adapter, is appropriate for straightforward data structures like an array of strings. A list of HashMaps, for example, can be displayed using the SimpleAdapter because it is more adaptable and can handle more complex data structures. Data from a database cursor is shown using the CursorAdapter.
1.activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="listview.example.com.listview.MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
</android.support.constraint.ConstraintLayout>
2.mylist.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#4d4d4d"
/>
3.strings.xml
<resources>
<string name="app_name">ListView</string>
<string-array name="array_technology">
<item>Android</item>
<item>Java</item>
<item>Php</item>
<item>Hadoop</item>
<item>Sap</item>
<item>Python</item>
<item>Ajax</item>
<item>C++</item>
<item>Ruby</item>
<item>Rails</item>
<item>.Net</item>
<item>Perl</item>
</string-array>
</resources>
4. MainActivity.java:
import android.support.v7.app.AppCompatActivity;
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;
public class MainActivity extends AppCompatActivity {
ListView listView;
TextView textView;
String[] listItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.listView);
textView=(TextView)findViewById(R.id.textView);
listItem = getResources().getStringArray(R.array.array_technology);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, listItem);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// TODO Auto-generated method stub
String value=adapter.getItem(position);
Toast.makeText(getApplicationContext(),value,Toast.LENGTH_SHORT).show();
}
});
}
}
No comments:
Post a Comment