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>