无法在android中的ListView的EditText中填充详细信息

问题描述 投票:0回答:2

我用自定义列表视图创建了一个应用程序。我的listview包含一个edittext字段。但是我无法用value填充编辑文本。我是android的新手,所以很难找到错误给我。

这是我的代码:

      public class MainActivity extends Activity {
         private ListView myList;
         private MyAdapter myAdapter;


        /** Called when the activity is first created. */
        @Override
           public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myList = (ListView) findViewById(R.id.MyList);
        myList.setItemsCanFocus(true);
        myAdapter = new MyAdapter();
        myList.setAdapter(myAdapter);

            }

          public class MyAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public ArrayList myItems = new ArrayList();
        public MyAdapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            for (int i = 0; i < 20; i++) {
                ListItem 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 View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.item, 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(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;
                        myItems.get(position).caption = Caption.getText().toString();
                    }
                }
              });

                return convertView;
            }
          }

         class ViewHolder {
             EditText caption;
          }

           class ListItem {
              String caption;
            }
        }

item.xml

     <?xml version="1.0" encoding="utf-8"?>
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">

         <EditText android:id="@+id/ItemCaption"
           android:layout_height="40dp" 
           android:layout_width="fill_parent"
           android:layout_marginLeft="2dip"
           android:singleLine="true">
         </EditText>

          </LinearLayout>

activity_main.xml中

android listview android-edittext
2个回答
3
投票

试试这种方式

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/ItemCaption"
        android:layout_height="40dp"
        android:layout_width="match_parent"
        android:layout_marginLeft="2dp"
        android:singleLine="true">
    </EditText>

</LinearLayout>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/MyList"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:descendantFocusability="afterDescendants">
    </ListView>

</LinearLayout>

my activity.Java

public class MyActivity extends Activity {

    private ListView myList;
    private MyAdapter myAdapter;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myList = (ListView) findViewById(R.id.MyList);
        myList.setItemsCanFocus(true);
        myAdapter = new MyAdapter(this);
        myList.setAdapter(myAdapter);

    }

    public class MyAdapter extends BaseAdapter {
        private ArrayList<ListItem> myItems;
        private Context context;
        public MyAdapter(Context context) {
            this.context = context;
            myItems = new ArrayList<ListItem>();
            for (int i = 0; i < 20; i++) {
                ListItem listItem = new ListItem();
                listItem.caption = "Caption" + i;
                myItems.add(listItem);
            }
        }

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

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

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

        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null, false);
                holder.caption = (EditText) convertView
                        .findViewById(R.id.ItemCaption);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.caption.setText(myItems.get(position).caption);
            holder.caption.setTag(position);
            holder.caption.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                public void onFocusChange(View v, boolean hasFocus) {
                    if (!hasFocus){
                        final int position = (Integer)v.getTag();
                        final EditText Caption = (EditText) v;
                        myItems.get(position).caption = Caption.getText().toString();
                    }
                }
            });
            return convertView;
        }
    }

    class ViewHolder {
        EditText caption;
    }

    class ListItem {
        String caption;
    }
}

0
投票

我使用了这段代码,但是如果listview有滚动,那么如果先前修改了它们,它会丢失这些值。为了解决这个问题,我补充说:

holder.txtValue.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            myItems.get(intPosition).value = s.toString();
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

这样,编辑框中所做的所有更改都会正确保存。

请享用!

© www.soinside.com 2019 - 2024. All rights reserved.