我有一个listview,我有一些textview和editText。我想要的是当editText中的文本被更改时,它应该更新listview中的相应textview。但问题是它正在更新所有listItems in列表视图。我的getview代码是这样的:
import java.util.ArrayList;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class ListAdapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<LItem> lstItems;
TextView tvAQuantity , tvBQuantity;
EditText etQuantity;
LItem p;
ViewHolder holder;
ListAdapter(Context context, ArrayList<LItem> objects) {
ctx = context;
lstItems = objects;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return lstItems.size();
}
@Override
public Object getItem(int position) {
return lstItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.list_style_task
, parent, false);
holder = new ViewHolder();
holder.tvAQuan = (TextView) view.findViewById(R.id.tvAQuantity);
holder.tvBQuan=(TextView)view.findViewById(R.id.tvBQuantity);
holder.etQuan=(EditText)view.findViewById(R.id.etTaskQuantity);
holder.tvMatN= (TextView) view.findViewById(R.id.tvMaterial ) ;
holder.tvTksN= (TextView) view.findViewById(R.id.tvTask ) ;
view.setTag(holder);
// tvAQuantity.setText(p.getAQ());
// etQuantity.setTag(position);
}
else {
holder=(ViewHolder)view.getTag();
}
p = getProduct(position);
if(p!=null)
{
if(holder.tvAQuan!=null)
{
holder.tvAQuan.setText(p.getAQ());
holder.tvBQuan.setText(p.getBQ());
holder.tvMatN.setText(p.getMName());
holder.tvTksN.setText(p.getTName());
holder. etQuan.setTag(position);
holder.etQuan.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
p.setAQ( holder.etQuan.getText().toString());
notifyDataSetChanged();
}
}
});
}
}
// CheckBox cbBuy = (CheckBox) view.findViewById(R.id.checkbox);
//cbBuy.setOnCheckedChangeListener(myCheckChangList);
// cbBuy.setTag(position);
// cbBuy.setChecked(p.selected);
return view;
}
LItem getProduct(int position)
{
return ((LItem) getItem(position));
}
ArrayList<LItem> getBox() {
ArrayList<LItem> box = new ArrayList<LItem>();
for (LItem p : lstItems) {
// if (p.selected)
// box.add(p);
}
return box;
}
OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
//getProduct((Integer) buttonView.getTag())= isChecked;
}
};
public static class ViewHolder{
public TextView tvAQuan;
public TextView tvBQuan;
public EditText etQuan;
public TextView tvMatN;
public TextView tvTksN;
}
}
答案 0 :(得分:1)
问题是您处理的getView方法有误。在这里,我将添加一个示例代码。
第1步
你需要一个像这样的viewHolder类。在这里添加你的膨胀的xml小部件:
/********* Create a holder Class to contain inflated xml file elements *********/
public static class ViewHolder{
public TextView text;
public TextView text1;
public TextView textWide;
public ImageView image;
}
第2步
在你的getView方法
中/****** Depends upon data size called for each row , Create each ListView row *****/
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if(convertView==null){
/****** Inflate tabitem.xml file for each row ( Defined below ) *******/
vi = inflater.inflate(R.layout.tabitem, null);
/****** View Holder Object to contain tabitem.xml file elements ******/
holder = new ViewHolder();
holder.text = (TextView) vi.findViewById(R.id.text);
holder.text1=(TextView)vi.findViewById(R.id.text1);
holder.image=(ImageView)vi.findViewById(R.id.image);
/************ Set holder with LayoutInflater ************/
vi.setTag( holder );
}
else
holder=(ViewHolder)vi.getTag();
return vi;
}
这是示例问任何疑问
修改1:
在您的情况下,像这样更改你的getView
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = lInflater.inflate(R.layout.list_style_task
, parent, false);
holder .tvAQuantity=((TextView) view.findViewById(R.id.tvAQuantity )) ;
holder .tvMaterial = (TextView) view.findViewById(R.id.tvMaterial );
holder.tvTask = (TextView) view.findViewById(R.id.tvTask );
holder . tvBQuantity=((TextView) view.findViewById(R.id.tvBQuantity ));
holder. etQuantity=(EditText)view.findViewById(R.id.etTaskQuantity);
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
p = getProduct(position);
holder .tvMaterial.setText(p.getMName() ;
holder.tvTask.setText(p.getTName() );
holder.tvBQuantity.setText(p.getBQ());
holder.tvAQuantity.setText(p.getAQ());
holder.etQuantity.setTag(position);
return view;
}
创建一个ViewHolder类
public static class ViewHolder{
public TextView tvAQuantity, tvTask;
public TextView tvMaterial;
public TextView textWide;
public EditText etQuantity;
}
注意:
我认为您不需要以下这一行。
holder.etQuantity.setTag(position);
只需评论此行并尝试。
这是样品询问是否需要任何帮助
编辑2:
我发现了这个问题。每当焦点更改并默认第一次时,都会调用setOnFocusChangeListener。因此,一旦您在editText中输入任何内容,您的所有项目都会自动更新。所以你需要使用其他方法,如TExtChanged Listeneer。
另一个问题是你从p获取数据意味着getProduct。并再次将值设置为getProduct。这样做的目的是什么 ?。我认为你需要在这里改变你的逻辑。
答案 1 :(得分:1)
持有者中定义的EditText在哪里,首先将视图持有者中定义的变量etQuan的声明更改为EditText而不是textview。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.list_style_task
, parent, false);
holder = new ViewHolder();
holder.tvAQuan = (TextView) view.findViewById(R.id.tvAQuantity);
holder.tvBQuan=(TextView)view.findViewById(R.id.tvBQuantity);
holder.tvMatN= (TextView) view.findViewById(R.id.tvMaterial ) ;
holder.tvTksN= (TextView) view.findViewById(R.id.tvTask ) ;
view.setTag(holder);
// tvAQuantity.setText(p.getAQ());
// etQuantity.setTag(position);
}
else {
holder=(ViewHolder)view.getTag();
}
// edited here
EditText etQuan=(EditText)view.findViewById(R.id.etTaskQuantity);
etQuan.setTag(position);
etQuan.setOnClickListener(ctx);
p = getProduct(position);
if(p!=null)
{
if(holder.tvAQuan!=null)
{
holder.tvAQuan.setText(p.getAQ());
holder.tvBQuan.setText(p.getBQ());
holder.tvMatN.setText(p.getMName());
holder.tvTksN.setText(p.getTName());
holder. etQuan.setTag(position);
holder.etQuan.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
p.setAQ( holder.etQuan.getText().toString());
notifyDataSetChanged();
}
}
});
}
}
// CheckBox cbBuy = (CheckBox) view.findViewById(R.id.checkbox);
//cbBuy.setOnCheckedChangeListener(myCheckChangList);
// cbBuy.setTag(position);
// cbBuy.setChecked(p.selected);
return view;
}
然后在您使用此自定义适配器的Activity上执行此操作。
public class Activityname extends ListActivity implements
android.view.View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourviewname);
//rest code of your activity
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.etTaskQuantity) {
EditText edttxt = (EditText) v;
LItem obj = (LItem) ListAdapter.getItem((Integer) edttxt
.getTag());
// with edttxt do what need to add
}
答案 2 :(得分:0)
解决后需要首先获取视图的父布局:
LinearLayout rl = (LinearLayout)v.getParent();
我使用按钮点击而不是编辑文本的焦点更改来修改代码。
final int posi=position;
holder.btnRefreshQuantity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout rl = (LinearLayout)v.getParent();
holder.tvAQuan = (TextView) rl .findViewById(R.id.tvAQuantity);
holder.etQuan=(EditText)rl.findViewById(R.id.etTaskQuantity);
p = getProduct(posi);
p.setAQ( holder.etQuan.getText().toString() );
String s= holder.etQuan.getText().toString();
// Log.d("etQuanText", holder.etQuan.getText().toString());
// Log.d("aq",Integer.toString(posi));
notifyDataSetChanged();
}
});