簡體   English   中英

Android 新的 Intent 在 ListView 后沒有響應

[英]Android new Intent not responding after ListView

我想在兩個活動之間設計一些意圖。 我在沒有聲明任何東西的情況下通過了意圖。 我想定義 Adapter 以在意圖中膨脹列表視圖,但在打開意圖時遇到了這個問題。 它是由列表視圖引起的。 我該如何解決?

這是下面的代碼。

public class AttactivePlacesActivity extends AppCompatActivity {

    ArrayList<City> c = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a);

        MainActivity m = new MainActivity();
        c = m.mArraylist;


        AAdapter r = new AAdapter(this,c,R.color.mainBackground);

        ListView listView = (ListView) findViewById(R.id.aa);

        listView.setAdapter(r);




    }
}

R.layout.a

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/aa"
    tools:context="com.example.android.tourguide.AttactivePlacesActivity">

</ListView>

網格視圖意圖

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

            switch (position){

                case 0:
                    Toast.makeText(getApplicationContext(),"A",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(MainActivity.this,AttactivePlacesActivity.class);
                    startActivity(intent);
                    break;

            }

        }
    });

您創建了MainActivity的引用來獲取mArrayList ,不是嗎? 首先,請不要在另一個活動中創建任何活動的參考。 其次,如果您想在第二個活動中獲取mArraylist ,只需將其放入 Intent 中,如下所示:

intent.putSerializable("我的數組",mArrayList);

並通過使用接收它:

c = getIntent.getSerializable("我的數組");

P/S:給你一些建議,請用有意義的名字命名變量,嘗試在開始Android之前仔細學習OOP java。 最后一個,先搜索再問

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM