安卓:getView适配器从来没有所谓的 [英] Android: getView in adapter never called
问题描述
不过我的歌厅周围的适配器是如何工作的头和结构。即时试图显示在XML文本(从服务器)和用户应该能够编辑该文本,然后将其发送回(到服务器)。与AutoCompleteTextView和EditText上有点困惑。这类似于:服务器发送一个粉(苹果,香蕉和芒果)。如果用户擦除芒果开始写的 BA ,香蕉又发表了AutoCompleteTextView。
但我的主要问题是getView不会被调用。
我有一种感觉它是与我如何申报适配器:arrayAdapter VS别的东西。
感谢您!
简单的活动
公共类EditMealActivity扩展ActionBarActivity { 私人餐mealList; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_edit_meal); // getMealById返回:芒果,苹果,香蕉
mealList = EditMealAdapter.getMealById(); ArrayAdapter适配器=新EditMealAdapter(这一点,R.layout.activity_edit_meal,mealList);
AutoCompleteTextView ACTV =(AutoCompleteTextView)findViewById(R.id.item1);
actv.setAdapter(适配器); } @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.edit_meal,菜单);
返回true;
} @覆盖
公共布尔onOptionsItemSelected(菜单项项){
//处理动作栏项目点击这里。操作栏会
//自动处理上点击主页/向上按钮,只要
//你在AndroidManifest.xml中指定一个父活动。
INT ID = item.getItemId();
如果(ID == R.id.action_settings){
返回true;
}
返回super.onOptionsItemSelected(项目);
}}
适配器连接到服务器
公共类EditMealAdapter扩展ArrayAdapter<膳食> { 私人餐单= NULL;
私有静态LayoutInflater吹气= NULL; 公共EditMealAdapter(上下文的背景下,INT layoutResourceID,
膳食_list){ 超(背景下,layoutResourceID);
this.list = _list;
} 公共静态餐getMealById(){
膳食mealList;
MealService mealService = MealServiceFactory.getMealService();
mealList = mealService.getMealByName();
返回mealList;
} // GETVIEW不会被调用??????? ************************
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
查看R = convertView;
如果(convertView == NULL)
{
充气= LayoutInflater.from(的getContext());
R = inflater.inflate(R.layout.activity_edit_meal,NULL);
} //如果这也许是:AutoCompleteTextView。用户是能写AP和苹果来了
的EditText foodItem1 =(EditText上)r.findViewById(R.id.item1);
的EditText gram1 =(EditText上)r.findViewById(R.id.grams1); foodItem1.setText(list.MealName);
gram1.setText(list.Meald); 返回ř;
}
}
下面是XML的EditText
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:ID =@ + ID /登入
机器人:方向=垂直
机器人:weightSum =1> < RelativeLayout的
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT>
<的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:textAppearance =机器人:ATTR / textAppearanceLarge
机器人:文字=大餐
机器人:ID =@ + ID / mealText
机器人:TEXTSIZE =20dp
机器人:文字颜色=#ff65706a
机器人:layout_marginTop =30dp
机器人:layout_gravity =center_vertical |左
机器人:layout_marginLeft =30dp/>
<的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:textAppearance =机器人:ATTR / textAppearanceLarge
机器人:文字=21克
机器人:ID =@ + ID / gramsText
机器人:TEXTSIZE =20dp
机器人:文字颜色=#ff65706a
机器人:layout_marginTop =30dp
机器人:layout_gravity =center_vertical |左
机器人:layout_marginLeft =200dp/>
< / RelativeLayout的>
<! - 在XML函数 - >
< RelativeLayout的
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT>
< AutoCompleteTextView
机器人:layout_width =140dp
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / ITEM1
安卓的inputType =textAutoComplete
机器人:imeOptions =actionNext
机器人:layout_alignParentLeft =真
机器人:layout_marginLeft =30dp>
< requestFocus的/>
< / AutoCompleteTextView> <的EditText
机器人:layout_width =70dp
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / grams1
机器人:imeOptions =actionNext
安卓的inputType =数字
机器人:layout_alignParentLeft =真
机器人:layout_marginLeft =200dp/> <按钮
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
安卓了minHeight =0dp
安卓了minWidth =0dp
机器人:可点击=真
机器人:\\文本=
机器人:ID =@ + ID / nutritionButton
机器人:layout_alignBottom =@ + ID / grams1
机器人:layout_toRightOf =@ + ID / grams1/>
< / RelativeLayout的> < RelativeLayout的
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:重力=底> <按钮
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=检查
机器人:ID =@ + ID / checkInButton
机器人:layout_gravity =CENTER_HORIZONTAL |底/> < / RelativeLayout的>
< / LinearLayout中>
您没有通过数组给超级
构造函数,以便在 ArrayAdapter
不能报告在其 getCount将()
实施项目的正确数目。
无论是覆盖 getCount将()
返回元素的数量阵列中,或使用的过载 ArrayAdapter
构造也需要在数组中
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>
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.
这篇关于安卓:getView适配器从来没有所谓的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!