Android - 收听图像按钮单击里面的listview里面的片段

时间:2014-01-03 01:36:20

标签: java android listview android-fragments imagebutton

我正在开发一个4标签应用程序。

我的一个片段是搜索片段,其中包含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 );

这是我的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>

我找不到任何方法让“播放”图像按钮对点击作出反应,我尝试过itemclicklistener,onclick属性......

有人可以提供示例代码或工作教程吗?

此外,我的java代码部分位于AsynTask的postexecute中。

3 个答案:

答案 0 :(得分:0)

不确定什么会阻止你,但设置onClickListener总是对我有效。

答案 1 :(得分:0)

您需要处理适配器中的play按钮,并且必须添加属性以播放按钮android:clickable="true"

答案 2 :(得分:0)

onClick和itemClickListener与片段进行交互。与片段完成交互后,必须使用侦听器类或其他内容将片段中的数据发送到父活动。

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