我使用ListView
填充BaseAdapter
。我的数组有两个值,我得到size = 2仍然列表视图只显示一个项目。以下代码有什么问题:
public class ActivityContactDetailScreen extends Activity{
String contact_id = "",nickName="",fname="",lname="",
ArrayList<String> phone_number,email_id;
private ContactDataSource datasource;
long _id;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_detail_screen);
// open database connection
datasource = new ContactDataSource(ActivityContactDetailScreen.this);
datasource.open();
// Get all required details from previous activity
Intent intent = getIntent();
contact_id = intent.getStringExtra("contact_id");
_id = intent.getLongExtra("_id", -1);
Log.e("contact_id ",contact_id);
String contactSql = "SELECT * FROM "
+ MySQLiteHelper.TABLE_NAME_CONTACT
+" WHERE "
+ MySQLiteHelper.COLUMN_CNT_CONTACT_ID
+ " = "
+ contact_id
+ " LIMIT 1";
String phoneSql = "SELECT * FROM "
+ MySQLiteHelper.TABLE_NAME_PHONE
+" WHERE "
+ MySQLiteHelper.COLUMN_PHN_CONTACT_ID
+ " = "
+ contact_id;
//Log.e("SQL ",contactSql +"::"+ phoneSql);
MySQLiteHelper dbhelper = new MySQLiteHelper(this) ;
SQLiteDatabase database = dbhelper.getWritableDatabase();
phone_number = new ArrayList<String>();
Cursor contact = database.rawQuery(contactSql, null);
while(contact.moveToNext()){
nickName = contact.getString(contact.getColumnIndex(MySQLiteHelper.COLUMN_CNT_NICK_NAME));
fname = contact.getString(contact.getColumnIndex(MySQLiteHelper.COLUMN_CNT_FIRST_NAME));
lname = contact.getString(contact.getColumnIndex(MySQLiteHelper.COLUMN_CNT_LAST_NAME));
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.personal_info);
TextView fnameTv = new TextView(ActivityContactDetailScreen.this);
fnameTv.setText(fname +" : "+ lname);
fnameTv.setPadding(20,0,0, 0);
linearLayout.addView(fnameTv);
Cursor phoneCrsr = database.rawQuery(phoneSql, null);
while(phoneCrsr.moveToNext()){
String number = phoneCrsr.getString(phoneCrsr.getColumnIndex(MySQLiteHelper.COLUMN_PHN_NUMBER));
if(!number.isEmpty()){
phone_number.add(number);
}
}
LinearLayout phoneLayout = (LinearLayout) findViewById(R.id.phone_info);
phoneLayout.setVisibility(View.VISIBLE);
ListView phone_list = (ListView) findViewById(R.id.listview_phone_number);
phone_list.setAdapter(new showPhoneNumberAdapter(this));
Log.e("PHONE DETAIL:",phone_number.toString());
phoneCrsr.close();
for(int i = 0; i<contact.getColumnCount();i++){
Log.e("CONTACT DETAIL:",""+ contact.getString(i));
}
Log.e("CONTACT DETAIL END:", "-----------------------------------------------------------------------");
}
contact.close();
database.close();
// To set title of activity
setTitle(nickName);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
datasource.close();
}
// Adapter to show phone numbers
class showPhoneNumberAdapter extends BaseAdapter{
LayoutInflater mInflater;
public showPhoneNumberAdapter(Context context){
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return phone_number.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertedView, ViewGroup parent) {
// TODO Auto-generated method stub
mInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertedView = mInflater.inflate(R.layout.layout_contact_detail_phonelist, parent, false);
TextView tv_number = (TextView) convertedView.findViewById(R.id.phone_number);
tv_number.setText(phone_number.get(position));
Log.e("getView:",":"+phone_number.get(position) +"::"+position);
return convertedView;
}
}
}
答案 0 :(得分:0)
删除ScrollView
更新布局结构将解决您的问题。
或您可以根据内容使用已调整大小的ListView
以使其符合ScrollView
。
// Resizing ListView based on contents
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}