0

I am working on a 4 tabs application.

One of my fragment is the search one, containing a searchview, textview, listview.

fragmentSearch.java

JSONObject jsonobject = new JSONObject(result);
textView1.setText((String) jsonobject.get("count"));

Integer count = Integer.parseInt((String) jsonobject.get("count"));

 if (count == 0) {
     // There are no results
    textView1.setText(getString(R.string.no_results, query_string));
 } else {
     // Display the number of results
     String countString = getResources().getQuantityString(R.plurals.search_results,
            count, new Object[] {count, query_string});
     textView1.setText(countString);

  // Specify the columns we want to display in the result
     String[] from = new String[] { "title",
                                    "artist",
                                    "duration",
                                    "url"};

     // Specify the corresponding layout elements where we want the columns to go
     int[] to = new int[] { R.id.title,
                            R.id.artist,
                            R.id.duration,
                            R.id.url};

     // -- container for all of our list items
     List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();

     // -- list item hash re-used
     Map<String, String> group;

     //create audio json array
     JSONArray audios = jsonobject.getJSONArray("audio");

     for (int i = 0; i < audios.length(); i++) {
         JSONObject audio = audios.getJSONObject(i);

         //get info
         String title = audio.getString("title");
         String artist = audio.getString("artist");
         String duration = audio.getString("duration");
         String durationformated = getDurationString(Integer.parseInt(duration));
         String url = audio.getString("url");

         // -- create record
        group = new HashMap<String, String>();

        group.put( "title", title );
        group.put( "artist",  artist );
        group.put( "duration",  durationformated );
        group.put( "url",  url );

        groupData.add(group);
     }

     SimpleAdapter adapter = new SimpleAdapter(searchContext , groupData, R.layout.result, 
            from,
             to );

     listView1.setAdapter( adapter );

and here is my result.xml

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:padding="5dp">

      <TextView
         android:id="@+id/url"
         android:layout_width="0dp"
         android:layout_height="0dp"
         android:visibility="gone"/>

     <TextView
         android:id="@+id/title"
         style="@android:style/TextAppearance.Large"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:layout_toLeftOf="@+id/play"
         android:singleLine="true"
         android:text="title"
         android:textColor="#000000" />

     <TextView
         android:id="@+id/artist"
         style="@android:style/TextAppearance.Small"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:layout_below="@id/title"
         android:layout_toLeftOf="@+id/play"
         android:singleLine="true"
         android:text="artist"
         android:textColor="#7D7D7D" />

     <TextView
         android:id="@+id/duration"
         style="@android:style/TextAppearance.Small"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/artist"
         android:layout_alignParentBottom="true"
         android:layout_below="@id/artist"
         android:layout_toLeftOf="@+id/play"
         android:singleLine="true"
         android:text="03:24"
         android:textColor="#7D7D7D" />

        <ImageButton
            android:id="@+id/play"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_toLeftOf="@+id/download"
            android:src="@drawable/play" />

        <ImageButton
            android:id="@+id/download"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentRight="true"
            android:src="@drawable/download" />

 </RelativeLayout>

I can't find any way to get the "play" image button to react to a click, I have tried itemclicklistener, onclick attributes...

Can someone please provide me a sample code or working tutorial?

Also, the part of my java code is inside the postexecute of an AsynTask.

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
user3119384
  • 329
  • 4
  • 23
  • The onClick and itemClickListener would interact with the fragment. Once you have interaction with the fragment done, you would have to send data from the fragment to the parent activity. I assume that is what you are trying to do? http://developer.android.com/training/basics/fragments/communicating.html – elimirks Jan 03 '14 at 01:40
  • Thank you this helped me much, in fact the onClick xml attrbiture should be declared as a public Void inside the parent activity (mainactivity in my case) How could I mark your comment as best answer? – user3119384 Jan 03 '14 at 18:25
  • No problem. I didn't post my comment as an answer because I wasn't sure if that was what you were asking. I made it an answer now. – elimirks Jan 03 '14 at 19:24

3 Answers3

0

Not sure whats holding you back but setting an onClickListener always worked for me.

TwilightSun
  • 2,275
  • 2
  • 18
  • 27
0

You need to handle the play button in your adapter and have to add a property to play button android:clickable="true"

Nitin Misra
  • 4,472
  • 3
  • 34
  • 52
0

The onClick and itemClickListener interacts with the fragment. Once you have interaction with the fragment done, you must send data from the fragment to the parent activity using a listener class or something along those lines.

http://developer.android.com/training/basics/fragments/communicating.html

elimirks
  • 1,452
  • 2
  • 17
  • 30