我制作了一个只有一组的可扩展 View ,单击一个按钮后,一个新的
添加了该组中的行,当输入时,这些行在文本框中包含一些数据
从行中,我发现当我添加添加 4 行时,我得到 7 行输入,总是这样
定量,当添加 5 行时,我得到 8 行输入,类似,不明白尝试做很多
技巧。
你能帮我看看从这些行中获取输入的最佳方法是什么吗? actlist 是接受输入的数组列表。 下面是代码 Activity
public class AddActActivity extends ExpandableListActivity implements
OnGroupClickListener,OnClickListener {
/** Called when the activity is first created. */
public TextView ShowTitle;
public TextView Date;
public static long cuesheetnimber;
public static ExpandableListView expandableListView;
public static ActAdapter expListAdapter;
static ArrayList<String> groupNames = new ArrayList<String>();
static ArrayList<ArrayList<Acts>> obsList = new ArrayList<ArrayList<Acts>>
();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addact);
ShowTitle = (TextView)findViewById(R.id.showtitleact);
ShowTitle.setText(getIntent().getStringExtra("ShowTitle"));
Date = (TextView)findViewById(R.id.showdateact);
Date.setText(DateSpinner.date);
cuesheetnimber=getIntent().getLongExtra("_ID", 0);
expandableListView =getExpandableListView();
expandableListView.setOnGroupClickListener(this);
expListAdapter = new ActAdapter( this,groupNames, obsList );
setListAdapter( expListAdapter );
expListAdapter.groups.add("a");
//AUDITDATA.HCWlist[AUDITDATA.row]=AUDITDATA.HCWNAME;
ArrayList<Acts> obs = new ArrayList<Acts>();
obs.add(new Acts());
//AUDITDATA.row++;
expListAdapter.childs.add(obs);
expandableListView.expandGroup(0);
expListAdapter.notifyDataSetChanged();
Button add=(Button)findViewById(R.id.button_add_act);
add.setOnClickListener(this);
Button SaveandExit=(Button)findViewById(R.id.SaveandExit);
SaveandExit.setOnClickListener(this);
}
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button_add_act:
Button b=(Button)v;
b.setText("Add Another Act");
expListAdapter.addHcwGroup(expandableListView);
// expListAdapter.notifyDataSetChanged();
break;
case R.id.SaveandExit:
// expListAdapter.notifyDataSetChanged();
saveandexit();
clear();
finish();
break;
default:
break;
}
}
private void clear() {
// TODO Auto-generated method stub
ActAdapter.actlist.clear();
ActAdapter.groups.clear();
ActAdapter.childs.clear();
}
@Override
public void onBackPressed() {
ActAdapter.actlist.clear();
ActAdapter.groups.clear();
ActAdapter.childs.clear();
// TODO Auto-generated method stub
super.onBackPressed();
}
private void saveandexit() {
// TODO Auto-generated method stub
for (Acts act :ActAdapter.actlist) {
Init.setActs(this, act);
}
}
}
And here is adapter code
;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.TimePicker;
public class ActAdapter extends BaseExpandableListAdapter {
public static int staticindex = 0;
private Context context;
static public ArrayList<String> groups;
static public ArrayList<ArrayList<Acts>> childs;
private LayoutInflater inflater;
public boolean flag = false;
public ActAdapter(Context context, ArrayList<String> groups,
ArrayList<ArrayList<Acts>> childs) {
this.context = context;
this.groups = groups;
this.childs = childs;
inflater = LayoutInflater.from(context);
}
public Object getChild(int groupPosition, int childPosition) {
return childs.get(groupPosition).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return (long) (groupPosition * 1024 + childPosition); // Max 1024
// children per
// group
}
public void setChilds(ArrayList<ArrayList<Acts>> childs) {
this.childs = childs;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View v = null;
long ShowID=AddActActivity.cuesheetnimber;
EditText ACTNUMBER;
EditText ACTTITLE;
TimePicker STARTTIME;
TimePicker ENDTIME;
EditText ACTIONDATA;
EditText NOTE;
if (convertView != null) {
v = convertView;
Log.d("dfg", groupPosition+"act set"+childPosition);
ACTNUMBER = (EditText) v.findViewById(R.id.ActionNumber);
ACTTITLE = (EditText) v.findViewById(R.id.ActionTitle);
STARTTIME = (TimePicker) v.findViewById(R.id.starttime);
ENDTIME = (TimePicker) v.findViewById(R.id.endttime);
ACTIONDATA = (EditText) v.findViewById(R.id.Actionname);
NOTE = (EditText) v.findViewById(R.id.Noteact);
Acts acts = new Acts();
acts.setShowID(ShowID + "");
acts.setACTIONDATA(ACTIONDATA.getText().toString());
acts.setACTNUMBER(ACTNUMBER.getText().toString());
acts.setNOTE(NOTE.getText().toString());
acts.setACTTITLE(ACTTITLE.getText().toString());
acts.setENDTIME(ENDTIME.getCurrentHour().toString() + "x"
+ ENDTIME.getCurrentMinute().toString());
acts.setSTARTTIME(STARTTIME.getCurrentHour().toString() +
"x"
+ STARTTIME.getCurrentMinute().toString());
actlist.add(acts);
} else {Log.d("dfg", "chil null");
actlist.clear();
v = inflater.inflate(R.layout.act_child_row, parent,
false);
}
return v;
}
public int getChildrenCount(int groupPosition) {
if (groupPosition > (groups.size() - 1))
return 0;
return childs.get(groupPosition).size();
}
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
public int getGroupCount() {
return groups.size();
}
public long getGroupId(int groupPosition) {
return (long) (groupPosition * 1024); // To be consistent with
// getChildId
}
static int po = 0;
public static ArrayList<Acts> actlist = new ArrayList<Acts>();
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
po++;
View v = null;
if (convertView != null) {
v = convertView;
Log.d("dfg", "grp reloaded");
} else {
v = inflater.inflate(R.layout.act_group_row, parent, false);
Log.d("dfg", "grp created");
}
;
return v;
}
public static void save()
{}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public void addHcwGroup(ExpandableListView expandableListView) {
childs.get(0).add(new Acts());
notifyDataSetChanged();
}
public void onGroupCollapsed(int groupPosition) {
}
public void onGroupExpanded(int groupPosition) {
}
}
感谢您的帮助
最佳答案
很抱歉让您久等了。我已经测试了代码,似乎与这个问题相关,没有什么是真正错误的。我的系统上发生的情况是 ExapandableListActivity
加载,然后我单击添加按钮,首先加载 2 个子行,然后加载 1 个子行。 但是您的适配器启动时已准备好加载一行。 并且您必须意识到,您的ExpandableListView
初始化折叠。您没有调用 .expandGroup();
方法,直到单击按钮,即在您将第二行添加到已有的行中。我希望这是有道理的。也许不是那么明显,因为您的 groupView
没有显示任何内容。
基本上,我的建议是在调用 setListAdapter();
之后立即调用 .expandGroup();
命令,以使事情更加明显。那么您应该清楚需要调整哪些内容才能获得您想要的结果。
我完全道歉,看来我完全误解了这个问题。我相信您希望能够自动将 editTexts 中选择的所有值保存在可扩展列表中。如果是这样,我会告诉你,你真的应该重新考虑你的设计。使用 ListView 的问题是,当一行从 View 中消失时,它就永远消失了,您需要重新制作它 - 因此 View 中的所有值都会消失。当然有 convertView
但它对此并不可靠,因为它绝对不会将最近删除的 View 放回到同一位置,无论如何都不是故意的。一旦您的子行不在视野范围内,它就会不断地对它们进行洗牌,我确信您不希望这样做。您的方法肯定会给您带来问题,因为您不断清除 getView 中的集合,而且您没有考虑当用户“完成”输入数据时如何捕获数据。
但是,如果这就是您想要的方式,您可以尝试将文本观察器应用于所有内容,并让它们不断调整集合中的相应值。 Yami
的答案中强调了该技术:How to get values from grid view 。
但是我发现这太费力了。您可能会发现制作 TableLayout
更有利,因为它将所有内容保留在内存中,您可以将所有 View 添加到布局中,然后将这些 View 添加到各自的集合中,然后您可以简单地循环遍历用于检索您的值的表格布局。这是一种替代方案,但如果不进行某种管理,您的堆大小可能会受到影响。
更好的性能是在 ExpandableListView
的每个子行上合并一个按钮,这样只有单击该按钮时,您才能将相应行的内容保存到集合中。
到目前为止,我只向您提供我能想到的解决方案,但如果您亲自问我如何解决这个问题,我不会使用 ListView
为此。我会将此组件视为一种表单,并尝试将其作为 Dialog
来代替。然后,如果我选择,我可以简单地将一整套详细信息显示为单独的列表,但只有一个 xml 膨胀用于输入详细信息。
关于java - 在列表中添加新行时 Android 可扩展 ListView 帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14032505/