R
R
run1822017-01-08 23:59:50
Java
run182, 2017-01-08 23:59:50

Why does android launch an activity on click?

public class AllPlacesActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_places);

        // Hashmap for ListView
        productsList = new ArrayList<HashMap<String, String>>();

        // Загружаем продукты в фоновом потоке
        new LoadAllPlaces().execute();

        // получаем ListView
        ListView lv = getListView();

        // на выбор одного продукта
        // запускается Edit Product Screen
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // getting values from selected ListItem
                String ID = ((TextView) view.findViewById(R.id.ID)).getText().toString();

                // Запускаем новый intent который покажет нам Activity
                Intent in = new Intent(AllPlacesActivity.this, EditPlaceActivity.class);
                // отправляем pid в следующий activity
                in.putExtra(ID, ID);

                // запуская новый Activity ожидаем ответ обратно
                startActivity(in);
            }
        });
    }

   ...
}

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


        <fragment
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/fragment"
            tools:layout="@layout/top_panel" />


        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>


    </LinearLayout>
</LinearLayout>

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

        <LinearLayout
            style="@style/placeItem">
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#eff3f3"/>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/ID"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:visibility="gone"/>
                <TextView
                    style="@style/placeName"
                    android:id="@+id/ADDRESS"
                    android:text="BUBBLE BOOM KENIGSBERG | 1км | Победы площадь 10 2 этаж, ТЦ Кловер" />
                <LinearLayout
                    style="@style/placeItemButton">
                    <ImageButton
                        style="@style/placeItemEnter"/>
                    <View
                        style="@style/placeButtonBorder"
                        />
                    <LinearLayout
                        style="@style/placeItemPeoples"
                        android:gravity="center">
                        <ImageButton
                            style="@style/placeItemPeoplesImg"/>
                        <TextView
                            style="@style/placeItemPeoplesTxt"
                            android:text="5"/>
                    </LinearLayout>
                </LinearLayout>
            </RelativeLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#d5d8d8"/>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#e5e9e9"/>
        </LinearLayout>

</LinearLayout>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Elysey, 2017-01-09
@run182

Intent in = new Intent(getApplicationContext(), EditPlaceActivity.class);

there should not be getApplicationContext(), but the activity context from where you want to call EditPlaceActivity
For example:
Intent in = new Intent(MainActivity.this, EditPlaceActivity.class);

D
davidnum95, 2017-01-09
@davidnum95

In my opinion, you can not run activities from the general application context.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question