从包含listview的活动中的edittext设置listview项目中textview的文本?

时间:2014-11-06 12:43:03

标签: android listview android-edittext textview

我有一个活动,表示创建成分列表并根据创建的列表搜索食谱,我想要做的是在用户插入文本并按下添加后从eduttext获取文本,然后添加文本出现在列表视图中,由于某种原因,文本不会改变,我不明白为什么。任何人都可以找到我的错误吗?

next to the x button there suppose to be text

列表视图的单项的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/ingredientItemTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginRight="62dp"
        android:layout_marginTop="37dp"
        android:textColor="@color/black"
        android:layout_toLeftOf="@+id/removeButton"
         />
    <Button
        android:id="@+id/removeButton"
        android:layout_width="40dip"
        android:layout_height="40dip"
        android:layout_alignBaseline="@+id/ingredientItemTv"
        android:layout_alignBottom="@+id/ingredientItemTv"
        android:layout_alignParentRight="true"
        android:background="@drawable/remove_icon"
        android:text="@string/remove" />
</RelativeLayout>

活动的

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg3"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/fridgeReipeTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
         android:textColor="@color/black"
        android:text="@string/fridgeReipeTitle" />
    <EditText
        android:id="@+id/ingredientEt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/fridgeReipeTv"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:ems="10"   
        />
    <Button
        android:id="@+id/ingAddBtn"
        android:layout_width="60dip"
        android:layout_height="40dip"
        android:layout_below="@+id/ingredientEt"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="27dp"
        android:background="@drawable/aqua_button"
        android:text="@string/addString" />

         <TextView
             android:id="@+id/recipeFindTv"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignParentBottom="true"
             android:layout_centerHorizontal="true"
             android:layout_marginTop="18dp"
             android:text="@string/findRecipe"
             android:textColor="@color/black" />
         <ListView
             android:id="@+id/lIngredientsListView"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_above="@+id/recFindBtn"
             android:layout_alignParentLeft="true"
             android:layout_below="@+id/ingAddBtn"
             android:layout_marginBottom="79dp" >
         </ListView>
         <Button
             android:id="@+id/recFindBtn"
             android:layout_width="50dip"
             android:layout_height="50dip"
             android:layout_alignParentBottom="true"
             android:layout_toRightOf="@+id/fridgeReipeTv"
             android:background="@drawable/find_icon"
             android:text="@string/find" />
</RelativeLayout>

活动的java

    package com.androidbegin.parselogintutorial;
            import java.util.ArrayList;
            import android.app.Activity;
            import android.content.Context;
            import android.os.Bundle;
            import android.util.Log;
            import android.view.View;
            import android.view.View.OnClickListener;
            import android.widget.AdapterView;
            import android.widget.AdapterView.OnItemClickListener;
            import android.widget.ArrayAdapter;
            import android.widget.Button;
            import android.widget.EditText;
            import android.widget.ListView;
            import android.widget.TextView;

            public class FridgeRecipe extends Activity
            {
                ListView ingsListView; 
                EditText ingredientEt;
                TextView mainTitleTv,endTileTv;
                Button addingBtn, searchBtn,removeBtn;
                ArrayList<IngrdientView> ingredients;
                int idInList = 0;
                IngredientsListAdapter ila;
                @Override
                protected void onCreate(Bundle savedInstanceState) 
                {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_fridge_recipe);
                    ingsListView = (ListView)findViewById(R.id.lIngredientsListView);
                    ingredientEt = (EditText)findViewById(R.id.ingredientEt);
                    mainTitleTv = (TextView)findViewById(R.id.fridgeReipeTv);
                    endTileTv = (TextView)findViewById(R.id.recipeFindTv);
                    addingBtn = (Button)findViewById(R.id.ingAddBtn);
                    searchBtn = (Button)findViewById(R.id.recFindBtn);
                    removeBtn = (Button)findViewById(R.id.removeButton);
                    ingredients = new ArrayList<IngrdientView>();
                    ila = new IngredientsListAdapter(FridgeRecipe.this, ingredients);
                    ingsListView.setAdapter(ila);
                    final String ingredient = ingredientEt.getText().toString();

                    addingBtn.setOnClickListener(new View.OnClickListener() 
                    {

                        @Override
                        public void onClick(View v) 
                        {
                            idInList++;
                            ingredients.add(new IngrdientView(ingredient,idInList));
                            ingredientEt.setText("");
                            ila.notifyDataSetChanged();

                        }
                    });



                    ingsListView.setOnItemClickListener(mMessageClickedHandler);

                }
                // Create a message handling object as an anonymous class.
                private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() 
                {
                    public void onItemClick(AdapterView parent, View v, int position, long id) 
                    {
                        if(v.getId() == R.id.removeButton)
                        {
                            ingredients.remove(position);


                        }
                    }
                };
            }

适配器的java:

package com.androidbegin.parselogintutorial;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;

public class IngredientsListAdapter extends ArrayAdapter<IngrdientView> implements ListAdapter, View.OnClickListener
    {

        Context context;
        ArrayList<IngrdientView> ingredients;

        public IngredientsListAdapter(Context context,ArrayList<IngrdientView> ingredients) 
        {
            super(context, 0);
            this.context = context;
            this.ingredients = ingredients;
        }

        @Override
        public void registerDataSetObserver(DataSetObserver observer) 
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) 
        {
            // TODO Auto-generated method stub

        }

        @Override
        public int getCount() 
        {
            // TODO Auto-generated method stub
            return ingredients.size();
        }

        @Override
        public IngrdientView getItem(int position) 
        {
            return ingredients.get(position);
        }

        @Override
        public long getItemId(int position) 
        {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public boolean hasStableIds() 
        {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            View root;
            if(convertView == null)
            {
                LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                root = inflater.inflate(R.layout.item_view, null);
            }
            else
            {
                root = convertView;
                TextView ingredientName = (TextView)root.findViewById(R.id.ingredientItemTv);
                Button removeBtn = (Button)root.findViewById(R.id.removeButton);
                IngrdientView ingView = ingredients.get(position);
                //ingredientName.setText(ingredientName.getText().toString());
                ingView.setIngredientName(ingredients.get(position).toString());
                removeBtn.setOnClickListener(this);
            }
            return root;
        }

        @Override
        public int getItemViewType(int position) 
        {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public int getViewTypeCount() 
        {
            // TODO Auto-generated method stub
            return 1;
        }

        @Override
        public boolean isEmpty() 
        {
            // TODO Auto-generated method stub
            return ingredients.size() == 0;
        }

        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub

        }

        @Override
        public boolean areAllItemsEnabled() 
        {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean isEnabled(int position) 
        {
            // TODO Auto-generated method stub
            return false;
        }

    }

3 个答案:

答案 0 :(得分:2)

我可以看到代码中哪一个错误的一点是你不对新创建的视图做任何事情。将您的代码更改为此代码并查看其是否发生了任何变化:

@Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View root;
        if(convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            root = inflater.inflate(R.layout.item_view, null);
       }
        else
        {
            root = convertView;
        }

            TextView ingredientName = (TextView)root.findViewById(R.id.ingredientItemTv);
            Button removeBtn = (Button)root.findViewById(R.id.removeButton);
            IngrdientView ingView = ingredients.get(position);
            //ingredientName.setText(ingredientName.getText().toString());
            ingView.setIngredientName(ingredients.get(position).toString());
            removeBtn.setOnClickListener(this);
        return root;
    }

你没有对新创建的视图做任何事情;你需要填充它。另一方面,您不应该在缓存的视图上重新订阅侦听器(即在您的其他视图之后)。你需要在第一次也是唯一一次创建视图的时候这样做(所以在if(convertView == null),否则你最终会得到多个回调(至少在其他编程语言中你会这样做)。

我还建议使用android称为“持有者模式”(Google it)的列表视图,以便顺利滚动,再次注意订阅。

我还建议您在添加新项目时确保调用getView()。从代码的简要概述来看,似乎确实如此,只是为了确保,放入一个断点并确保它被调用。

查看this开发人员指南,了解更多示例并进一步阅读。

也是你而不是你自己:)

答案 1 :(得分:0)

将文本添加到收藏列表(Arraylist或数组)中。修改列表视图适配器中的集合(listview_adapter.setCollection(您的集合))。然后调用listview_adapter.notifyDataSetChanged();

使用新的Collection列表重新初始化listview_adapter。并将适配器添加到列表视图中。

答案 2 :(得分:0)

我为其工作所做的更改是在适配器中进行的:

首先改变:

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    View root;
    if(convertView == null)
    {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        root = inflater.inflate(R.layout.item_view, null);
    }
    else
    {
        root = convertView;
        TextView ingredientName = (TextView)root.findViewById(R.id.ingredientItemTv);       
        ingredientName.setText(ingredients.get(position).getIngredientName()+"");
        Button removeBtn = (Button)root.findViewById(R.id.removeButton);
        removeBtn.setTag(position);
        //IngrdientView ingView = ingredients.get(position);
        removeBtn.setOnClickListener(this);

    }
    return root;
}

第二次改变:

    @Override
    public void onClick(View v) 
    {
        int index = (Integer)v.getTag();
        IngrdientView data = ingredients.get(index);
        ingredients.remove(data);
//      ((AdapterView)adapterView).setAdapter(null);
//      ((AdapterView)adapterView).setAdapter(this);
        this.notifyDataSetChanged();
    }

希望这可以帮助某人...