2016-09-15 6 views
1

編集テキストを展開可能リストビューに送信しようとしています。最初の編集Text.Sorry私は私のスクリーンショットを投稿することができませんでした評判の目的は親でなければならず、2番目は、保存ボタンをクリックした後、拡張可能なリストビューで子として格納する必要があります。テキストを親として編集し、そのテキストを展開可能な子として表示listView

これは私が動的に textin IDのEditTexからテキストを追加することができ、[追加]ボタンをクリックすると、私の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> 

です。これらは簡単に起こります。この本は、それのXMLで拡張可能なリストビューが含まれている私の拡張可能なリストクラスである私のメインの活動

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

です:

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 Viewに送りたいと思っていました。 :) –

関連する問題