简体   繁体   English

我有一个ListView,其中每一行都包含一个EditText字段和textview,我在Edittext中键入的内容我想在textview中看到什么?

[英]I have a ListView, where each row contains an EditText field and textview, what ever i typed in the Edittext i want to see in the textview?

Here i have the following code below :- 在这里我有下面的代码如下:

public class OrderActivity extends ListActivity implements OnClickListener{

ArrayList<String> data,rate, num;
OrderAdapter orderAdapter;
int i = 0;
int s;
TextView tvNumber,tvName,tvRate;
EditText etQty,etRemarks;
Button sendToConfirm,edit,saveAndAdd;
//  EditText qty,remarks;
//OrderAdapter orderAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.order);
    Init();

Here i get arraylists from previous activity 在这里,我从以前的活动中获得了数组列表

    Bundle extras = getIntent().getExtras(); 
    data = extras.getStringArrayList("list");
    rate = extras.getStringArrayList("rate");
    num = extras.getStringArrayList("number");
    for(String str : data)  {
        Log.d("data", str);

    }
    for(String str1 : rate) {
        Log.d("data", str1);
        //s = Integer.parseInt(st)

    }
    //Log.d("total", s);
    for(String strnum : num)    {
        Log.d("data", strnum);

    }


    String[] captionArray = (String[]) data.toArray(
            new String[data.size()]);

    orderAdapter = new OrderAdapter(
            OrderActivity.this, R.layout.order_list,
            captionArray);
    setListAdapter(orderAdapter);
    return ;
}

Initialize the buttons 初始化按钮

public void Init() {
    sendToConfirm = (Button) findViewById(R.id.bsendtoconfirm);
    edit = (Button) findViewById(R.id.bEdit);
    saveAndAdd = (Button) findViewById(R.id.bsave);
    sendToConfirm.setOnClickListener(this);
    edit.setOnClickListener(this);
    saveAndAdd.setOnClickListener(this);
    //etQty.addTextChangedListener(this);
}

Here is the baseAdapter class. 这是baseAdapter类。

private class OrderAdapter extends BaseAdapter {
    String[] items;

    public OrderAdapter(Context context, int textViewResourceId,
            String[] items) {
        this.items = items;
    } 


    public View getView(final int POSITION, View convertView,
            ViewGroup parent) {

        //          TextView tvNumber,tvName,tvRate;
        //          EditText etQty,etRemarks;

        View view = convertView;

        if (view == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.order_list, null);

        } 
        tvNumber = (TextView) view.findViewById(R.id.itemnumber);
        tvName = (TextView) view.findViewById(R.id.itemname);
        tvRate = (TextView) view.findViewById(R.id.itemrate);
        etQty = (EditText) view.findViewById(R.id.editqty);
        etRemarks = (EditText) view.findViewById(R.id.editremarks);
        etQty = (EditText) view.findViewById(R.id.editqty);
        //tvRate = (TextView) v.findViewById(R.id.itemrate);

Ontextchange listener . Ontextchange侦听器。 here whatever i type in the edittext i need to get in the textview ?please help me out with this. 在这里,无论我在edittext中键入什么内容,都需要进入textview吗?请帮我解决这个问题。

        etQty.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                String text;
                text=etQty.getText().toString();
                if(text!=null)
                {
                    tvRate.setText(text);
                }
                else {
                    tvRate.setText("You Have Not type anything");
                }
            }
        });

        tvNumber.setText(num.get(POSITION));
        tvName.setText(data.get(POSITION));
        tvRate.setText(rate.get(POSITION));


        return view; 
    }


    public int getCount() {
        // TODO Auto-generated method stub
        return items.length;
    }


    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }


    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
}
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.bsendtoconfirm:
        Log.d("click", "send");
        break;

    case R.id.bEdit:
        Log.d("click", "edit");
        break;
    case R.id.bsave:
        Log.d("click", "saved");
        finish();
        break;
    }
}
//  public void afterTextChanged(Editable s) {
//      // TODO Auto-generated method stub
////        String text;
////        text=etQty.getText().toString();
////        if(text!=null)
////        {
////        tvRate.setText(text);
////        }
////        else {
////        tvRate.setText("You Have Not type anything");
////        }
//  }
//  public void beforeTextChanged(CharSequence s, int start, int count,
//          int after) {
//      // TODO Auto-generated method stub
//
//  }
//  public void onTextChanged(CharSequence s, int start, int before, int count) {
//      // TODO Auto-generated method stub
//
//  }

} }

hi you can use the code below to do this, 您好,您可以使用以下代码执行此操作,

for source: https://dl.dropbox.com/u/68130108/ListViewEditItem.rar 来源: https : //dl.dropbox.com/u/68130108/ListViewEditItem.rar

public class OrderActivity extends ListActivity implements OnClickListener{
// create static object to hold your widget in listview
static TextView sTxt;
static EditText sEdTxt;
static int p;

.
.
.
.


etQty.setId(position);
etQty.setTag(tvRate);           
etQty.setOnFocusChangeListener(new OnFocusChangeListener() {

public void onFocusChange(View v, boolean hasFocus) {
    sPosition = v.getId();
    sTxt = (TextView) v.getTag();
    EditText e = ((EditText) v);
    if (!e.getText().toString().equals("")) {
        if (hasFocus) {
                // change your num arraylist values at the position
                // because you can’t save anything on ListView Item.
            num.set(sPosition,e.getText().toString());                          
        }                       
    }
}
});

editText.addTextChangedListener(new TextWatcher() {
   @Override
   public void onTextChanged(CharSequence s, int start,int before, int count) {}
   @Override
   public void beforeTextChanged(CharSequence s, int start,int count, int after) { }
   @Override
   public void afterTextChanged(Editable s) {
       // every text change setText of textview 
       sTxt.setText(s);
   }
});

tvRate.setText(num.get(position));
.
.
.
.

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想在单个TextView上显示3个EditText值 - I want to display 3 EditText values on single TextView 我无法更改ListView字体,但是edittext和textview自定义字体有效 - i cant change ListView font, but edittext and textview custom fonts are working 我想为2个EditText字段实现TextWatcher界面以在TextView上显示 - I want to implement the TextWatcher interface for 2 EditText fields to show on TextView EditText 焦点在 android studio 中不起作用,我想在 edittext 焦点改变时隐藏一个 textview,怎么办? - EditText focus is not working in android studio, i want to hide a textview when focus change in edittext, how to do this? 如何在单选按钮中放置EditText和TextView? - How can I place an EditText and TextView in a RadioButton? 如何在一行中有多个TextView和EditText - How to have multiple TextView and EditText in one row each “我在listview行中有按钮和textview”,我想在同一行中“更新textview值,同时单击按钮”? - “I have button and textview together in listview row”, I want to “update the textview value while click the button” in the same row? 我如何将 textview 放在 edittext 中 - How can i put textview inside edittext 具有EditText和TextView的Android Listview - Android Listview with EditText and TextView EditText/TextView 到 ListView - EditText/TextView to ListView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM