我有一项显示客户信息的活动。所有控件都是动态绘制的,因为屏幕设计由中央SQL数据库控制。 (IE屏幕位置,可编辑和强制)因此设计没有XML。
到目前为止,我所做的一切工作都非常顺利,直到我尝试添加ListView对象来列出客户的通信数据。 (IE是电话号码,电子邮件,传真号码或网站URL的列表)
正确添加此列表视图并显示第一行数据,但我似乎无法再显示任何行。
将项目添加到表格行中,然后将这些项目添加到表格布局中。这可能是问题吗?但是,当添加ListView项时,首先将它添加到RelativeLayout项,这样可以允许显示多行?!
任何想法为什么会发生这种情况?我检查了我的数组的值,我肯定有多个项目。
以下是活动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);
最后,使用以下代码填充List视图:
} 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 {
我做了一个测试并在我的应用程序顶部添加了一个Listview,并将listadapter设置为我的数组,并显示所有行。
所以看起来它与表行
有关答案 0 :(得分:5)
答案 1 :(得分:1)
不要将ListView放在ScrollView中。