基于arraylist项中的位置的多个不同意图

时间:2017-06-20 09:19:17

标签: java android listview android-intent arraylist

我一直在寻找很长时间,但仍然没有找到任何解决方案。 我想要的是当你点击其中一个自定义列表项https://prnt.sc/flwqe3时,点击它时每个列表项都有他自己的意图。

如果有人知道答案,请告诉我。

这是自定义列表项的活动

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

        //Set the titel of the actionbar
        TextView actionbartitel = (TextView) findViewById(R.id.actionbar_titel);
        Typeface  = Typeface.createFromAsset(getAssets(), "fonts/.ttf");
        actionbartitel.setTextSize(30);
        actionbartitel.setTypeface();
        actionbartitel.setText("");

        //set the back button of the actionbar
        ImageView actionbarimage = (ImageView) findViewById(R.id.actionbar_logo);
        actionbarimage.setImageResource(R.drawable.ic_arrow_back_white_24dp);
        // Set a click listener on that View
        actionbarimage.setOnClickListener(new View.OnClickListener() {
            // The code in this method will be executed when the family category is clicked on.
            @Override
            public void onClick(View view) {
                // Create a new intent to open the {@link FamilyActivity}
                Intent mainIntent = new Intent(Activity.this, MainActivity.class);

                // Start the new activity
                startActivity(mainIntent);
            }
        });


        interstitial = new InterstitialAd(this);
        interstitial.setAdUnitId(getResources().getString(R.string.interstitial_ad_unit_id));
        AdRequest adRequest = new AdRequest.Builder().build();
        interstitial.loadAd(adRequest);
        interstitial.setAdListener(new AdListener() {
            public void onAdLoaded() {
                displayInterstitial();
            }
        });


        ListView listView = (ListView) findViewById(R.id.list);
        ArrayList<Subjects>  = new ArrayList<Subjects>();

        .add(new Subjects("", "", R.mipmap.ic_launcher, "5:60", 0xff00ffff,
                Typeface.createFromAsset(getAssets(), "fonts/Font.ttf"), Typeface.createFromAsset(getAssets(), "fonts/timeFont.ttf")));

        .add(new Subjects("", " " + System.getProperty("line.separator") + " ", R.mipmap.ic_launcher, "5:60", 0xff0000ff,
                Typeface.createFromAsset(getAssets(), "fonts/Font.ttf"), Typeface.createFromAsset(getAssets(), "fonts/timeFont.ttf")));


        WordAdapter adapter = new WordAdapter(this, list);
        listView.setAdapter(adapter);
    }

    public void displayInterstitial() {
        if (interstitial.isLoaded()) {
            View loadingIndicator = findViewById(R.id.classicmprogressbar);
            loadingIndicator.setVisibility(View.GONE);

            interstitial.show();
        }
    }
}

1 个答案:

答案 0 :(得分:2)

当您引用Array List的每个项目都有自己的意图和ArrayList Has Subject类对象时,您可以在Subject类中添加Class对象,如Class cls; 并将其添加到arrayList

ArrayList<Subjects> subjects = new ArrayList<Subjects>();

    .add(new Subjects("", "", R.mipmap.ic_launcher, "5:60", 0xff00ffff,
            Typeface.createFromAsset(getAssets(), "fonts/Font.ttf"), Typeface.createFromAsset(getAssets(), "fonts/timeFont.ttf"),IntentOne.class));

    .add(new Subjects("", " " + System.getProperty("line.separator") + " ", R.mipmap.ic_launcher, "5:60", 0xff0000ff,
            Typeface.createFromAsset(getAssets(), "fonts/Font.ttf"), Typeface.createFromAsset(getAssets(), "fonts/timeFont.ttf"),IntenTwo.class));
单击

和单击列表项,可以使用以下代码访问

// Create a new intent to open the {@link FamilyActivity}
            Intent mainIntent = new Intent(Activity.this, adapter.getItem(position).cls);

            // Start the new activity
            startActivity(mainIntent);