Constant id for all the items of a ListView making OnClick method useless

I'm quite new to android and i'm facing some trouble with managing listviews... I'm generating a ListView properly but all the items in it have the same id, so when I click on any of them, they all do the same thing (which is not what I expect of course...)

I'm loading data from XML URLs, parsing them with a SAX parser. Here is my adapter. My "liste" contains 6 rows of 2 strings separated by "&&&". I then display the listview with :

  zadapter.notifyDataSetChanged();
   setListAdapter(this.zadapter);

source below


class InfoAdapter extends ArrayAdapter<String> {
  private ArrayList<String> items;
  public InfoAdapter(Context context, int textViewResourceId, ArrayList<String> items) {
   super(context, textViewResourceId, items);
   this.items = items;
  }

  public View getView(int position, View convertView, ViewGroup parent) {
   View v = convertView;
   if (v == null) {
    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = vi.inflate(R.layout.row, null);
   }
   String liste = items.get(position);
   String[] info = liste.split("&&&");
   if (liste != null) {
    TextView tt = (TextView) v.findViewById(R.id.toptext);
    TextView bt = (TextView) v.findViewById(R.id.bottomtext);
    if (tt != null) {
     tt.setText(info[0]);
    }
    if(bt != null){
     bt.setText(info[1]);
    }
    Log.e(MY_DEBUG_TAG, "Debug position text"+tt.getId());
   }
   Log.e(MY_DEBUG_TAG, "Debug position view"+v.getId());
   return v;
  }
 }

Thank you in advance for any help.


Asked by: Vivian439 | Posted: 25-01-2022






Answer 1

In your debug statement, you are having the id of the view which is an identifier and what I believe you want is the index.

To trigger an action that depends on the selected item, you have to use the setOnItemClickListener method from the ListView and implement the OnItemClickListener interface.

The method public void onItemClick(AdapterView<?> list, View v, int pos, long id) is the one you need. See the description of the method here

Answered by: Melanie486 | Posted: 26-02-2022



Answer 2

You can request the listview for an index of the selected item and use that, in combination with your listadapter to figure out the actual data of the view.

Answered by: Nicole602 | Posted: 26-02-2022



Answer 3

I think you may want to extend from BaseAdapter (doesn't require much more work), as ArrayAdapter<T>'s internal list of objects is hidden from your subclass and so your overriden methods are using a completely different list of objects.

See the ArrayAdapter source for more details, especially the private List<T> mObjects bit.

Answered by: Chester720 | Posted: 26-02-2022



Similar questions

frame rate - Android game with constant FPS?

I'm implementing a game loop with constant FPS. I'm thinking about 25-30 FPS should be enough. Any ideas from your own experience? Should I even restrict FPS at all?


javascript - How to prevent webkits constant url loading url / throbber of doom

There is a few questions about this, but mostly lacking details and no definitive answer. So I am using xhr long polling, my poll is pretty basic and just looks like var poll = function() { $.get(url, function(data) { doStuff(data); poll(); }); }; I start that from a script that is imported just before , I remove absolutely everything else on the page so the only thin...


I need a constant and unified solution to stream audio in Android and IOS web browsers

I need a constant and unified solution to stream audio in Android and IOS web browsers. Actually, I have it in flash based but I want something for mobile web browsers instead of flash. Thanks


android - Constant live update

Does anybody know of a way to have my application constantly check firstly if there is internet connectivity..and secondly if there is internet connection to call a function...It needs to check for internet coverage say every 10 minutes etc?


Android SQL Null Constant Value

I have this cuestion for you... We are trying to pass an argument to a query in a content resolver. We want to get all rows if the argument is NULL, or get the specific value indicated at the argument as contrary. The method we use declares: static String allArtists = ""; //// Some code ... Vector&lt;String&gt; discs = new Vector&lt;String&gt;(); discs = getD...


android - String error "constant string too long"

There is a 100,000-character text that need to be displayed. If I put it into String object, I get an error "constant string too long". The same is with StringBuffer object. StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("Long text here........"); //&lt;-- error Is there a solution to this beside cutting the text into smaller texts?


android - Define a map as constant in java

For my Android app I've the need of defining some keys in a single constant, and I think the best way to do it is using a map. But not sure whether that's really the way to go, and how to do it correctly. As I'm targeting Android, a Bundle may also be an option. I have a list of keys like: "h" = "http" "f" = "ftp" Basically the program is to read a QR code (to keep that code from growing too big I'm...


Android set constant Button in more than 5 activity

Hi I am new to Android I want to Set One Button named (Click) in More than Five Activity like Title of the Application at the Top.I did not want it to add on all the 5 activity Manually is there any simple way in Android?


java - Access Urban Airship Constant From PhoneGap

Does anyone know how I can access the APID constant - EXTRA_APID (com.urbanairship.push.APID) from within PhoneGap or in Javascript? Thanks!


android - service doesn't keep constant control of main thread on UI

I have a service in my Android application that has a UI its running. The problem I am running into, it that on various devices, it doesn't seem to maintain control of the main thread. For instance, it does not ALWAYS respond to the back button being pressed. Is there a way to ensure that the service always has control of the UI? Thanks.






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top