[英]setting text of a textview in an item of a listview from an edittext in the activity containing the listview?
我有一个代表成分列表创建并根据创建的列表搜索配方的活动,我要做的是在用户插入文本并按添加后从eduttext中获取文本,然后使添加的文本出现在列表视图,由于某种原因,文本没有改变,我也不知道为什么。 谁能找到我的错误?
列表视图中单个项目的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>
活动的 xml:
<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;
}
}
我可以看到的一件事是您的代码有问题,那就是您不对新创建的视图执行任何操作。 将您的代码更改为此,看看它是否有任何改变:
@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)
在if(convertView == null)
否则最终将获得多个回调(至少会使用其他编程语言)。
我还建议您为列表视图使用android所谓的“持有人模式”(Google),以允许平滑滚动,并且再次注意订阅。
我还建议您确保在添加新项目时调用getView()。 从您的代码的简要概述看来,确实是这样,但是只是要确保将其置于断点并确保已调用它。
查看此开发者指南,以获取更多示例和进一步的阅读。
也是你的,而不是你的:)
将文本添加到“收藏夹”列表(Arraylist或数组)中。 在列表视图适配器中修改集合(listview_adapter.setCollection(您的集合))。 然后调用listview_adapter.notifyDataSetChanged();
要么
用新的收藏夹列表重新初始化listview_adapter。 并将适配器添加到列表视图。
我所做的更改是在适配器中进行的:
第一个变化:
@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();
}
希望这对某人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.