user742030
user742030

Reputation:

How to Get a ListView Item with Android onItemClick?

This is a continuance from a SO Q here but I am still missing something.

I dont know how to get a item that was mapped out from a JSONObject for a list view. The HashMap key value is:

map.put(TAG_RES_FILE, resFile);

And I would like to put that String into my onItemClick(){int passResFile = getResources().getIdentifier(TAG_RES_FILE, "raw", "com.andaero.app");}

I thought by putting the tag name in the method below, the system would automatically pull it from that item position - obviously not. So how do it get it?? Thnx.

EDIT: I added a log.i() to see what values are within the position that is clicked and it returns:

getIdentifier(11925): {isRawRes=true, title=Advisory Circulators, label=AC, _id=1, resFile=advisory_circulators_sort_list, description=Provides guidance such as methods, procedures, and practices for complying with regulations and requirements., containerID=R.id.listContainer}

It's the

resFile=advisory_circulators_sort_list

That is what I need to get - How do I do this??

This is the whole listener:

public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
                    MainActivity.mLayout.toggleSidebar();
                    setHasOptionsMenu(true);

                    FragmentManager fm = getFragmentManager();
                    final FragmentTransaction lcFT = fm.beginTransaction();
                    lcFT.setCustomAnimations(R.anim.slide_in, R.anim.hyperspace_out, R.anim.hyperspace_in, R.anim.slide_out);

                    final Bundle args = new Bundle();

                    Object o = lv.getItemAtPosition(pos);
                    String resFile = (String) o.toString();
                    int passResFile = getResources().getIdentifier(TAG_RES_FILE, "raw", "com.andaero.app");
                    args.putInt("KEY_RES_FILE", passResFile);

                    boolean isRawRes = true;
                    args.putBoolean("KEY_IS_RAW_RES", isRawRes);
                    Log.i("getIdentifier", resFile );

                    // Delayed to improve animations
                    final Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {
                        public void run() {
                            ListViewFragment lvf = new ListViewFragment();
                            lcFT.replace(R.id.listContainer, lvf).commit();
                            lvf.setArguments(args);
                        }
                    }, 300);
                }

Upvotes: 1

Views: 1075

Answers (2)

user742030
user742030

Reputation:

So I went ahead and sent the item to a textView and set it's visability to gone in the layout file. This works just fine but I was hopeing for a better/cleaner way of doing it.

Changed within the onItemClick() method from:

Object o = lv.getItemAtPosition(pos);
String resFile = (String) o.toString();
int passResFile = getResources().getIdentifier(TAG_RES_FILE, "raw", "com.andaero.app");
args.putInt("KEY_RES_FILE", passResFile);

To this:

String resFile = ((TextView) view.findViewById(R.id.listResFile)).getText().toString();
int passResFile = getResources().getIdentifier(resFile, "raw", "com.andaero.app");
args.putInt("KEY_RES_FILE", passResFile);

Upvotes: 1

android developer
android developer

Reputation: 116352

TAG_RES_FILE isn't declared in this method , so it's probably a constant . to show that it's correct , try to debug this method and see if it's changing according to what you would expect.

in fact , in this whole method you don't even look at which item the end user has clicked , not via the view's tag and not via the position of the item . i also don't see any usage of the map that you've talked about.

Upvotes: 0

Related Questions