3

仍然对适配器的工作方式和结构有所了解。我试图在 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>
4

3 回答 3

3

您没有将数组传递给super构造函数,因此 ArrayAdapter无法在其getCount()实现中报告正确的项目数。

要么覆盖getCount()以返回数组中元素的数量,要么使用ArrayAdapter也接受数组的构造函数的重载。

于 2014-08-07T14:52:09.693 回答
1

您使用的重载构造函数与实际构造函数不同。没关系,但是您需要覆盖 getCount 方法,因为它始终返回 0。

就是这样:

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

编辑:

现在我注意到您正在使用自定义类Meal。您需要跟踪它的计数。

如果它只有芒果、苹果、香蕉,那么你可以简单地return 3;

编辑2:

问题是您的 Meal 类不能循环遍历对象,这证明了这一点: foodItem1.setText(list.MealName);.

您希望您的课程可以像list.get(0).MealName, list.get(1).MealName...

也许您想要的是 Meal 对象列表:List<Meal> meals = getMeals();.

此刻(即使你能得到餐数),你所有的观点都将保持不变list.MealNamelist.Meald不会改变。希望这有点清除它。

于 2014-08-07T14:56:54.090 回答
0

通常我们填充数据,然后将该数据传递给 customAdapter,类似于以下伪代码:

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

并在自定义适配器内部确保您正确覆盖了 getCount() 方法。

通常它是这样实现的:

    getCount(){
       data.size()
    }
于 2014-08-07T15:14:21.717 回答