Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.sarasas;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.app.Activity;
- import android.app.ListActivity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.ListView;
- import android.widget.Toast;
- import android.widget.ArrayAdapter;
- import java.util.ArrayList;
- public class MainActivity extends Activity {
- ListView listView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_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",
- "Solaris",
- "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
- //Adapteris adapter = new ArrayAdapter (this, R.layout.eilute, android.R.id.text1, values);
- Adapteris adapter = new Adapteris(this,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(),
- "Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
- .show();
- }
- });
- }
- }
- package com.example.sarasas;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ArrayAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
- public class Adapteris extends ArrayAdapter {
- private final Context context;
- private final String[] values;
- public Adapteris (Context context, String[] values) {
- super(context, R.layout.eilute, values);
- this.context = context;
- this.values = values;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- View rowView = inflater.inflate(R.layout.eilute, parent, false);
- TextView textView = (TextView) rowView.findViewById(R.id.label);
- ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
- textView.setText(values[position]);
- // Change the icon for Windows and iPhone
- String s = values[position];
- if (s.startsWith("Android Example") || s.startsWith("Android List View")
- || s.startsWith("Solaris")) {
- imageView.setImageResource(R.drawable.no);
- } else {
- imageView.setImageResource(R.drawable.ok);
- }
- return rowView;
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ImageView
- android:id="@+id/icon"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- app:srcCompat="@android:drawable/btn_star_big_on" />
- <TextView
- android:id="@+id/label"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="@+id/label"
- android:textSize="40px"/>
- </LinearLayout>
- <?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=".MainActivity">
- <ListView
- android:id="@+id/list"
- android:layout_width="395dp"
- android:layout_height="715dp"
- tools:layout_editor_absoluteX="16dp" />
- </android.support.constraint.ConstraintLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement