enter image description here

I have the following layout defined in useful_numbers_item_fragment.xml:

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:id="@+id/call_linear_layout">

        <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:orientation="vertical">
            <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/useful_nums_item_name"/>

            <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/useful_nums_item_value"/>
        </LinearLayout>

       <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:src="@drawable/call"
                android:id="@+id/call_btn"
                android:onClick="callNumber"/>

    </LinearLayout>

I dynamically populate the two text views in a class called UNItemListFragment.java in the onCreate method:

public void onCreate(Bundle savedInstance) {
        super.onCreate(savedInstance);

        if (getArguments().containsKey(Constants.UNItem.GROUP_ID)) {

            simpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.useful_numbers_item_fragment, null,
                    new String[]{Constants.UNItem.NAME, Constants.UNItem.VALUE},
                    new int[]{R.id.useful_nums_item_name, R.id.useful_nums_item_value}, 0);
            setListAdapter(simpleCursorAdapter);
            getLoaderManager().initLoader(0, getArguments(), this);

        }
    }

For each number if i click on the button i want to make a phone call by calling the callNumber method when the user clicks the button:

public void callNumber(View view) {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        TextView unItemVal = (TextView) findViewById(R.id.useful_nums_item_value);

            String phoneNumber = unItemVal.getText().toString();
            callIntent.setData(Uri.parse("tel:" + phoneNumber));
            startActivity(callIntent);
    }

It is ok when I click the first button in the list, but when I click on the other buttons it continues calling the number defined in the first row...

Any idea how to resolve this?

有帮助吗?

解决方案

The problem is that this line:

TextView unItemVal = (TextView) findViewById(R.id.useful_nums_item_value);

is executed on the activity, so the findViewById will always return the first item with that id, which is likely the first item in the list.

The best way to fix this would be to override the adapter and add a tag containing the phone number to the view. A quick way to fix this would be to tag along in the view hierarchy, like so:

public void callNumber(View view) {
    if( view != null ) { // view is the button tapped
        View parent = view.getParent(); // this should be the LinearLayout
        if( parent instanceof LinearLayout ) {
            TextView unItemVal = (TextView) ((LinearLayout)parent).findViewById(R.id.useful_nums_item_value);
            if( unItemVal != null ) {
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                String phoneNumber = unItemVal.getText().toString();
                callIntent.setData(Uri.parse("tel:" + phoneNumber));
                startActivity(callIntent);
            }
        }
    }
}

This would find the parent for the button that was clicked, and then find the text-view containing the number within that ViewGroup.

其他提示

Using findViewById() will return the first view in the activity or fragment with the specified id. If this is a ListView, it will correspond to the first row.

There are many ways to work around this problem. The quickest one (but certainly not the prettiest one, since it depends on the layout) would be to use findViewById() relative to the LinearLayout that contains the list item. Assuming that view is the ImageButton, it would be somthing like:

((View)view.getParent()).findViewById(R.id.useful_nums_item_value)

A more elegant solution would be to set a tag in the adapter's getView(), containing the data you need (in this case, the phone number to call).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top