android:Custom SimpleCursorAdapter不加载所有数据

时间:2012-08-10 19:53:31

标签: android simplecursoradapter

需要帮助,我有一个listview活动,我希望每个项目显示几个文本字段加上2个按钮(比如有一个呼叫按钮和一个短信按钮的联系人应用程序。阅读教程我发现了如何获取按钮可点击,但现在我的textviews(除了1之外的所有文本视图)都没有显示除了我在布局文件中留下的股票文本之外的任何数据.1文本视图显示我需要的数据。没有在我的位置自定义SCA我是否提供特定数据,所以我假设它发生在后台。

我的R.id.company_address_details_phone在没有任何操作的情况下加载了所需的数据,但我的ViewHolder中的其他TextViews都没有显示任何数据。附加到我的调用按钮的我的For循环确实从我的光标输出数据,所以我知道这不是我缺少数据。任何和所有的帮助表示赞赏。

作为旁注,当第一次编写代码并且没有使用自定义的SimpleCursorAdapter时,我的所有文本视图都按预期填充(我只是无法单击按钮)。这导致我假设我的CompanyActivity类没有错误,但我在CompanyDetailsCursroAdapter类中遗漏了一些东西。

*的 修改 *** 所以我回头看了之后回答了自己的问题。在我填充的视图中,我只是将我的一个视图设置为默认文本,使其看起来好像是从我的数据库中提取的。我现在使用下面的代码使用传递给我的自定义游标适配器的游标适配器中的数据填充我的视图。我想知道这是否是最佳方式,还是有更简化或Android批准的方法。

            viewHolder.addressTextView.setText(companyDetailsCursor.getString(companyDetailsCursor.getColumnIndex("AddressStreet")));
        viewHolder.cityTextView.setText(companyDetailsCursor.getString(companyDetailsCursor.getColumnIndex("AddressCity")));
        viewHolder.stateTextView.setText(companyDetailsCursor.getString(companyDetailsCursor.getColumnIndex("AddressState")));
        viewHolder.zipTextView.setText(companyDetailsCursor.getString(companyDetailsCursor.getColumnIndex("AddressZip")));
        viewHolder.phoneTextView.setText(companyDetailsCursor.getString(companyDetailsCursor.getColumnIndex("AddressPhone")));

CompanyDetailsCursorAdapter.java

public class CompanyDetailsCursorAdapter extends SimpleCursorAdapter implements Filterable{
private final Context context;
private int layout;
Cursor companyDetailsCursor;

public CompanyDetailsCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);

    this.context = context;
    this.layout = layout;
    this.companyDetailsCursor = c;
}

static class ViewHolder {
    protected TextView addressTextView;
    protected TextView cityTextView;
    protected TextView stateTextView;
    protected TextView zipTextView;
    protected TextView phoneTextView;

    protected ImageButton callButton;
    protected ImageButton mapButton;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;

    if (convertView == null) {
        final LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(R.layout.company_address_details, null);

        convertView.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
                // TODO Auto-generated method stub

            }

        });

        viewHolder = new ViewHolder();
        viewHolder.addressTextView = (TextView) convertView.findViewById(R.id.company_address_details_street);
        viewHolder.cityTextView = (TextView) convertView.findViewById(R.id.company_address_details_city);
        viewHolder.stateTextView = (TextView) convertView.findViewById(R.id.company_address_details_state);
        viewHolder.zipTextView = (TextView) convertView.findViewById(R.id.company_address_details_zip);
        viewHolder.phoneTextView = (TextView) convertView.findViewById(R.id.company_address_details_phone);

        viewHolder.callButton = (ImageButton) convertView.findViewById(R.id.company_address_details_call_button);

        viewHolder.callButton.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
                companyDetailsCursor.moveToPosition(position);
                if (RouteTrackerGlobal.APP_DEBUG) { Log.d(RouteTrackerGlobal.APP_TAG,"getview position set to " + String.valueOf(position));}
                for (int i = 0; i < companyDetailsCursor.getColumnCount();i++){
                    Log.i(RouteTrackerGlobal.APP_TAG, companyDetailsCursor.getString(i));
                }
            }

        });

        convertView.setTag(viewHolder);

    } else {
        viewHolder  = (ViewHolder) convertView.getTag();
    }

    return convertView;

}

CompanyActivity.java

public class CompanyActivity extends ListActivity {
DbAdapter dbAdapter;
int companyID;

Context context = this;

Cursor companyDetailsCursor;
@Override
public void onCreate(Bundle savedInstanceBundle) {
    super.onCreate(savedInstanceBundle);

    dbAdapter = new DbAdapter(this);

    setContentView(R.layout.company_activity);

    Bundle companyActivityBundle = getIntent().getExtras();

    companyID = companyActivityBundle.getInt("CompanyID");

    ListView list = getListView();

    list.setOnItemLongClickListener(new OnItemLongClickListener(){

        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            return false;
        }

    });
}

@Override
public void onPause() {
    dbAdapter.close();
    super.onPause();
}

@Override
public void onResume() {
    dbAdapter.open();
    super.onResume();
    loadDetails();
}

@Override
protected void onListItemClick (ListView l, View v, int position, long id){
    //TODO: When item in list clicked, call activity to display address and directions
}

public void loadDetails() {
    TextView companyLabel = (TextView) findViewById(R.id.company_activity_company_label);

    companyDetailsCursor = dbAdapter.getCompanyDetails(companyID);
    startManagingCursor(companyDetailsCursor);

    companyDetailsCursor.moveToFirst();

    companyLabel.setText(companyDetailsCursor.getString(companyDetailsCursor.getColumnIndex("CompanyName")));

    String[] columns = new String[] {"AddressPhone", "AddressStreet", "AddressCity", "AddressState", "AddressZip"};
    int[] to = new int[] {R.id.company_address_details_phone, R.id.company_address_details_street, R.id.company_address_details_city, R.id.company_address_details_state, R.id.company_address_details_zip};

    CompanyDetailsCursorAdapter companyDetailsAdapter = new CompanyDetailsCursorAdapter(context, R.layout.company_address_details, companyDetailsCursor, columns, to);
    setListAdapter(companyDetailsAdapter);


}

}

Company_Address_Details.xml

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <TextView
            android:id="@+id/company_address_details_phone"
            style="@style/company_address_listview"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="@string/company_address_details_phone_default" />
        <ImageButton android:id="@+id/company_address_details_call_button"
            style="@style/company_address_listview"
            android:layout_alignParentRight="true"
            android:layout_centerInParent="true"
            android:background="@android:color/transparent"
            android:src="@android:drawable/sym_action_call"
            android:contentDescription="@string/company_address_details_phone_hint"/>
    </RelativeLayout>


    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >


        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/company_address_details_call_button1"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/company_address_details_street"
                    style="@style/company_address_listview"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/company_address_details_street_default" />
            </LinearLayout>


            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >



                <TextView
                    android:id="@+id/company_address_details_city"
                    style="@style/company_address_listview"
                    android:layout_width="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:text="@string/company_address_details_city_default" />



                <TextView
                    android:id="@+id/company_address_details_state"
                    style="@style/company_address_listview"
                    android:layout_centerInParent="true"
                    android:layout_centerVertical="true"
                    android:text="@string/company_address_details_state_default" />



                <TextView
                    android:id="@+id/company_address_details_zip"
                    style="@style/company_address_listview"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:text="@string/company_address_details_zip_default" />

            </RelativeLayout>

        </LinearLayout>







        <ImageButton
            android:id="@+id/company_address_details_call_button1"
            style="@style/company_address_listview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerHorizontal="false"
            android:layout_centerInParent="false"
            android:layout_centerVertical="true"
            android:background="@android:color/transparent"
            android:contentDescription="@string/company_address_details_phone_hint"
            android:paddingLeft="20dp"
            android:src="@android:drawable/ic_dialog_map" />

    </RelativeLayout>

</LinearLayout>

0 个答案:

没有答案