我試圖將編輯文本發送到可展開列表視圖。首先編輯文本。由於聲譽目的,我無法發佈我的屏幕截圖應該是父母,第二個應該在點擊保存按鈕後作爲其子項存儲在可擴展列表視圖中。將文本編輯爲父級及其文本視圖作爲子級展開列表查看
這是我的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;
}
}
我試圖將編輯文本值發送到可擴展列表視圖。我正確嗎?請幫忙。
這是輝煌..它的作品 –