How to pass variable of which row clicked to my intent? [duplicate]

55
January 13, 2018, at 09:24 AM

This question already has an answer here:

  • How to get the selected item from ListView? 8 answers

I'm creating my first Android app using Java and Android Studio. I've got a main class and layout which (with the help of an adapter) displays a listview of chapter numbers. I've got a clicklistener and when a row is clicked a new activity is loaded. However, what I want to do is to capture a variable which records which row was clicked so that I can load the relevant content in the new activity. What is the best way to do this?

MainActivity:

public class MainActivity extends AppCompatActivity {
    String[] nameArray = {"Chapter 1","Chapter 2","Chapter 3","Chapter 4","Chapter 5","Chapter 6", "Chapter 7", "Chapter 8", "Chapter 9", "Chapter 10", "Chapter 11", "Chapter 12", "Chapter 13", "Chapter 14", "Chapter 15", "Chapter 16", "Chapter 17", "Chapter 18", "Chapter 19", "Chapter 20", "Chapter 21" }; //List of chapters to display
    ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ChapterlistAdapter whatever = new ChapterlistAdapter(this, nameArray);
        listView = (ListView) findViewById(R.id.chapterListview);
        listView.setAdapter(whatever);
        // Make the row respond when clicked
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                // TODO Auto-generated method stub
                Log.d("dev","Its clicked" );
                goToChapter();
            }
        });
    }
    /** Called when the user taps the Send button */
    public void goToChapter() {
        Intent intent = new Intent(this, CardView.class);
        intent.putExtra(EXTRA_MESSAGE, "chapter clicked");
        startActivity(intent);
    }

ChapterlistAdapter:

public class ChapterlistAdapter extends ArrayAdapter {
    //to reference the Activity
    private final Activity context; //Stores which activity the list listview is on?
    //to store the list of countries
    private final String[] chapterArray;
    public ChapterlistAdapter(Activity context, String[] nameArrayParam){
        super(context,R.layout.chapter_listview_row , nameArrayParam);
        this.context=context;
        this.chapterArray = nameArrayParam;
    }
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater=context.getLayoutInflater();
        View rowView=inflater.inflate(R.layout.chapter_listview_row, null,true);
        //this code gets references to objects in the listview_row.xml file
        TextView nameTextField = (TextView) rowView.findViewById(R.id.chapterListviewRowText);
        //this code sets the values of the objects to values from the arrays
        nameTextField.setText(chapterArray[position]);

        return rowView;
    };
}

My thoughts are to pass a variable in the putExtra method - however I'm not sure how to capture that variable. Can I get it from MainActivity? Or do I have to do it in the adapter class?

The end goal is to know which chapter was clicked so that content for that chapter can be displayed.

Answer 1

Get the clicked item by the position like:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //get here using position  
        String selected = (String) parent.getItemAtPosition(position);                     
    }
});
READ ALSO
getMapAsync() is not giving callback to onMapReady() to initialize googleMap object

getMapAsync() is not giving callback to onMapReady() to initialize googleMap object

I have been working on google maps and below is the method I am using to initialize the mapSurprisingly, mapFragment

78
Switching to first tab in tabbed activity not working properly in android

Switching to first tab in tabbed activity not working properly in android

So I have a weird problemBasically I have a Main activity with four buttons, each button opens a different tab (there is 4 tabs)

75
How do we show a notification in the OS with React Native and Android?

How do we show a notification in the OS with React Native and Android?

So I just set up a Firebase function to send notifications to my devices and I added react-native-firebase to my project so I signin, get a token and receive notifications

81
Android 8.0 Oreo RunningServiceInfo returns 0 services

Android 8.0 Oreo RunningServiceInfo returns 0 services

my Oneplus 5 just updated to Oreo and something has brokenI have a simple widget which starts and stops some service

75