【问题标题】:Adding Audio functionality in listview在列表视图中添加音频功能
【发布时间】:2015-04-21 07:26:09
【问题描述】:

我添加了一个自定义的ListView,它带有一个可以在每一行播放不同音频的按钮。这是我为它编写的代码。但它不能正常工作。 Onclicklistener 不工作。请帮忙!

CustomListAdapter.class

public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<String> data;
private ArrayList<Bitmap> image_bit;
private static ArrayList<String> audio_bit;
private MediaPlayer player;

private static LayoutInflater layoutInflater;

public CustomListAdapter(Activity acivity, ArrayList<String> data,ArrayList<Bitmap> image_bit,ArrayList<String>audio) {
    // TODO Auto-generated constructor stub
    this.activity = acivity;
    this.data = data;
    this.image_bit=image_bit;
    this.audio_bit=audio;
    layoutInflater = (LayoutInflater) acivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return data.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView( final int position, View convertView, ViewGroup parent) {

    ListCell listCell;
    if(convertView == null)
    {
        convertView = layoutInflater.inflate(R.layout.listact_kitchen, null);
        listCell = new ListCell();
        listCell.tvStep = (TextView) convertView.findViewById(R.id.tvStep);
        listCell.list_image=(ImageView)convertView.findViewById(R.id.list_image);
        listCell.imageButton1=(Button)convertView.findViewById(R.id.imageButton1);
        convertView.setTag(listCell);
    }
    else
    {
        listCell = (ListCell) convertView.getTag();
    }

    try
    {
        listCell.tvStep.setText(data.get(position));
        listCell.list_image.setImageBitmap(image_bit.get(position));
        listCell.imageButton1.setOnClickListener(new OnClickListener() {


            public void onClick(View v) {
                // TODO Auto-generated method stub
                player=new MediaPlayer();
                     try {
                        player.setDataSource(audio_bit.get(position));
                        player.prepare();
                        player.start();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 


            }
        });


    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    // TODO Auto-generated method stub
    return convertView;
}

private class ListCell
{
    TextView tvStep;
    ImageView list_image;
    Button imageButton1;
}

}

这是最新的logcat

04-22 12:06:35.618: I/Process(5129): Sending signal. PID: 5129 SIG: 9
04-22 12:06:35.998: D/PackageItemInfo(6020): loadIcon(cached)name=org.example.beproj.MainActivity
04-22 12:06:40.553: D/dalvikvm(6020): GC_FOR_ALLOC freed 390K, 42% free 9839K/16688K, paused 24ms, total 31ms
04-22 12:06:40.573: I/dalvikvm-heap(6020): Grow heap (frag case) to 19.715MB for 8294416-byte allocation
04-22 12:06:40.593: D/dalvikvm(6020): GC_CONCURRENT freed 18K, 28% free 17921K/24792K, paused 6ms+2ms, total 21ms
04-22 12:06:40.803: D/dalvikvm(6020): GC_FOR_ALLOC freed 34K, 28% free 17921K/24792K, paused 21ms, total 21ms
04-22 12:06:40.823: I/dalvikvm-heap(6020): Grow heap (frag case) to 27.608MB for 8294416-byte allocation
04-22 12:06:40.843: D/dalvikvm(6020): GC_CONCURRENT freed <1K, 21% free 26021K/32896K, paused 3ms+3ms, total 24ms
04-22 12:06:41.074: D/dalvikvm(6020): GC_FOR_ALLOC freed 33K, 21% free 26022K/32896K, paused 17ms, total 17ms
04-22 12:06:41.084: I/dalvikvm-heap(6020): Grow heap (frag case) to 35.519MB for 8294416-byte allocation
04-22 12:06:41.134: D/dalvikvm(6020): GC_CONCURRENT freed <1K, 17% free 34122K/41000K, paused 4ms+9ms, total 42ms
04-22 12:06:41.414: D/dalvikvm(6020): GC_FOR_ALLOC freed 32K, 17% free 34122K/41000K, paused 16ms, total 16ms
04-22 12:06:41.424: I/dalvikvm-heap(6020): Grow heap (frag case) to 43.429MB for 8294416-byte allocation
04-22 12:06:41.454: D/dalvikvm(6020): GC_CONCURRENT freed <1K, 15% free 42222K/49104K, paused 3ms+3ms, total 24ms
04-22 12:06:41.644: D/dalvikvm(6020): GC_FOR_ALLOC freed 33K, 15% free 42222K/49104K, paused 15ms, total 16ms
04-22 12:06:41.664: I/dalvikvm-heap(6020): Grow heap (frag case) to 51.339MB for 8294416-byte allocation
04-22 12:06:41.704: D/dalvikvm(6020): GC_CONCURRENT freed <1K, 13% free 50322K/57208K, paused 3ms+12ms, total 40ms
04-22 12:06:41.915: D/dalvikvm(6020): GC_FOR_ALLOC freed 34K, 13% free 50323K/57208K, paused 19ms, total 19ms
04-22 12:06:41.925: I/dalvikvm-heap(6020): Grow heap (frag case) to 59.250MB for 8294416-byte allocation
04-22 12:06:41.965: D/dalvikvm(6020): GC_CONCURRENT freed <1K, 11% free 58423K/65312K, paused 3ms+4ms, total 36ms
04-22 12:06:42.195: D/dalvikvm(6020): GC_FOR_ALLOC freed 32K, 11% free 58423K/65312K, paused 18ms, total 18ms
04-22 12:06:42.225: I/dalvikvm-heap(6020): Grow heap (frag case) to 67.161MB for 8294416-byte allocation
04-22 12:06:42.285: D/dalvikvm(6020): GC_CONCURRENT freed <1K, 10% free 66523K/73416K, paused 3ms+21ms, total 62ms
04-22 12:06:42.485: D/dalvikvm(6020): GC_FOR_ALLOC freed 34K, 10% free 66524K/73416K, paused 16ms, total 16ms
04-22 12:06:42.515: I/dalvikvm-heap(6020): Grow heap (frag case) to 75.072MB for 8294416-byte allocation
04-22 12:06:42.575: D/dalvikvm(6020): GC_CONCURRENT freed <1K, 9% free 74624K/81520K, paused 5ms+4ms, total 59ms
04-22 12:06:42.785: D/dalvikvm(6020): GC_FOR_ALLOC freed 32K, 9% free 74624K/81520K, paused 26ms, total 26ms
04-22 12:06:42.836: I/dalvikvm-heap(6020): Grow heap (frag case) to 82.982MB for 8294416-byte allocation
04-22 12:06:42.886: D/dalvikvm(6020): GC_CONCURRENT freed <1K, 8% free 82724K/89624K, paused 5ms+20ms, total 46ms
04-22 12:06:43.066: D/dalvikvm(6020): GC_FOR_ALLOC freed 33K, 8% free 82724K/89624K, paused 19ms, total 19ms
04-22 12:06:43.126: I/dalvikvm-heap(6020): Grow heap (frag case) to 90.892MB for 8294416-byte allocation
04-22 12:06:43.176: I/dalvikvm-heap(6020): Clamp target GC heap from 96.892MB to 96.000MB
04-22 12:06:43.176: D/dalvikvm(6020): GC_CONCURRENT freed <1K, 8% free 90824K/97728K, paused 4ms+6ms, total 55ms
04-22 12:06:43.356: I/dalvikvm-heap(6020): Clamp target GC heap from 96.893MB to 96.000MB
04-22 12:06:43.356: D/dalvikvm(6020): GC_FOR_ALLOC freed 33K, 8% free 90825K/97728K, paused 20ms, total 20ms
04-22 12:06:43.356: I/dalvikvm-heap(6020): Forcing collection of SoftReferences for 8294416-byte allocation
04-22 12:06:43.386: I/dalvikvm-heap(6020): Clamp target GC heap from 96.885MB to 96.000MB
04-22 12:06:43.386: D/dalvikvm(6020): GC_BEFORE_OOM freed 9K, 8% free 90815K/97728K, paused 34ms, total 34ms
04-22 12:06:43.386: E/dalvikvm-heap(6020): Out of memory on a 8294416-byte allocation.
04-22 12:06:43.386: I/dalvikvm(6020): "main" prio=5 tid=1 RUNNABLE
04-22 12:06:43.386: I/dalvikvm(6020):   | group="main" sCount=0 dsCount=0 obj=0x41277b50 self=0x412671d0
04-22 12:06:43.386: I/dalvikvm(6020):   | sysTid=6020 nice=0 sched=0/0 cgrp=apps handle=1075481820
04-22 12:06:43.386: I/dalvikvm(6020):   | state=R schedstat=( 0 0 0 ) utm=215 stm=39 core=0
04-22 12:06:43.386: I/dalvikvm(6020):   at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
04-22 12:06:43.386: I/dalvikvm(6020):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:529)
04-22 12:06:43.386: I/dalvikvm(6020):   at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:302)
04-22 12:06:43.396: I/dalvikvm(6020):   at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:328)
04-22 12:06:43.396: I/dalvikvm(6020):   at org.example.beproj.change.ImgBitmap(change.java:97)
04-22 12:06:43.396: I/dalvikvm(6020):   at org.example.beproj.change.onCreate(change.java:55)
04-22 12:06:43.396: I/dalvikvm(6020):   at android.app.Activity.performCreate(Activity.java:5117)
04-22 12:06:43.396: I/dalvikvm(6020):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
04-22 12:06:43.396: I/dalvikvm(6020):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
04-22 12:06:43.396: I/dalvikvm(6020):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236)
04-22 12:06:43.396: I/dalvikvm(6020):   at android.app.ActivityThread.access$600(ActivityThread.java:145)
04-22 12:06:43.396: I/dalvikvm(6020):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
04-22 12:06:43.396: I/dalvikvm(6020):   at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 12:06:43.396: I/dalvikvm(6020):   at android.os.Looper.loop(Looper.java:137)
04-22 12:06:43.396: I/dalvikvm(6020):   at android.app.ActivityThread.main(ActivityThread.java:5099)
04-22 12:06:43.396: I/dalvikvm(6020):   at java.lang.reflect.Method.invokeNative(Native Method)
04-22 12:06:43.396: I/dalvikvm(6020):   at java.lang.reflect.Method.invoke(Method.java:511)
04-22 12:06:43.396: I/dalvikvm(6020):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)
04-22 12:06:43.396: I/dalvikvm(6020):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570)
04-22 12:06:43.396: I/dalvikvm(6020):   at dalvik.system.NativeStart.main(Native Method)
04-22 12:06:43.396: D/skia(6020): --- decoder->decode returned false
04-22 12:06:43.396: D/AndroidRuntime(6020): Shutting down VM
04-22 12:06:43.396: W/dalvikvm(6020): threadid=1: thread exiting with uncaught exception (group=0x41276ae0)
04-22 12:06:43.406: E/AndroidRuntime(6020): FATAL EXCEPTION: main
04-22 12:06:43.406: E/AndroidRuntime(6020): java.lang.OutOfMemoryError
04-22 12:06:43.406: E/AndroidRuntime(6020):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
04-22 12:06:43.406: E/AndroidRuntime(6020):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:529)
04-22 12:06:43.406: E/AndroidRuntime(6020):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:302)
04-22 12:06:43.406: E/AndroidRuntime(6020):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:328)
04-22 12:06:43.406: E/AndroidRuntime(6020):     at org.example.beproj.change.ImgBitmap(change.java:97)
04-22 12:06:43.406: E/AndroidRuntime(6020):     at org.example.beproj.change.onCreate(change.java:55)
04-22 12:06:43.406: E/AndroidRuntime(6020):     at android.app.Activity.performCreate(Activity.java:5117)
04-22 12:06:43.406: E/AndroidRuntime(6020):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
04-22 12:06:43.406: E/AndroidRuntime(6020):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
04-22 12:06:43.406: E/AndroidRuntime(6020):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236)
04-22 12:06:43.406: E/AndroidRuntime(6020):     at android.app.ActivityThread.access$600(ActivityThread.java:145)
04-22 12:06:43.406: E/AndroidRuntime(6020):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
04-22 12:06:43.406: E/AndroidRuntime(6020):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 12:06:43.406: E/AndroidRuntime(6020):     at android.os.Looper.loop(Looper.java:137)
04-22 12:06:43.406: E/AndroidRuntime(6020):     at android.app.ActivityThread.main(ActivityThread.java:5099)
04-22 12:06:43.406: E/AndroidRuntime(6020):     at java.lang.reflect.Method.invokeNative(Native Method)
04-22 12:06:43.406: E/AndroidRuntime(6020):     at java.lang.reflect.Method.invoke(Method.java:511)
04-22 12:06:43.406: E/AndroidRuntime(6020):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)
04-22 12:06:43.406: E/AndroidRuntime(6020):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570)
04-22 12:06:43.406: E/AndroidRuntime(6020):     at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • 你有什么异常吗?你能发布 logcat 的输出吗?
  • 您面临的问题是什么?
  • 首先确保您的 onClick() 方法被调用。在那里敬酒/记录以确保。而且“工作不正常”也不是解决问题的方法。你需要说什么是行不通的。
  • 它有例外吗?或不?请提供有关“不工作”的更多信息
  • 将 android:focusableInTouchMode="false" 按钮放入 xml 文件中。

标签: android android-listview android-sqlite


【解决方案1】:

目前的问题可能是你不会在 onclicklistener 中获得位置。

将imageButton1标签中的位置设置为

listCell.imageButton1.setTag (position);

在点击监听器内部检索它为

int position = Integer.valueOf(v.getTag());

所以你的 onclicklistener 看起来像这样

listCell.imageButton1.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
         int position = Integer.valueOf(v.getTag());
         player=new MediaPlayer();
         try {
               player.setDataSource(audio_bit.get(position));
               player.prepare();
               player.start();
         } catch (Exception e) {
             e.printStackTrace();
         } 
    }
 });

【讨论】:

  • 我已经放了logcat
猜你喜欢
  • 2018-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
  • 2021-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多