GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — NB_Android/code_45-48_list_views.txt at master · glwhitney/NB_Android Expand file tree/ Copy pathcode_45-48_list_views.txtMore file actions 213 lines (179 loc) · 8.85 KB / Copy pathcode_45-48_list_views.txtFile metadata and controls213 lines (179 loc) · 8.85 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 Android Development Tutorials 45 thru 48 - ListViews ***** Tutorial Notes ** 45 - ListView Start a new Empty Activity App and delete Hello World. Add a ListView. NOTE: Bucky extends Activity because he doesn't want an ActionBar in his layout. I tried this and it made no difference because the theme defined in styles.xml was Theme.AppCompat.Light.DarkActionBar. Leave the extends AppCompatActivity and change the theme toTheme.AppCompat.Light.NoActionBar. 1. Layout a. Delete the TextView and add a ListView. b. Set the width and height to match_Parent c. Give it the ID buckysListView 2. Main.java a. Import 5 items b. in on create make an array of foods c. started to make a ListAdapter using an Array Adapter to take the array foods[] and map it to the ListView. ** 46 - ListView Example 1. Make a ListAdapter using an ArrayAdapter(content, format, array) 2. Get a ref to the ListVview 3. setAdapter(the adapter) 4. Run the app, the list appears but doesn’t respond to clicks. 5. setItemClickListener 6. get the value of the selected item using parent and position 7. make a toast. NOTE: If you rapidly click multiple items, the Toasts are queued. ** 47 - Custom ListView Row In the last tutorial we used a built-in layout for the row items. In this one we are going to make a custom layout. Here we are going to use the same image every time but we are going to change the text to the right of it. If you were going to have different images you would need to create an array of image ids and coordinate with the text array. 1. Keep using the App from the last tutorial. Make a new layout called custom_row. 2. Grab and image from internet of at least 100 x100 a. Store it in drawable-hdpi // I stored mine in drawable. NOTE: Studio 2.0 doesn't make all those drawable folders for different resolutions. So just drop the image in drawable. Tip! Go to the android forum and copy Bucky's image, then paste it into Paint and save it in drawable. 3. Create a new xml for the rows and use a linear layout a. Change the linear layout orientation to horizontal i.e. left to right b. add an imageView and set it's src attribute, without the file extension. c. add a largeText with the id buckysText d. give them both some padding 5 dp ** 48 - Custom ListView Adapter Now we need a custom adapter for our custom list item , to replace the one we used last time used android.R.layout.simple_list_item_1 1. Create a new (non public) class that extends arrayAdapter<String> a. Use Alt-Insert for the constructor template NOTE: Bucky is making a custom constructor using a 2 parameters template but is actually passing 3 parameters to the Super. Tip! Select the constructor with 3 parameters and object [] is the last parameter. b. Context context means the current context i.e. App environment. c. Replace the second parameter with a String array called foods. d. Replace resource argument in the call to super with R.layout.custom_row e. Replace array argument in the call to super with foods 2. Now tell it what view to use by overriding getView() and deleting the default return a. Inflate the custom_view layout. b. Get references to the custom row views i. Get a ref to a single food Item using the position. ii. Get a ref, custom layout TextView iii. Get a ref to custom layout image. Note: I didn't bother to change the default ID. c. Set the text and the image for the custom row d. Return the custom view 3. Change the List Adapter in MainActivity.java to use the new Custom Adapter class. 4. Run the App with the new custom row. NOTE: If you want to have a different image for each row. After looking at the constructors available for ArrayAdapter, I think you will need to: 1. add more images to drawable 2. Create a new object that holds both the image and the text 3. Create an array of those objects to pass to Custom View. ***** manifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.thenewboston.listexample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> ***** MainActivity.java package com.thenewboston.listexample; 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.ListAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String[] foods = {"Bacon", "Ham", "Tuna", "Candy", "Meatball", "Potato"}; /* Tutorial 46 ListAdapter buckysAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, foods); */ ListAdapter buckysAdapter = new CustomAdapter(this,foods);// Pass the food array to the constructor. ListView buckysListView = (ListView) findViewById(R.id.buckysListView); buckysListView.setAdapter(buckysAdapter); buckysListView.setOnItemClickListener( new AdapterView.OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String food = String.valueOf(parent.getItemAtPosition(position));// parent is the ListView Toast.makeText(MainActivity.this, food, Toast.LENGTH_LONG).show(); } } ); } } ***** activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.thenewboston.listexample.MainActivity"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/buckysListView" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout> ***** CustomAdapter.java package com.thenewboston.listexample; 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; /** * Created by Gary on 5/15/2016. */ class CustomAdapter extends ArrayAdapter<String> { public CustomAdapter(Context context, String[] foods) { super(context,R.layout.custom_row, foods); } @Override public View getView(int position, View convertView, ViewGroup parent) { // default - return super.getView(position, convertView, parent); // add the layout LayoutInflater buckysInflater = LayoutInflater.from(getContext()); View customView = buckysInflater.inflate(R.layout.custom_row, parent, false); // get references. String singleFoodItem = getItem(position); TextView itemText = (TextView) customView.findViewById(R.id.buckysText); ImageView buckysImage = (ImageView) customView.findViewById(R.id.imageView); // dynamically update the text from the array itemText.setText(singleFoodItem); // using the same image every time buckysImage.setImageResource(R.drawable.chuncky); // Now we can finally return our custom View or custom item return customView; } } ***** custom_row.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="80dp" android:layout_height="80dp" android:id="@+id/imageView" android:src="@drawable/chuncky" android:layout_margin="5dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Large Text" android:id="@+id/buckysText" android:layout_margin="5dp" /> </LinearLayout> |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |