gpt4 book ai didi

android - 将文本作为父级编辑并将其 TextView 作为子级编辑为可扩展 ListView

转载 作者:行者123 更新时间:2023-11-30 01:00:28 25 4
gpt4 key购买 nike

我正在尝试将编辑文本发送到可扩展 ListView 中。首先编辑文本。抱歉,我无法发布我的屏幕截图,因为声誉目的应该是父项,第二个应在单击保存按钮后作为其子项存储在可扩展 ListView 中。

这是我的 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 中的文本。这些这很容易发生。这是我的主要 Activity

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 中包含可扩展 ListView :

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

我正在尝试将编辑文本值发送到可扩展 ListView 。我走对了吗?请帮忙。

最佳答案

根据此更改您的代码。我已经为你做了这件事。

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

如果这没有帮助,请随时发表评论。

关于android - 将文本作为父级编辑并将其 TextView 作为子级编辑为可扩展 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39504254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com