【问题标题】:Multiple different intents based on position in arraylist item基于数组列表项中的位置的多个不同意图
【发布时间】:2017-06-20 09:19:16
【问题描述】:

我已经找了很长时间,但仍然没有找到任何解决方案。 我想要的是,当您单击其中一个自定义列表项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();
        }
    }
}

【问题讨论】:

    标签: java android listview android-intent arraylist


    【解决方案1】:

    正如您所指的 Array List 的每个项目都有自己的意图,而 ArrayList 有 Subject 类对象您可以做什么,在 Subject 类中您可以添加一个 Class 对象,如 Class cls; 并将其添加到arrayList 中:

    ArrayList&lt;Subjects&gt; subjects = new ArrayList&lt;Subjects&gt;();

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

    【讨论】:

    • 我编辑了主题并将课程添加到列表项中
    • 最终 WordAdapter 适配器 = new WordAdapter(this, obsclassiclist); listView.setAdapter(适配器); listView.setOnItemClickListener(new ListView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { // 新建intent 打开{@link FamilyActivity} Intent mainIntent = new Intent(ObsclassicActivity.this, adapter.getItem(position).cls); // 启动新活动 startActivity(mainIntent); }
    • 当我点击仍然没有任何事情发生时,我的听众有什么不对劲
    • 你需要给listView添加监听器,即listView.setOnItemClickListener
    • prntscr.com/fm2kff 这是我当前在活动中拥有的监听器,当我按下它时,什么都没有发生
    猜你喜欢
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多