How to make phone calls programmatically in Android?

enter image description here

I have the following layout defined in the_numbers_item_fragment.xml utility file:

<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 two textual representations in the UNItemListFragment.java class 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 the button, I want to 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);
    }

This is normal when I click the first button in the list, but when I click on the other buttons it continues to call the number defined on the first line ...

Any idea how to solve this problem?

+2
source share
2 answers

The problem is that this line:

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

, findViewById , , , .

, , . , , :

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);
            }
        }
    }
}

, , , ViewGroup.

+9

findViewById() first id. ListView, .

. (, , , ) findViewById() LinearLayout, . , view ImageButton, :

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

getView(), ( ).

0

Source: https://habr.com/ru/post/1616334/


All Articles