2

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

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

我的尝试是这样的

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;
            }
        }
    }

}

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

4

2 回答 2

1

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

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

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

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

更新4:我删除了以前的代码,因为它的性能很差并且创建了很多对象,您可以查看以下完整示例:

活动代码:

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;
    }

}
于 2013-05-25T15:53:13.797 回答
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;
}
于 2013-05-25T13:30:51.403 回答