单击按钮问题后,在EditText ListView中获取文本内容

时间:2013-05-25 13:18:33

标签: android listview

我有一个EditText的Listview,当我点击确认按钮时,我需要获取每个编辑行的String值,但我不知道如何。

我试图调整一些样本但没有成功(我总是得到默认值,而不是编辑后的值)。

我的尝试就是这个

public class MyActivity extends Activity {
    static int nItems;

    ImageButton confirmButton;
    ListView myList;

    ListViewAdapterEditText adapterG1, adapterG2, adapterG3;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        .....
        myList = (ListView) findViewById(R.id.listaG1);
        myList.setItemsCanFocus(true);
        adapterG1 = new ListViewAdapterEditText();
        myList.setAdapter(adapterG1);

    }

    OnClickListener mConfirmButtonListener = new OnClickListener() {
        public void onClick(View v) {
        ArrayList a1 = adapterG1.getItems();
                for (int i = 0; i < nItems; i++) {
                    System.out.println(a1.get(i)
                            + "\n\n");
        }
    };

    public class ListViewAdapterEditText extends BaseAdapter {
        private LayoutInflater mInflater;
        public ArrayList myItems = new ArrayList();
        ListItem listItem;

        public ListViewAdapterEditText() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            for (int i = 0; i < nItems; i++) {
                listItem = new ListItem();
                listItem.caption = "Caption" + i;
                myItems.add(listItem);
            }
            notifyDataSetChanged();
        }

        public int getCount() {
            return myItems.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public ArrayList<String> getItems() {
            ArrayList<String> items = new ArrayList<String>();
            for (int i = 0; i < nItems; i++) {
                ListItem li = (ListItem) myItems.get(i);
                items.add(li.getCaption());
            }
            return items;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.list_item_row,
                        null);
                holder.caption = (EditText) convertView
                        .findViewById(R.id.ItemCaption);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            // Fill EditText with the value you have in data source
            holder.caption.setText(((ListItem) myItems.get(position)).caption);
            // holder.caption.setText(myItems.get(position).caption);
            holder.caption.setId(position);

            // we need to update adapter once we finish with editing
            holder.caption
                    .setOnFocusChangeListener(new OnFocusChangeListener() {
                        public void onFocusChange(View v, boolean hasFocus) {
                            if (!hasFocus) {
                                final int position = v.getId();
                                final EditText Caption = (EditText) v;
                                ((ListItem) myItems.get(position)).caption = Caption
                                        .getText().toString();
                            }
                        }
                    });

            return convertView;
        }

        class ViewHolder {
            EditText caption;
        }

        class ListItem {
            String caption;

            public String getCaption() {
                return caption;
            }
        }
    }

}

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

您确定调用了OnFocusChangeListener吗?如果您在EditText中编辑文本然后点击确认按钮,则不会在触摸模式下调用此侦听器,因为焦点仍在EditText上。

更新:考虑你在EditText中编辑文本的情况,虽然没有确认并滚动ListView以便项目视图被回收,我不确定你的首选方式是什么,但如果你想存储编辑的数据,您可以使用setRecyclerListener(android.widget.AbsListView.RecyclerListener)在项目视图被回收时收到通知,以便您可以保存编辑结果。要保存屏幕上显示的EditText结果,可以使用getChildAt等方法在屏幕上显示项目视图,然后获取EditText的文本。

Update2:另一种更好,更干净的方法是使用TextWatcher和addTextChangeListener,这将在EditText中的文本发生更改时通知您。

Update3:我只是编写以下示例并测试它,它可以在我的手机上运行。 :)

Update4:我删除了之前的代码导致其性能不佳并创建了很多对象,您可以检查以下完整示例:

活动代码:

package com.example.asynctasktest;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

/**
 * @author Daniel Chow
 *
 * May 26, 2013 12:57:49 AM
 */
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView = (ListView) findViewById(R.id.listview);
        final TestAdapter adapter = new TestAdapter(this);
        listView.setAdapter(adapter);
        Button confirmButton = (Button) findViewById(R.id.confirm_button);
        confirmButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                List<String> items = adapter.getItems();
                for (int i = 0, n = items.size(); i < n; i++) {
                    Log.e("", items.get(i));
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


}

适配器代码:

/**
 * 
 */
package com.example.asynctasktest;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;

/**
 * @author Daniel Chow
 * 
 *         May 26, 2013 1:13:02 AM */
public class TestAdapter extends BaseAdapter {

    private List<String> items = new ArrayList<String>();
    private Context context;

    public TestAdapter(Context context) {
        this.context = context;
        for (int i = 0; i < 12; i++) {
            items.add("caption " + i);
        }
    }

    public List<String> getItems() {
        return new ArrayList<String>(items);
    }

    @Override
    public int getCount() {
        return 12;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = new EditText(context);
            holder = new ViewHolder();
            holder.editText = (EditText) convertView;
            holder.watcher = new EditTextWatcher();
            holder.editText.addTextChangedListener(holder.watcher);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.watcher.setTarget(position);
        holder.editText.setText(items.get(position));
        return convertView;
    }

    private class EditTextWatcher implements TextWatcher {

        private int target;

        public void setTarget(int target) {
            this.target = target;
        }

        @Override
        public void afterTextChanged(Editable s) {
            items.set(target, s.toString());
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

    }

    private static class ViewHolder {
        EditText editText;
        EditTextWatcher watcher;
    }

}

答案 1 :(得分:0)

我通常遵循更简单的技术

public class Item_Adapter extends BaseAdapter {
      private String[] Val;

 public Item_Adapter () {
    Val= new String[nItems];

 }
 public View getView(int position, View convertView, ViewGroup parent) {
           ViewHolder holder;
 ///bla bla

holder.caption
                .setOnFocusChangeListener(new OnFocusChangeListener() {
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (!hasFocus) {
                            final int position = v.getId();
                            final EditText Caption = (EditText) v;
                            ((ListItem) myItems.get(position)).caption = Caption
                                    .getText().toString();
                             Val[position]= Caption
                                    .getText().toString();

                        }
                    }
                });
      ///bla bla
    return convertView;
}
     /////most important returning your array so you can use it in the Activity
    public String[] getVal() {
    return Val;
}