动态添加的ListView只显示1个项目
我有一个显示客户信息的活动。所有控件都是动态绘制的,因为屏幕的设计是由中央 SQL 数据库控制的。 (IE屏幕位置,可编辑且强制)因此没有XML用于设计。
到目前为止,我所做的一切都运行得很好,直到我开始尝试添加 ListView 对象来列出客户通信数据。 (即电话号码、电子邮件、传真号码或网站 URL 的列表)
此列表视图已正确添加,并且显示第一行数据,但我似乎无法再显示更多行。
这些项目将添加到表行中,然后再添加到表布局中。这可能是问题所在吗?但是,当添加 ListView 项目时,它首先添加到relativelayout 项目中,因此这应该允许显示多行?!
任何想法为什么会发生这种情况?我已经检查了数组的值,其中肯定有不止一项。
以下是 Activity onCreate 方法:
@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);
}
现在,handleField 方法决定如何处理要添加的字段:
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);
}
}
}
}
使用以下代码将 ListView 项添加到第二个代码块中:
// 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 {
我已经做了一个测试,并在应用程序的顶部添加了一个列表视图,并将列表适配器设置为我的数组,并且所有行都显示出来。
所以看起来它与表行有关
I have an activity that shows customer information. All the controls are drawn dynamically as the design for the screen is controlled by a central SQL database. (IE screen position, editable and mandatory) so there is no XML for the design.
Now so far everything I have done has worked very well until I came to try adding a ListView object to list the customers communication data. (IE a list of phone numbers, emails, fax numbers or website URLs)
This List view is added correctly and the first row of data is displayed but I cannot seem to get the anymore rows to display.
The items are added into a table row and these are then added onto a table layout. Could this be the problem? However when adding the ListView item this is first added to a RelativeLayout item so this should allow more than one line to be displayed?!
Any Ideas why this could be occurring? I have checked the values of my array and I definitely have more than one item in it.
The following is the Activities onCreate method:
@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);
}
And now the handleField method that decides what to do with the field im adding:
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);
}
}
}
}
The ListView Item is added in the second block of code with the following code:
// 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);
Finally the List view is populated with the following code:
} 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 {
I have done a test and added a Listview at the top of my application and set the listadapter to my array and all the lines are displayed.
So looks like Its to do with the Table rows
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过一些研究后,我发现将
ListView
放置在另一个可滚动项目中并不是一个好的做法。以下链接对此进行了讨论:
Google I/O 2010 - ListView 的世界
After doing some research I have found that its not good practice to place a
ListView
inside a another scrollable item.The following link discusses this:
Google I/O 2010 - The world of ListView
不要将 ListView 放在 ScrollView 中。
Do not put ListView in a ScrollView.