1

需要帮助,我有一个 listview 活动,我想为每个项目显示几个文本字段加上 2 个按钮(例如具有呼叫按钮和文本消息按钮的联系人应用程序。阅读教程我发现了如何让按钮可点击,但现在我的文本视图(除 1 之外的所有)不显示除我留在布局文件中的股票文本之外的任何数据。1 文本视图确实显示了我需要的数据。在我的自定义 SCA 中没有我给它提供了特定的数据,所以我假设它发生在后台。

我的 R.id.company_address_details_phone 无需我进行任何操作即可加载所需的数据,但我的 ViewHolder 中的其他 TextView 均不显示任何数据。连接到我的呼叫按钮的 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;

}

公司活动.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>
4

0 回答 0