代码之家  ›  专栏  ›  技术社区  ›  Zubair Younas

从可扩展列表视图中的子级获取值

  •  -3
  • Zubair Younas  · 技术社区  · 8 年前

    我有一个可扩展列表视图(EXLV),它的每个子项都有一个EditText,EXLV外有一个按钮。已经一个星期了,我在胡闹。 我想通过点击按钮在编辑文本中输入值/数据。 因为我输入的值是重新生成的(我不知道为什么)。 任何帮助都将不胜感激。 以下是整个项目的链接,以防: https://drive.google.com/file/d/1YsCb23-wL_KBgj2braqT59mPezRhSS28/view?usp=sharing

    主要活动。Java语言 :

    package com.lvexpandable;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.util.logging.Handler;
    
    import android.Manifest;
    import android.app.Activity;
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.content.pm.PackageManager;
    import android.os.Bundle;
    import android.os.CountDownTimer;
    import android.support.v4.app.ActivityCompat;
    import android.support.v7.app.AlertDialog;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ExpandableListView;
    import android.widget.ExpandableListView.OnChildClickListener;
    import android.widget.ExpandableListView.OnGroupClickListener;
    import android.widget.ExpandableListView.OnGroupCollapseListener;
    import android.widget.ExpandableListView.OnGroupExpandListener;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
        final int snnn=1857;
    
        Button btn;
        EditText edt;
        ExpandableListAdapter listAdapter;
        ExpandableListView expListView;
        List<String> listDataHeader;
        HashMap<String, List<String>> listDataChild;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            String[] mStrings = new String[20];
    
            // get the listview
            expListView = (ExpandableListView) findViewById(R.id.lvExp);
    
            // preparing list data
            prepareListData();
    
            listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
    
            // setting list adapter
            expListView.setAdapter(listAdapter);
    
    
        }
        public void btnClick(View v){
            //I want to get values entered in EditTexts here. Maybe using SharedPref, in a List<> or whatever.
    
        }
    
        /*
         * Preparing the list data
         */
        private void prepareListData() {
            listDataHeader = new ArrayList<>();
            listDataChild = new HashMap<>();
    
            // Adding child data
            listDataHeader.add("PG");
            listDataHeader.add("Nursery");
    
    
    
            // Adding child data
            List<String> pg = new ArrayList<String>();
            pg.add("PG1: ");
            pg.add("PG2: ");
            pg.add("PG3: ");
            pg.add("PG4: ");
            pg.add("PG5: ");
            pg.add("PG6: ");
            pg.add("PG7: ");
            pg.add("PG8: ");
            pg.add("PG9: ");
            pg.add("PG10: ");
            pg.add("PG11: ");
            pg.add("PG12: ");
            pg.add("PG13: ");
            pg.add("PG14: ");
            pg.add("PG15: ");
    
    
            List<String> nur = new ArrayList<String>();
            nur.add("Object A ");
            nur.add("Obj B: ");
            nur.add("Obj C:");
    
    
    
            listDataChild.put(listDataHeader.get(0), pg); // Header, Child data
            listDataChild.put(listDataHeader.get(1), nur);
    
        }
    }
    

    可扩展列表适配器。Java语言 :

    package com.lvexpandable;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Random;
    
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.graphics.Typeface;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseExpandableListAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class ExpandableListAdapter extends BaseExpandableListAdapter {
    
    
        private Context _context;
        private List<String> _listDataHeader; // header titles
        // child data in format of header title, child title
        private HashMap<String, List<String>> _listDataChild;
    
    
    
    
    
    
        public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                     HashMap<String, List<String>> listChildData) {
            this._context = context;
            this._listDataHeader = listDataHeader;
            this._listDataChild = listChildData;
    
        }
    
    
        @Override
        public Object getChild(int groupPosition, int childPosititon) {
            return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                    .get(childPosititon);
        }
    
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }
    
    
    
        @Override
        public View getChildView(int groupPosition, final int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
    
    
    
            final String childText = (String) getChild(groupPosition, childPosition);
    
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.list_item, null);
            }
            TextView txtListChild = convertView.findViewById(R.id.lblListItem);
    
    
    
            txtListChild.setText(childText);
            return convertView;
        }
    
    
    
    
    
        @Override
        public int getChildrenCount(int groupPosition) {
            return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                    .size();
        }
    
        @Override
        public Object getGroup(int groupPosition) {
            return this._listDataHeader.get(groupPosition);
        }
    
        @Override
        public int getGroupCount() {
            return this._listDataHeader.size();
        }
    
        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }
    
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            String headerTitle = (String) getGroup(groupPosition);
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.list_group, null);
            }
    
            TextView lblListHeader = (TextView) convertView
                    .findViewById(R.id.lblListHeader);
            lblListHeader.setTypeface(null, Typeface.BOLD);
            lblListHeader.setText(headerTitle);
    
            return convertView;
        }
    
        @Override
        public boolean hasStableIds() {
            return false;
        }
    
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }
    

    活动_main。xml :

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.lvexpandable.MainActivity">
    
        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="50dp"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <ExpandableListView
                android:id="@+id/lvExp"
                android:layout_width="match_parent"
                android:layout_height="400dp" />
    
            <Button
                android:onClick="btnClick"
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="230dp"
                android:text="Button"
                tools:layout_editor_absoluteX="257dp"
                tools:layout_editor_absoluteY="360dp" />
        </LinearLayout>
    
    
    
    </android.support.constraint.ConstraintLayout>
    

    如果有人感到困惑,请随时提问:)

    1 回复  |  直到 8 年前
        1
  •  0
  •   Zubair Younas    8 年前

    你想做的事情当然是可能的,但很复杂:

    1. 在每个子项中添加一个按钮
    2. 当用户键入子0时,他必须按下该子对象的按钮(单击按钮时,将值保存在索引[childPosition]的数组中)。在里面 这样,数组[0]将具有子1的文本,与下一个类似 儿童

    ( 步骤3和其他步骤是可选的 )

    1. 如果您不喜欢这种设计(因为用户需要在每次输入值时按下按钮,请参阅步骤4)
    2. 将按钮(在步骤1中添加)的可见性设置为 不可见
    3. 为ChildItem中的EditText添加TextWatcher(TextChangedListener)(当用户离开当前EditText时,它将获取输入的文本并将其保存在数组[]中并单击按钮本身)。作为:

      edt.addTextChangedListener(new TextWatcher() {
          @Override
          public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
      
          @Override
          public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
      @Override
      public void afterTextChanged(Editable editable) {
      
      if(edt.getText().toString()==""&&edt.getText().toString().equals(null)){
      }
      else {
          edt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
              @Override
              public void onFocusChange(View v, boolean hasFocus) {
                  if (!hasFocus)//Perform Click when edt loses Focus {
                      btn.callOnClick();
                  }
              }
          });
      }
      }});
      

      现在,单击按钮将类似于:

      btn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
              if(edt.getText().toString().equals("")){
      
              }
              else {
              Toast.makeText(view.getContext(),"Entered: "+edt.getText().toString(),Toast.LENGTH_SHORT).show();
              myStringArray[childPosition]=edt.getText().toString();
              edt.setText("");
              }
          }
      });