1

Im trying to make ListView using data from Array, but I need to get Id of clicked row (not shown in that row, but userd in creation of that product)

Im using this class for object:

package com.example.raidplanner;
public class RaidWpis {
private int id;
private int id_gildia;
private String nazwa;
private int schemat;
private int data_zapis;
private int data_start;
private int opis;
private int id_officer;
private int nick_officer;
private int typ;

public RaidWpis(int id,String nazwa) {
    setNazwa(nazwa);
    setId(id);
}

public int getId()                  { return id; }

public void setId(int id)           { this.id = id; }

public String getNazwa()            { return nazwa; }

public void setNazwa(String nazwa)  { this.nazwa = nazwa; }

public String toString() {
        return this.nazwa;
    }
    public String toString2() {
        return this.id+" - "+nazwa;
    }

}

And this code in Activity:

RaidWpis[] items = {
            new RaidWpis(1, "aaaa"),
            new RaidWpis(3, "bbbb"),
            new RaidWpis(6, "cccc"),
            new RaidWpis(11, "dddd"),
            new RaidWpis(17, "eeee"),
        };        
    mainListView = (ListView) findViewById( R.id.mainListView );  

    ArrayAdapter<RaidWpis> raidList = new ArrayAdapter<RaidWpis>(this, R.layout.simplerow, items);  

    // Create ArrayAdapter using the raid list.  
    mainListView.setAdapter(raidList);

    mainListView.setOnItemClickListener(new OnItemClickListener() {
          @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) 
          {
                String item = ((TextView)view).getText().toString();
                Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();   
          }
        });

2 Answers 2

1

I need to get Id of clicked row (not shown in that row, but userd in creation of that product)

Try this in your onItemClick() method:

RaidWpis obj = parent.getAdapter().getItem(position); // or just raidList.get(position) if raidList is a field variable
Toast.makeText(getBaseContext(), obj.getID() + "", Toast.LENGTH_LONG).show();
Sign up to request clarification or add additional context in comments.

5 Comments

when I use it i get error in Eclipse: Multiple markers at this line - The method get(int) is undefined for the type capture#1-of ? - Line breakpoint:Raidy_lista [line: 60] - onItemClick(AdapterView<?>, View, int, long)
Sorry, it should be getItem() not get(). I don't believe you need to cast the Adapter with (ArrayAdapter<RaidWpis> parent.getAdapter()).getItem(position), but I'm away from my compiler at the moment.
Thanks a Lot, I Added Cast to RaidWpis and your solution worked just fine.
Glad I could help, please click the check mark in this answer to accept it as the solution.
Sry but I dont have 15 rep to do this.
1

from the position, you can get the RaidWpis Object by using raidList.get(position). Once you have this RaidWpis object, you can call getId() to get the id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.