仍然对适配器的工作方式和结构有所了解。我试图在 xml 中显示文本(来自服务器),用户应该能够编辑该文本,然后将其发送回(到服务器)。与 AutoCompleteTextView 和 EditText 有点混淆。这就像:服务器发送一顿饭(苹果、香蕉和芒果)。如果用户擦除 mango 并开始编写ba,banana 会出现 AutoCompleteTextView。
但我的主要问题是永远不会调用 getView。我觉得这与我声明适配器的方式有关:arrayAdapter 与其他东西。
谢谢!
简单的活动
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);
}
}
连接到服务器的适配器
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;
}
}
这是 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>