javaandroidcustom-arrayadapter

Intent in Custom Adapter


Hi i Need to add Intent in my onClick,in adapter to be connected with my Arraylist,and pass diferent link for diferent item in listview.My code is below so what changes do i need to make?How can i connect Intent with my adapter and which commands i need to use

 package com.example.android.extremebiceps;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;



public class Workout extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_workout);

        //kreirenje arraja
        final ArrayList<Word> workouts = new ArrayList<Word>();
        workouts.add(new Word("workout1",R.layout.activity_main));
        workouts.add(new Word("workout2",R.layout.activity_main));
        workouts.add(new Word("workout3",R.layout.activity_main));
        workouts.add(new Word("workout4",R.layout.activity_main));
        workouts.add(new Word("workout5",R.layout.activity_main));




        // kreiranje variable za pracenje trenutne pozicije


        WordAdapter adapter=new WordAdapter(this,workouts);


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

        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long l) {

                Word word = workouts.get(position);


            }
        });

    }
}

My Adapter

package com.example.android.extremebiceps;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;

public class WordAdapter extends ArrayAdapter<Word> {
    public WordAdapter(Activity context, ArrayList<Word> Workouts) {
        // Here, we initialize the ArrayAdapter's internal storage for the context and the list.
        // the second argument is used when the ArrayAdapter is populating a single TextView.
        // Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
        // going to use this second argument, so it can be any value. Here, we used 0.
        super(context, 0, Workouts);
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        // Check if the existing view is being reused, otherwise inflate the view
        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.list_item, parent, false);
        }
        Word currentWord = getItem(position);

        //nadzi button
        Button buttonView = (Button) listItemView.findViewById(R.id.Button1);
        //uzmi tekst iz buttona
        buttonView.setText(currentWord.getDefaultButton());




        return listItemView;
    }

and my Word class

package com.example.android.extremebiceps;


public class Word {

    //trazenje buttona
    private String mDefaultButton;

    //Trazenje intenta
    private int mLink;

    public Word(String DefaultButton,int Link){
        mDefaultButton=DefaultButton;
        mLink=Link;
    }

    //uzimanje buttona
    public String getDefaultButton(){
        return mDefaultButton;
    }
    //uzimanje intenta
    public int getLink(){
        return mLink;
    }
}

And when i try to add Toast in my onItemClickListener it doeesnt work.Why


Solution

  • listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
            Word word = workouts.get(position);
            Intent in = new Intent(Workout.this, NextActivity.class);
            in.putExtra("Word", wokouts.get(position).yourMethod());
            startActivity(in);
    
         }
    });