Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: getView in adapter never called

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>
like image 526
Sindri Þór Avatar asked May 01 '26 20:05

Sindri Þór


2 Answers

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.

like image 197
laalto Avatar answered May 03 '26 10:05

laalto


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.

like image 35
Simas Avatar answered May 03 '26 10:05

Simas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!