如何在不同的布局中获取id

时间:2010-11-19 01:19:45

标签: android listview

当我使用findVewById(布局setcontextview(R.layout.main))时,我在single_item.xml中输入id“@ + id / call”。应用程序崩溃。如何修复错误 代码single_items.xml用于在类CustomSqlCursorAdapter中扩展listView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
    android:src="@drawable/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible"
/>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:minWidth="30px"
    >
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="30px"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name: "
            android:id="@+id/name"
            android:textColor="#2E6100"
            android:textStyle="bold"
        />
        <TextView android:id="@+id/contact_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#2E6100"
             android:textStyle="bold"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="30px">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Phone: "
            android:clickable="true"
            android:id="@+id/phone"
            android:textColor="#2E6100"/>
        <TextView android:id="@+id/phone_number"
            android:layout_width="wrap_content"
           android:layout_height="wrap_content"
            android:textColor="#2E6100"/>
    </LinearLayout>
</LinearLayout>
<ImageView
    android:src="@drawable/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/call"
    android:clickable="true"
/> 
</LinearLayout>

class managecontact.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mDbHelper = new database(this);
    mDbHelper.open();


    /* ImageView call=(ImageView)this.findViewById(R.id.call);
     call.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View v) {
            //TODO Auto-generated method stub
            call();
        }                
     });*/



    registerForContextMenu(getListView());
}


private void fillData() {

    Cursor currentCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(currentCursor);

    // Create an array to specify the fields we want to display in the list (only TITLE)
    String[] from = new String[]{database.KEY_TITLE};

    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{ R.id.contact_name};

    // Now create a simple cursor adapter and set it to display
    CustomSqlCursorAdapter adapter = new CustomSqlCursorAdapter(this, R.layout.single_item,currentCursor, from, to);
    setListAdapter(adapter);

}
private void call() {

    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:123456789"));
    startActivity(callIntent);

}

我想在点击图片图标时拨打电话。请告诉我如何更正

1 个答案:

答案 0 :(得分:1)

  • 您可能在自定义ListAdapter中使用了inflater
  • 在这方面,可能已经完成了不完全相同的事情。
  • `如果() { LinearLayout ll = yourInflater.inflate(....); else {ll =(LinearLayout)convertView;}
  • 为了获得呼叫ID,请执行以下操作:  `ImageView call = ll.findViewById(r.id.call);



希望它会帮助你............