2016-09-15 45 views
1

我試圖將編輯文本發送到可展開列表視圖。首先編輯文本。由於聲譽目的,我無法發佈我的屏幕截圖應該是父母,第二個應該在點擊保存按鈕後作爲其子項存儲在可擴展列表視圖中。將文本編輯爲父級及其文本視圖作爲子級展開列表查看

這是我的main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:orientation="vertical" 
tools:context=".MainActivity" 
android:weightSum="1"> 

<LinearLayout 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <EditText 
     android:layout_width="wrap_content" 
     android:id="@+id/heading" 
     android:layout_height="wrap_content" 
     android:hint="Heading for your Field"/> 
    <EditText 
     android:id="@+id/textin" 
     android:layout_width="237dp" 
     android:background="@drawable/rounded_edittext" 
     android:layout_height="wrap_content" 
     android:hint="Your Options"/> 

</LinearLayout> 
    <Button 
    android:id="@+id/add" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:text="Add" 
    android:layout_gravity="right" /> 
    <Button 
    android:id="@+id/btn_save" 
    android:layout_width="320dp" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:text="Save" 
    android:onClick="save_button" 
    android:layout_gravity="right" /> 
    </LinearLayout> 

通過點擊添加按鈕,我可以從ID的EditTex添加文本textin 動態。這些很容易發生。這是我的主要活動

public class MainActivity extends AppCompatActivity { 

EditText textIn,txtHeading; 
Button buttonAdd,btnsave; 
TextView textViewOut; 
LinearLayout container; 

ExpandableDataPump expandableDataPump; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    textViewOut = (TextView)findViewById(R.id.textout); 
    textIn = (EditText)findViewById(R.id.textin); 
    txtHeading = (EditText)findViewById(R.id.heading); 
    buttonAdd = (Button)findViewById(R.id.add); 
    container = (LinearLayout)findViewById(R.id.container); 


    buttonAdd.setOnClickListener(new View.OnClickListener(){ 

     @Override 
     public void onClick(View arg0) { 


      LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      final View addView = layoutInflater.inflate(R.layout.list_view, null); 

      TextView textOut = (TextView)addView.findViewById(R.id.textout); 

      textOut.setText(textIn.getText().toString()); 


      Button buttonRemove = (Button)addView.findViewById(R.id.remove); 
      buttonRemove.setOnClickListener(new View.OnClickListener(){ 

       @Override 
       public void onClick(View v) { 
        ((LinearLayout)addView.getParent()).removeView(addView); 
       }}); 

      container.addView(addView); 
     }}); 

    btnsave =(Button) findViewById(R.id.btn_save); 
    btnsave.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      try { 
       expandableDataPump.getData(); 
      } 
      catch (Exception ex) { 

    Toast.makeText(MainActivity.this,"You have an 
    ERROR",Toast.LENGTH_LONG).show();    } 



     } 
    }); 

} 
public class ExpandableDataPump { 
    public HashMap<String, List<String>> getData() { 
     HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>(); 


     List<String> childs = new ArrayList<String>(); 
     childs.add(textIn.getText().toString()); 
     expandableListDetail.put(txtHeading.getText().toString(), childs); 

     return expandableListDetail; 
    } 
} 
} 

這是我的一個包含可擴展列表視圖,在它的XML擴展列表類:

public class ExpandableList extends AppCompatActivity { 

ExpandableListView expandableListView; 
ExpandableListAdapter expandableListAdapter; 
List<String> expandableListTitle; 
HashMap<String, List<String>> expandableListDetail; 
MainActivity.ExpandableDataPump expandableDataPump; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.expandable_list); 


    expandableListView = (ExpandableListView) findViewById(R.id.expandableListView); 

    expandableListDetail = expandableDataPump.getData(); 
    expandableListTitle = new ArrayList<String>(expandableListDetail.keySet()); 
    expandableListAdapter = new CustomExpandableListAdapter(this, expandableListTitle, expandableListDetail); 
    expandableListView.setAdapter(expandableListAdapter); 
    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { 

     @Override 
     public void onGroupExpand(int groupPosition) { 
      Toast.makeText(getApplicationContext(), 
        expandableListTitle.get(groupPosition) + " List Expanded.", 
        Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { 

     @Override 
     public void onGroupCollapse(int groupPosition) { 
      Toast.makeText(getApplicationContext(), 
        expandableListTitle.get(groupPosition) + " List Collapsed.", 
        Toast.LENGTH_SHORT).show(); 

     } 
    }); 

    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
     @Override 
     public boolean onChildClick(ExpandableListView parent, View v, 
            int groupPosition, int childPosition, long id) { 
      Toast.makeText(
        getApplicationContext(), 
        expandableListTitle.get(groupPosition) 
          + " -> " 
          + expandableListDetail.get(
          expandableListTitle.get(groupPosition)).get(
          childPosition), Toast.LENGTH_SHORT 
      ).show(); 
      return false; 
     } 
    }); 
}} 

這是我的適配器類別:

public class CustomExpandableListAdapter extends 
BaseExpandableListAdapter { 

private Context context; 
private List<String> expandableListTitile; 
private HashMap<String,List<String>> expandableListDetail; 


public CustomExpandableListAdapter(Context context,List<String> 
expandableLIstTitle, 
           HashMap<String,List<String>>  
expandableListDetail){ 
this.context = context; 
this.expandableListTitile = expandableLIstTitle; 
this.expandableListDetail = expandableListDetail; 
} 
@Override 
public Object getChild(int listPosition, int expandedListPosition) { 
    return 
    this.expandableListDetail. 
    get(this.expandableListTitile.get(listPosition)) 
      .get(expandedListPosition); 
} 

@Override 
public long getChildId(int listPosition, int expandedListPosition) { 
    return expandedListPosition; 
} 

@Override 
public View getChildView(int listPosition, final int 
expandedListPosition, 
         boolean isLastChild, View convertView, 
ViewGroup parent) { 
    final String expandedListText = (String) getChild(listPosition, 
    expandedListPosition); 
    if (convertView == null) { 
     LayoutInflater layoutInflater = (LayoutInflater) this.context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = layoutInflater.inflate(R.layout.list_item, 
    null); 
    } 
    TextView expandedListTextView = (TextView) convertView 
      .findViewById(R.id.expandedListItem); 
    expandedListTextView.setText(expandedListText); 
    return convertView; 
} 

@Override 
public int getChildrenCount(int listPosition) {  return 
this.expandableListDetail. 
    get(this.expandableListTitile.get(listPosition)) 
      .size(); 
} 

@Override 
public Object getGroup(int listPosition) { 
    return this.expandableListTitile.get(listPosition); 
} 

@Override 
public int getGroupCount() { 
    return this.expandableListTitile.size(); 
} 

@Override 
public long getGroupId(int listPosition) { 
    return listPosition; 
} 

@Override 
public View getGroupView(int listPosition, boolean isExpanded, 
         View convertView, ViewGroup parent) { 
    String listTitle = (String) getGroup(listPosition); 
    if (convertView == null) { 
     LayoutInflater layoutInflater = (LayoutInflater) this.context. 
       getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = layoutInflater.inflate(R.layout.list_group, 
null); 
    } 
    TextView listTitleTextView = (TextView) convertView 
      .findViewById(R.id.listTitle); 
    listTitleTextView.setTypeface(null, Typeface.BOLD); 
    listTitleTextView.setText(listTitle); 
    return convertView; 
} 

@Override 
public boolean hasStableIds() { 
    return false; 
} 

@Override 
public boolean isChildSelectable(int listPosition, int  
expandedListPosition) { 
    return true; 
} 
} 

我試圖將編輯文本值發送到可擴展列表視圖。我正確嗎?請幫忙。

回答

1

變化根據這個你的代碼。我爲你做了這個。

public class CustomExpandableListAdapter extends BaseExpandableListAdapter { 

private Context context; 
private String expandableListTitile; 
private ArrayList<String> expandableListDetail; 


public CustomExpandableListAdapter(Context context, String expandableLIstTitle, 
            ArrayList<String> expandableListDetail){ 
    this.context = context; 
    this.expandableListTitile = expandableLIstTitle; 
    this.expandableListDetail = expandableListDetail; 
} 
    @Override 
    public Object getChild(int listPosition, int expandedListPosition) { 
     return this.expandableListDetail 
       .get(expandedListPosition); 
    } 

    @Override 
    public long getChildId(int listPosition, int expandedListPosition) { 
     return expandedListPosition; 
    } 

    @Override 
    public View getChildView(int listPosition, final int expandedListPosition, 
          boolean isLastChild, View convertView, ViewGroup parent) { 
     final String expandedListText = (String) getChild(listPosition, expandedListPosition); 
     if (convertView == null) { 
      LayoutInflater layoutInflater = (LayoutInflater) this.context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = layoutInflater.inflate(R.layout.list_item, null); 
     } 
     TextView expandedListTextView = (TextView) convertView 
       .findViewById(R.id.expandedListItem); 
     expandedListTextView.setText(expandedListText); 
     return convertView; 
    } 

    @Override 
    public int getChildrenCount(int listPosition) { 
     return this.expandableListDetail 
       .size(); 
    } 

    @Override 
    public Object getGroup(int listPosition) { 
     return this.expandableListTitile; 
    } 

    @Override 
    public int getGroupCount() { 
     return 1; 
    } 

    @Override 
    public long getGroupId(int listPosition) { 
     return listPosition; 
    } 

    @Override 
    public View getGroupView(int listPosition, boolean isExpanded, 
          View convertView, ViewGroup parent) { 
     String listTitle = (String) getGroup(listPosition); 
     if (convertView == null) { 
      LayoutInflater layoutInflater = (LayoutInflater) this.context. 
        getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = layoutInflater.inflate(R.layout.list_group, null); 
     } 
     TextView listTitleTextView = (TextView) convertView 
       .findViewById(R.id.listTitle); 
     listTitleTextView.setTypeface(null, Typeface.BOLD); 
     listTitleTextView.setText(listTitle); 
     return convertView; 
    } 

    @Override 
    public boolean hasStableIds() { 
     return false; 
    } 

    @Override 
    public boolean isChildSelectable(int listPosition, int expandedListPosition) { 
     return true; 
    } 
} 

這是MainActivity。

public class MainActivity extends AppCompatActivity { 


EditText textIn, txtHeading; 
Button buttonAdd, btnsave; 
TextView textViewOut; 
LinearLayout container; 
protected static final String Shared = null; 
ExpandableDataPump expandableDataPump; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    expandableDataPump = new ExpandableDataPump(); 


    textViewOut = (TextView) findViewById(R.id.textout); 
    textIn = (EditText) findViewById(R.id.textin); 
    txtHeading = (EditText) findViewById(R.id.heading); 
    buttonAdd = (Button) findViewById(R.id.add); 
    container = (LinearLayout) findViewById(R.id.container); 


    buttonAdd.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 


      LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      final View addView = layoutInflater.inflate(R.layout.list_view, null); 

      TextView textOut = (TextView) addView.findViewById(R.id.textout); 

      textOut.setText(textIn.getText().toString()); 


      Button buttonRemove = (Button) addView.findViewById(R.id.remove); 
      buttonRemove.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        ((LinearLayout) addView.getParent()).removeView(addView); 
       } 
      }); 

      container.addView(addView); 
     } 
    }); 

    btnsave = (Button) findViewById(R.id.btn_save); 
    btnsave.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      try { 
       Intent intent = new Intent(getApplicationContext(), ExpandableList.class); 
       String Heading = txtHeading.getText().toString(); 
       intent.putExtra("Heading", Heading); 

       intent.putStringArrayListExtra("Options", (ArrayList<String>) expandableDataPump.getData()); 
       startActivity(intent); 

      } catch (Exception ex) { 
       ex.printStackTrace(); 

       Toast.makeText(MainActivity.this, "You have an ERROR", Toast.LENGTH_LONG).show(); 
      } 


     } 
    }); 

} 

public class ExpandableDataPump { 
    public List<String> getData() { 
     List<String> childs = new ArrayList<>(); 
     for (int i = 2; i < container.getChildCount(); i++) { 
      if (container.getChildAt(i) instanceof RelativeLayout) { 
       childs.add(((TextView) container.getChildAt(i).findViewById(R.id.textout)).getText().toString()); 

      } 
     } 
     return childs; 

    } 
} 
} 

這是您的ExpandableListActivity

public class ExpandableList extends AppCompatActivity { 
private static final String Shared = null; 

ExpandableListView expandableListView; 
ExpandableListAdapter expandableListAdapter; 
String expandableListTitle; 
ArrayList<String> expandableListDetail; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.expandable_list); 


    expandableListDetail = getIntent().getStringArrayListExtra("Options"); 

    expandableListTitle = getIntent().getStringExtra("Heading"); 


    expandableListAdapter = new CustomExpandableListAdapter(this, expandableListTitle, expandableListDetail); 


    expandableListView.setAdapter(expandableListAdapter); 
    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { 

     @Override 
     public void onGroupExpand(int groupPosition) { 

      Toast.makeText(getApplicationContext(), 
        expandableListTitle + " List Expanded.", 
        Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { 

     @Override 
     public void onGroupCollapse(int groupPosition) { 
      Toast.makeText(getApplicationContext(), 
        expandableListTitle + " List Collapsed.", 
        Toast.LENGTH_SHORT).show(); 

     } 
    }); 

    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
     @Override 
     public boolean onChildClick(ExpandableListView parent, View v, 
            int groupPosition, int childPosition, long id) { 
      Toast.makeText(
        getApplicationContext(), 
        expandableListTitle 
          + " -> " 
          + expandableListDetail/*.get(
          expandableListTitle.get(groupPosition)).get(
          childPosition)*/, Toast.LENGTH_SHORT 
      ).show(); 
      return false; 
     } 
    }); 
}} 

如果這不利於隨意評論。

+0

這是輝煌..它的作品 –

0

我做了一些快速googleling和這個職位是我覺得你有點眼熟,從什麼樣子你會在正確的方向:

Send data from edittext to listview

希望這有助於

+1

這不是關於列表視圖。我希望將其發送到ExpandableList視圖。 :) –