3

Still geting my head around how adapters work and the structure. Im trying to show text (from server) in xml and the user should be able to edit that text and then send it back (to server). Little confused with AutoCompleteTextView and EditText. This works like: Server sends a meal (apple, banana and mango). If user erase mango and start to write ba, banana comes up with AutoCompleteTextView.

But my main problem is that getView is never called. I have a feeling it has something to do with how i declare the adapter: arrayAdapter vs something else.

Thank you!

simple activity

public class EditMealActivity extends ActionBarActivity {

    private Meal mealList;

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

        // getMealById returns: mango, apple, banana
        mealList = EditMealAdapter.getMealById();

        ArrayAdapter adapter = new EditMealAdapter(this, R.layout.activity_edit_meal, mealList);
        AutoCompleteTextView actv = (AutoCompleteTextView)findViewById(R.id.item1);
        actv.setAdapter(adapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.edit_meal, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

Adapter that connects to server

public class EditMealAdapter extends ArrayAdapter<Meal> {

    private Meal list = null;
    private static LayoutInflater inflater=null;

    public EditMealAdapter(Context context, int layoutResourceID,
                           Meal _list){

        super(context, layoutResourceID);
        this.list = _list;
    }

    public static Meal getMealById(){
        Meal mealList;
        MealService mealService = MealServiceFactory.getMealService();
        mealList = mealService.getMealByName();
        return mealList;
    }

    // GETVIEW IS NEVER CALLED??????? ************************
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View r = convertView;
        if(convertView == null)
        {
            inflater = LayoutInflater.from(getContext());
            r = inflater.inflate(R.layout.activity_edit_meal, null);
        }

    // Should this maybe be: AutoCompleteTextView. User is to be able to write ap and apple comes up
        EditText foodItem1 = (EditText) r.findViewById(R.id.item1);
        EditText gram1 = (EditText) r.findViewById(R.id.grams1);

        foodItem1.setText(list.MealName);
        gram1.setText(list.Meald);

        return r;
    }
}

Here is EditText xml

<?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="match_parent"
    android:id="@+id/checkin"
    android:orientation="vertical"
    android:weightSum="1">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Meal"
            android:id="@+id/mealText"
            android:textSize="20dp"
            android:textColor="#ff65706a"
            android:layout_marginTop="30dp"
            android:layout_gravity="center_vertical|left"
            android:layout_marginLeft="30dp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Grams"
            android:id="@+id/gramsText"
            android:textSize="20dp"
            android:textColor="#ff65706a"
            android:layout_marginTop="30dp"
            android:layout_gravity="center_vertical|left"
            android:layout_marginLeft="200dp" />
    </RelativeLayout>


    <!-- The xml function -->
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <AutoCompleteTextView
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:id="@+id/item1"
            android:inputType="textAutoComplete"
            android:imeOptions="actionNext"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="30dp" >
            <requestFocus/>
        </AutoCompleteTextView>

        <EditText
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:id="@+id/grams1"
            android:imeOptions="actionNext"
            android:inputType="number"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="200dp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minHeight="0dp"
            android:minWidth="0dp"
            android:clickable="true"
            android:text="\?"
            android:id="@+id/nutritionButton"
            android:layout_alignBottom="@+id/grams1"
            android:layout_toRightOf="@+id/grams1" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="bottom">

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Check in"
               android:id="@+id/checkInButton"
            android:layout_gravity="center_horizontal|bottom"/>

    </RelativeLayout>
</LinearLayout>
Sindri Þór
  • 2,887
  • 3
  • 26
  • 32

3 Answers3

3

You didn't pass the array to the super constructor so the ArrayAdapter cannot report the correct number of items in its getCount() implementation.

Either override getCount() to return the number of elements in the array, or use an overload of the ArrayAdapter constructor that also takes in the array.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • That is a good point! But when I return list.size I get "Cannot resolve method size()" – Sindri Þór Aug 07 '14 at 14:59
  • You have only one `Meal` object there and not an array, so returning 1 would work at first. – laalto Aug 07 '14 at 15:18
  • Yes i think you are right about that. But either getCount or getView does not get called yet.. Any idea? – Sindri Þór Aug 07 '14 at 15:30
  • Well I have found out how to find the size. And there was no way for you to have known it. One of the items 'Ingredients' in meal. I had to find the size of that. so its list.Ingredients.size(); But it never gets called, witch i dont understand quite yet. – Sindri Þór Aug 07 '14 at 15:40
1

You are using a different overloaded constructor than your actual constructor. That's fine however you need to Override the getCount method as at the moment it always returns 0.

Here's how:

@Override
public int getCount() {
    return list.size();
}

EDIT:

Now I noticed you are using a custom class Meal. You need to track it's count.

If the only items that it has is mango, apple, banana then you can simply return 3;

EDIT2:

The problem is that your Meal class cannot loop through objects, this proves it: foodItem1.setText(list.MealName);.

You want your class to be loopable like list.get(0).MealName, list.get(1).MealName...

Perhaps what you want is a list of Meal objects: List<Meal> meals = getMeals();.

At the moment (even if you could get the meal count), all your views will be the same as list.MealName and list.Meald doesn't change. Hope this somewhat clears it up.

Simas
  • 43,548
  • 10
  • 88
  • 116
  • I see, but I am only using a mock database at the moment. I would simple never know how many items in the feature. So i guess i need to fix my constructer. I tried to add _list to super(...) in the constructor but then i get "cannot resolve....." Any idea? – Sindri Þór Aug 07 '14 at 15:11
  • You are misunderstanding how adapters work. They take a bunch of items and use them to create Views which are then displayed in a ListView or what not. **You must have the items before the adapter can give you anything back**. – Simas Aug 07 '14 at 15:13
  • Maybe I am missunderstanding how this all works but i think you are wrong, no disrespect. In activity: mealList has 3 items of varueble Meal, not array or list. So i can not take a list.size (i should have renamed it something else then list). Maybe its becouse the adapter is arrayadapter. But to answer your not-question: i do send data to adapter, its a Meal, not List or ArrayList. – Sindri Þór Aug 07 '14 at 15:26
  • @SindriÞór updated hopefully you can make some sense of it :-) – Simas Aug 07 '14 at 15:40
  • Well I have found out how to find the size. And there was no way for you to have known it. One of the items 'Ingredients' in meal. I had to find the size of that. so its list.Ingredients.size(); But it never gets called, witch i dont understand quite yet. – Sindri Þór Aug 07 '14 at 15:41
0

Normallly we populate data and than pass that data to the customAdapter something like following psuedo code:

data=getData();
CustomAdapter adapter=new CustomAdapter(context,data);

and inside custom adapter make sure you have override getCount() method properly.

normally it is implemented as this:

    getCount(){
       data.size()
    }
rupesh jain
  • 3,410
  • 1
  • 14
  • 22