Android 动态添加的ListView仅显示1项
我有一个显示客户信息的活动。所有控件都是动态绘制的,因为屏幕的设计由中央SQL数据库控制。(即屏幕位置,可编辑且必须),因此设计没有XML 到目前为止,我所做的一切都很好,直到我开始尝试添加一个ListView对象来列出客户通信数据。(即电话号码、电子邮件、传真号码或网站URL列表) 此列表视图已正确添加,并显示了第一行数据,但我似乎无法显示更多的行 这些项目将添加到表行中,然后添加到表布局中。这可能是问题所在吗?但是,当添加ListView项目时,它首先添加到RelativeLayout项目,因此这应该允许显示多行 你知道为什么会发生这种情况吗?我已经检查了数组的值,其中肯定有多个项 以下是创建方法的活动:Android 动态添加的ListView仅显示1项,android,android-listview,dynamic-data,Android,Android Listview,Dynamic Data,我有一个显示客户信息的活动。所有控件都是动态绘制的,因为屏幕的设计由中央SQL数据库控制。(即屏幕位置,可编辑且必须),因此设计没有XML 到目前为止,我所做的一切都很好,直到我开始尝试添加一个ListView对象来列出客户通信数据。(即电话号码、电子邮件、传真号码或网站URL列表) 此列表视图已正确添加,并显示了第一行数据,但我似乎无法显示更多的行 这些项目将添加到表行中,然后添加到表布局中。这可能是问题所在吗?但是,当添加ListView项目时,它首先添加到RelativeLayout项目,
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
model = SharedModel.getInstance();
dbHelper = new DatabaseHelper(this);
LinearLayout baseLayout = new LinearLayout(this);
baseLayout.setOrientation(LinearLayout.VERTICAL);
// add the customer name and number field.
// NOTE: We will always require this widget regardless of dialog design fields
tvCustomerNameNumber = new TextView(this);
tvCustomerNameNumber.setTextSize(20);
baseLayout.addView(tvCustomerNameNumber);
// buttons
RelativeLayout.LayoutParams alignBotton = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
LinearLayout buttons = new LinearLayout(this);
buttons.setOrientation(LinearLayout.HORIZONTAL);
alignBotton.addRule(RelativeLayout.ALIGN_BOTTOM, baseLayout.getId());
// button edit
Button edit = new Button(this);
edit.setId(EDIT_BUTTON_ID);
edit.setText(dbHelper.getActivityString(dbHelper.getWritableDatabase(), model.getUserLang().getLanguage().GetCodeId(), "buttonEdit", 1001000));
edit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setButtonsEnabled(false, true, true);
setControlsEnabled(true);
}
});
buttons.addView(edit);
// button save
Button save = new Button(this);
save.setId(SAVE_BUTTON_ID);
save.setText(dbHelper.getActivityString(dbHelper.getWritableDatabase(), model.getUserLang().getLanguage().GetCodeId(), "buttonSave", 1001000));
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
saveCustomer();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
buttons.addView(save);
// button cancel
Button cancel = new Button(this);
cancel.setId(CANCEL_BUTTON_ID);
cancel.setText(dbHelper.getActivityString(dbHelper.getWritableDatabase(), model.getUserLang().getLanguage().GetCodeId(), "buttonCancel", 1001000));
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
cancelChanges();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
buttons.addView(cancel);
// add the buttons
baseLayout.addView(buttons);
// build up the table of controls
TableLayout tl = new TableLayout(this);
// get the dialog design json array for customer information
dialogFields = model.getDialogDesignFields();
// reset the field type indexes
addedIndexes = new ArrayList<Integer>();
addedCodeIndexes = new ArrayList<Integer>();
addedDateIndexes = new ArrayList<Integer>();
addedTextIndexes = new ArrayList<Integer>();
addedListIndexes = new ArrayList<Integer>();
mandatoryIndexes = new ArrayList<Integer>();
// for each control in the array
for(int i=0;i<=model.getDialogDesignFields().length() - 1; i++) {
try {
// if the control is for the customer page
if(dialogFields.getJSONObject(i).get("formname").equals("CustomerInformation")){
handleField(tl, dialogFields.getJSONObject(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// Scroll view.
// NOTE: We will always need this widget to enable us to scroll the page
// if too many items are added for the display size
ScrollView sv = new ScrollView(this);
sv.addView(tl);
baseLayout.addView(sv);
// set the content view
this.setContentView(baseLayout);
// make sure we are not in edit mode
setButtonsEnabled(true, false, false);
setControlsEnabled(false);
setResult(0);
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
此.setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u Graphic);
model=SharedModel.getInstance();
dbHelper=新的DatabaseHelper(此);
LinearLayout baseLayout=新的LinearLayout(此);
基本布局。设置方向(线性布局。垂直);
//添加客户名称和编号字段。
//注意:无论对话框设计字段如何,我们始终需要此小部件
tvCustomerNameNumber=新文本视图(此);
tvCustomerNameNumber.setTextSize(20);
baseLayout.addView(tvCustomerNameNumber);
//钮扣
RelativeLayout.LayoutParams alignBotton=新的RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_父级,RelativeLayout.LayoutParams.FILL_父级);
LinearLayout按钮=新的LinearLayout(此按钮);
按钮。设置方向(线性布局。水平);
alignBotton.addRule(RelativeLayout.ALIGN_BOTTOM,baseLayout.getId());
//按钮编辑
按钮编辑=新按钮(此按钮);
edit.setId(编辑按钮ID);
edit.setText(dbHelper.getActivityString(dbHelper.getWritableDatabase(),model.getUserLang().getLanguage().GetCodeId(),“buttonEdit”,1001000));
edit.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
setButtonsEnabled(假、真、真);
setControlsEnabled(真);
}
});
按钮。添加视图(编辑);
//按钮保存
按钮保存=新按钮(此按钮);
save.setId(保存按钮ID);
save.setText(dbHelper.getActivityString(dbHelper.getWritableDatabase(),model.getUserLang().getLanguage().GetCodeId(),“buttonSave”,1001000));
save.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
试一试{
saveCustomer();
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
});
按钮。添加视图(保存);
//按钮取消
按钮取消=新按钮(此按钮);
取消设置ID(取消按钮ID);
cancel.setText(dbHelper.getActivityString(dbHelper.getWritableDatabase(),model.getUserLang().getLanguage().GetCodeId(),“buttonCancel”,1001000));
cancel.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
试一试{
取消更改();
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
});
按钮。添加视图(取消);
//添加按钮
baseLayout.addView(按钮);
//建立控制表
TableLayout tl=新的TableLayout(本);
//获取客户信息的对话框设计json数组
dialogFields=model.getDialogDesignFields();
//重置字段类型索引
AddedIndex=新的ArrayList();
AddedCodeIndex=新的ArrayList();
AddedDataIndex=新的ArrayList();
AddedTimeDexes=新的ArrayList();
AddedListIndex=新的ArrayList();
mandatoryIndexes=newArrayList();
//对于数组中的每个控件
对于(int i=0;i在做了一些研究之后,我发现将列表视图
放在另一个可滚动项目中不是一个好的做法
以下链接对此进行了讨论:
在做了一些研究之后,我发现将列表视图
放在另一个可滚动项目中不是一个好的做法
以下链接对此进行了讨论:
不要将ListView放在滚动视图中。不要将ListView放在滚动视图中。因为我是stackoverflow新手,所以我无法对答案进行投票;但我想让人们知道解决方案是有效的。很抱歉,答案重复了!因为我是stackoverflow新手,所以我无法对答案进行投票;但我想让人们知道在解决方案工作。对不起,重复的答案!
private void handleField(TableLayout tl, final JSONObject field) throws JSONException {
// get field index
int id = field.getInt("viewindex") + 1;
final String name = field.getString("name");
// if the name = CustomerAddressID don't do anything
if(name.equals("CustomerAddressID") != true) {
// if the name is not equal to CustomerCommunicationID
if(name.equals("CustomerCommunicationID") == false) {
if(id > 0) {
// create and add controls
// add the field index to the
addedIndexes.add(id);
// layout parameters
RelativeLayout.LayoutParams alignRight = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
// if the field is mandatory
if(field.getInt("mandatory") == 1) {
TextView mandatory = new TextView(this);
mandatory.setText("*");
tl.addView(mandatory);
mandatoryIndexes.add(id);
}
// field name description
TextView fn = new TextView(this);
fn.setId(id + 1000);
fn.setText(dbHelper.getActivityString(dbHelper.getWritableDatabase(), model.getUserLang().getLanguage().GetCodeId(), name, 1000030));
tl.addView(fn);
// create a table row
TableRow tr = new TableRow(this);
tr.setId(id + 10000);
tl.addView(tr);
// create a relative layout
RelativeLayout rl = new RelativeLayout(this);
rl.setId(id + 100000);
rl.setTag(field);
rl.setBackgroundResource(R.drawable.myborder);
// if the control is not disabled
if(field.getInt("disabled") == 0) {
// so we are adding a listener
// if the name is code we are adding to code indexes
if(name.endsWith("CODE") == true | name.equals("PreferredVisitDay")) {
addedCodeIndexes.add(id);
} else if (name.endsWith("DATE") == true) {
addedDateIndexes.add(id);
} else {
addedTextIndexes.add(id);
}
// add a listener
rl.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// show the code picker for customer group
JSONObject field = (JSONObject) v.getTag();
try {
prepareDialog(field);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
// image view
ImageView iv = new ImageView(this);
iv.setId(id + 1000000);
iv.setImageResource(R.drawable.edit16x16);
iv.setVisibility(View.GONE);
alignRight.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, iv.getId());
rl.addView(iv, alignRight);
}
tl.addView(rl);
// fieldValue
TextView fv = new TextView(this);
fv.setId(id + 10000000);
fv.setTextSize(18);
rl.addView(fv);
}
} else {
if(id > 0) {
addedIndexes.add(id);
// field name description
TextView fn = new TextView(this);
fn.setId(id + 1000);
fn.setText(dbHelper.getActivityString(dbHelper.getWritableDatabase(), model.getUserLang().getLanguage().GetCodeId(), name, 1000030));
tl.addView(fn);
// create a table row
TableRow tr = new TableRow(this);
tr.setId(id + 10000);
tl.addView(tr);
// create a relative layout
RelativeLayout rl = new RelativeLayout(this);
rl.setId(id + 100000);
rl.setTag(field);
rl.setBackgroundResource(R.drawable.myborder);
tl.addView(rl);
// list view
ListView lv = new ListView(this);
lv.setId(id + 10000000);
lv.setTag(field);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
try {
communicationClicked(arg0, view, position, id, field);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
rl.addView(lv);
}
}
}
}
// list view
ListView lv = new ListView(this);
lv.setId(id + 10000000);
lv.setTag(field);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
try {
communicationClicked(arg0, view, position, id, field);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
rl.addView(lv);
} else if (name.equals("CustomerCommunicationID")){
// set communications value
ListView lv = (ListView)findViewById(index + 10000000);
// create an array adapter with the customer type data array
ArrayAdapter<Communication> communicationAdapter = new ArrayAdapter<Communication>(this, R.layout.communication_menu_item, currentCustomer.getCustomerCommunications());
// assign the my customers list control the array adapter
lv.setAdapter(communicationAdapter);
Log.i(TAG, "Communications Assigned");
} else {