【问题标题】:BaseAdapter is not showing all dataBaseAdapter 未显示所有数据
【发布时间】:2014-03-11 11:46:08
【问题描述】:

我正在使用BaseAdapter 填充ListView。我的数组有两个值,我得到 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;
        }   
    }

}

【问题讨论】:

  • 您的 ListView 是否呈现在 ScrollView 中?
  • 使 getItem() 返回值为 null 到 phone_number.get(arg0)
  • @SujithPt :是的,有主滚动视图->相对布局->线性布局->列表视图。
  • @I-droid 你们为什么不使用 SimpleCursorAdapter,它是今天第三个使用 BaseAdapter 来显示 sqlite db 内容的人...
  • @pskink :我正在使用rawquery 获取数据。我不知道如何将它与 SimpleCursorAdapter 一起使用。如果你告诉我怎么用,我会用它。

标签: android listview android-listview baseadapter


【解决方案1】:

请在adapter设置为listview后调用此方法。此方法计算listview的总高度并设置,因此您无需删除Scrollview。

public static void getTotalHeightofListView(ListView listView) {

ListAdapter mAdapter = listView.getAdapter();

int totalHeight = 0;

for (int i = 0; i < mAdapter.getCount(); i++) {
    View mView = mAdapter.getView(i, null, listView);

    mView.measure(
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),

            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

    totalHeight += mView.getMeasuredHeight();
    Log.w("HEIGHT" + i, String.valueOf(totalHeight));

}

ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
        + (listView.getDividerHeight() * (mAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();

}

【讨论】:

  • 大声笑,这段代码没有意义......它不再是 ListView You've just recreated a very expensive LinearLayout :) – Romain Guy(android team)
  • @Selvin 是的。但是在我们的整体布局非常大的时候,我们想在 ScrollView 中管理列表视图,那时我们只有这一个选项。
  • 将 mView 移到循环外会有所帮助,但实际上在 scrollview 内滚动视图是个坏主意
  • @Kalpesh:我在这个列表视图中有一个imagebutton。使用上述方法,我无法获取列表视图的 onItemClicked 的点击事件。为什么?
【解决方案2】:

通过删除 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();
}

【讨论】:

  • 我不会使用那个滚动视图,但是我的布局可以有比屏幕尺寸更长的内容。所以有必要使用它。
  • 那么你可以选择第二个选项。
  • 第二个选项可以阻止我的列表视图的项目获得点击事件吗??
猜你喜欢
  • 2015-09-26
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多