Android Custom Adapter menu

User Tag List

Results 1 to 1 of 1
  1. #1
    Veritable's Avatar OwnedCore News Correspondent
    Reputation
    326
    Join Date
    Apr 2007
    Posts
    372
    Thanks G/R
    52/123
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Android Custom Adapter

    Hello there, so I am attempting to get back to the android app side of things now that the server has a new feature, and I have to add in a bunch of cool new functions to the application for Android.

    But what I didn't expect, is that since I haven't done much work in the last 4+ months on Java / Android, I have forgotten quite a few things. I thought that I would put a tidbit of information that I have been using over and over again in respects to displaying data within the application.

    The method I have chosen to go with, is a Custom Adapter.

    In the following example, I am going to use a Spinner (Drop Down Box) to display Information from a SQLite database.

    I am going to assume that you have the retrieval of data setup already for the database, so all we are going to do is pretend that we get a set of data back so we can work with it.

    Whenever I want to rebuild the list from the database, I call this function. For now we'll just populate it with the 3 items we are going to use for testing, rather than doing the actual database calls... but I will leave my cursor intact (commented out) so you can see what it looks like:

    Code:
        private void RebuildServerAdapter() {
    
    
            //Cursor d = dbAdapter.selectData("SELECT * FROM "+dbAdapter.helper.ConTable+" WHERE 1", null);
            ServerInfo[] serverNames = new ServerInfo[3];
            for(int i = 0; i < serverNames.length; i++) {
                ServerInfo t = new ServerInfo();
                t.Name = "Server " + i;
                t.Host = "192.168.1."+i+100;
                t.Port = "57953";
                Log.d("ServerRebuild: ",t.Name);
                serverNames[i] = t;
            }
            Spinner s = this.findViewById(R.id.ServerDropDown);
            ServerSpinnerAdapter adapter = new ServerSpinnerAdapter(context, serverNames);
            s.setAdapter(adapter);
        }
    Now the really cool part. The actual Adapter for the spinner. you can see that we have a ServerSpinnerAdapter class. This contains everything we need to create the actual drop down. So i'll post the code for the adapter in here.

    Code:
    import android.content.Context;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;
    
    
    public class ServerSpinnerAdapter extends BaseAdapter {
        Context context;
        ServerInfo[] serverNames;
        LayoutInflater inflater;
        
        // local variables assigned in the constructor
        public ServerSpinnerAdapter(Context applicationContext, ServerInfo[] _serverNames) {
            this.context = applicationContext;
            this.serverNames = _serverNames;
            inflater = (LayoutInflater.from(applicationContext));
        }
    
    
        @Override
        public int getCount() {
            return serverNames.length;
        }
    
    
        @Override
        public Object getItem(int i) {
            return null;
        }
    
    
        @Override
        public long getItemId(int i) {
            return 0;
        }
    
        // This is where it actually 'draws' the data. So we have to provide each row, with data based on the Layout we have created.
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = inflter.inflate(R.layout.server_spinner_items, null);
            TextView names = view.findViewById(R.id.txtServerName);
            Log.d("Inflator: ", i + " - Value is "+serverNames[i].Name); // Log Note so I can see it working
            names.setText(serverNames[i].Name); // Set the txtServerName TextView in the Layout to the name value in the data array
            return view;
        }
    }
    So the only thing left is the actual Layout. Now this is just a row, it is not the whole spinner, it just tells it what the row actually looks like. You could add lots of things in here, like images and what not to fancy it up, but for now I am just adding the Name of the server.

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
    
        <TextView
            android:id="@+id/txtServerName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="2dp"
            android:textColor="#000" />
    </LinearLayout>
    So you can that it has a single TextView element, with the name txtServerName.

    Every time it goes through the items in the arrayadapter we specify, it will take each item (the serverNames[i].Name in the inflator section) and dump that associated "Name" text value into that Spinner row.

    To add an image, you could just use an image in the app itself, and pass the resource ID's in the constructor, so it may look like this:

    Code:
    public ServerSpinnerAdapter(Context applicationContext, int[] imageResources, ServerInfo[] _serverNames) {
    You could do something like, have a image reference in the ServerInfo class, that when you build the ImageView section, your resource ID would be associated with whatever R.id.image222 whatever your image is. And in the inflator, you have to set the ImageView value to that id. There are other tutorials that show this (Ones with flags from what I have seen) on what exactly that looks like. But the steps are all there.

    You have a Layout which contains the Spinner row (in this case, but this could also be a ListView row).
    You have a Custom Adapter that contains a Constructor to pass variables for the Spinner as well as the View function override for creating the individual rows based on the data provided.
    You have the rebuilding of the spinner row. I use the function because it's not a string in the app, but rather a database. If you wanted to, you could just modify an array of strings, (ArrayList) and you could call a method:

    Code:
    adapter.notifyDataSetChanged();
    and it will reload the data in the array to the adapter. (refresh)

    Hope that helps, and if you have any other questions or need clarification, don't hesitate to post.
    Last edited by Veritable; 12-21-2017 at 03:36 PM.

    Android Custom Adapter

Similar Threads

  1. mmowned special: custom item names
    By Relz in forum World of Warcraft Guides
    Replies: 58
    Last Post: 09-27-2006, 11:51 PM
  2. CUSTOM RACE TO RACE model conversion now avaible, request here!
    By bloodofwar in forum World of Warcraft Model Editing
    Replies: 13
    Last Post: 08-22-2006, 04:20 PM
  3. Custom RACE to RACE conversion now avable! request here!
    By bloodofwar in forum WoW ME Questions and Requests
    Replies: 0
    Last Post: 08-20-2006, 09:17 PM
  4. Custom Computer Build (opinions/comments?)
    By Matt in forum Community Chat
    Replies: 11
    Last Post: 07-23-2006, 12:57 PM
  5. New Custom Model Swapping Fix (1.11 Working)
    By Matt in forum World of Warcraft Exploits
    Replies: 5
    Last Post: 06-23-2006, 06:05 PM
All times are GMT -5. The time now is 08:00 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search