2013-03-01 56 views
0

我有一些麻烦,在一个ListView正确识别项目的项目。机器人 - 确定在ListView

有4类,没关系,这是一个很大的代码,以便在第一,我要解释这些类的逻辑。

  • 输入ListActivity和初始化其ListView
  • 执行AsyncTask其从服务器下载JSON响应,解析它,使用对象填充ListView和设置在适配器而呈现出ProgressDialog
  • PlaylistItem类包含从单个JSONObject获取数据的方法。它是用来参数以及它的目的ArrayList的
    • AsyncTask后完成列表填充项,看起来像|按钮|艺术家(TextView) - 标题(TextView

UPDATE

解决第1期,但仍无法找出什么地方错了按钮

2)。我在适配器的getView()方法中为我的按钮设置了一个OnClickListener。要找出按钮是否被正确识别,我什么也没做,只是改变了它的背景。但在某个按钮点击迫使每11或12按钮的背景改变。到目前为止无法弄清楚。

我无法继续获得URL和流音频,直到这些问题都解决了,所以任何帮助是极大的赞赏。我的课程在下面,请问是否看起来不清楚。

AudioList

  public class AudioList extends ListActivity { 
private ListView lv; 
private PlaylistLoader loader; 
private AudioListAdapter adapter; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_audio_list); 
    init(); // initialize the ListView 

    /*--- populate the list with user's audio in case network connection is available ---*/ 
    loader = new PlaylistLoader(this, lv, adapter); 
    if (Utils.isNetworkAvailable(this)) { 
     loader.execute(); 
    } else { 
     APP_CONSTANTS.NO_DATA_CONNECTION(this); 
    } 

} 

@Override 
protected void onResume() { 
    super.onResume(); 
    lv.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 

      Toast.makeText(getApplicationContext(), Integer.toString(arg2), 
        Toast.LENGTH_SHORT).show(); 
      } 
    }); 

} 

private void init() { 
    lv = getListView(); 
    lv.setTranscriptMode(0x00000000); 
    lv.setDividerHeight(1); 
    lv.setSmoothScrollbarEnabled(true); 
    lv.setVerticalFadingEdgeEnabled(true); 

} 

PlaylistLoader

 public class PlaylistLoader extends AsyncTask<Void, Void, Void> { 

private JSONObject usersPlaylist, singleJSONItem; 
private JSONArray responseJSONArray; 
private ListView lv; 
private ArrayList<PlaylistItem> playlist; 
private Activity a; 
private PlaylistItem audioList; 
private SharedPreferences prefs; 
private ProgressDialog pd; 
AudioListAdapter adapter; 

public PlaylistLoader(Activity a, ListView lv, AudioListAdapter adapter) { 
    this.lv = lv; 
    this.a = a; 
    this.adapter = adapter; 
} 

@Override 
protected Void doInBackground(Void... arg0) { 
    /*--- create new ArrayList of PlaylistItem Objects ---*/ 
    playlist = new ArrayList<PlaylistItem>(); 
    /*--- get the preferences using context of calling activity ---*/ 
    prefs = PreferenceManager.getDefaultSharedPreferences(a); 
    try { 
     /*--- download the response JSONObject from server // access_token and 
     * user_id come from activity's defaultSharedPreferences ---*/ 
     usersPlaylist = Utils.retrieveJsonObjectFromUrl(new URL(
       APP_CONSTANTS.REQUEST_AUDIO_LIST(prefs)), a); 
     /*--- get the response array from received object ---*/ 
     responseJSONArray = usersPlaylist.getJSONArray("response"); 
     /*--- populate the ArrayList with Objects from the response array ---*/ 
     for (int i = 0; i < responseJSONArray.length(); i++) { 
      singleJSONItem = responseJSONArray.getJSONObject(i); 
      audioList = new PlaylistItem(singleJSONItem); 
      playlist.add(audioList); 

     } 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    pd = new ProgressDialog(a); 
    pd.setTitle("Please wait"); 
    pd.setMessage("Retrieving audio list..."); 
    pd.show(); 

} 

@Override 
protected void onPostExecute(Void result) { 
    super.onPostExecute(result); 
    lv.setVisibility(View.VISIBLE); 
    pd.dismiss(); 
    /*--- set the adapter passed in constructor as an adapter for passed ListView ---*/ 
    adapter = new AudioListAdapter(a, R.layout.playlist_item, playlist); 
    lv.setAdapter(adapter); 

} 
    } 

AudioListAdapter

  public class AudioListAdapter extends ArrayAdapter<PlaylistItem> { 
private PlaylistItem pl; 
private Context context; 
private int layoutResourceId; 
private PlaylistItem aud; 
private ArrayList<PlaylistItem> data = null; 

public AudioListAdapter(Context context, int layoutResourceId, 
     ArrayList<PlaylistItem> data) { 
    super(context, layoutResourceId, data); 
    this.layoutResourceId = layoutResourceId; 
    this.context = context; 
    this.data = data; 

} 

@Override 
public PlaylistItem getItem(int position) { 
    return super.getItem(position); 
} 

@Override 
public int getCount() { 
    return data.size(); 
} 

@Override 
public int getPosition(PlaylistItem item) { 
    return super.getPosition(item); 
} 

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

    pl = new PlaylistItem(); 
    aud = getItem(position); 

    if (convertView == null) { 
     LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
     convertView = inflater.inflate(layoutResourceId, parent, false); 
     pl.btnPlay = (Button) convertView.findViewById(R.id.btn_list_play); 
     pl.imgSaved = (ImageView) convertView 
       .findViewById(R.id.img_list_audio_saved); 
     pl.tvArtist = (TextView) convertView 
       .findViewById(R.id.tvListItemArtist); 
     pl.tvTitle = (TextView) convertView 
       .findViewById(R.id.tvListItemSong); 

     convertView.setTag(pl); 
    } else { 
     pl = (PlaylistItem) convertView.getTag(); 
     pl.btnPlay.setBackgroundResource(R.drawable.list_button_play); 
    } 

    pl.tvArtist.setText(aud.getArtist() + " " + "-"); 
    pl.tvTitle.setText(aud.getTitle()); 

    pl.btnPlay.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      /*--- vibrate if this option is enabled in the preferences ---*/ 
      if (APP_CONSTANTS.isHapticFeedbackEnabled(getContext())) { 
       APP_CONSTANTS.doVibrate(getContext()); 

      } 
     pl.btnPlay.setBackgroundResource(R.drawable.list_button_pause); 

     } 
    }); 

    return convertView; 
} 

PlayListItem

 public class PlaylistItem { 

private String artist, title; 
private JSONObject obj; 
public Button btnPlay; 
public TextView tvArtist, tvTitle; 
public ImageView imgSaved; 
public int duration; 
public int audio_id; 
public String url; 

/*--- the constructor takes a single JSONObject from the response array ---*/ 
public PlaylistItem(JSONObject obj) { 
    this.obj = obj; 
} 

public PlaylistItem() { 
    // default constructor 
} 

/*--- the methods below return values by key from the passed JSONObject ---*/ 

public String getArtist() { 
    try { 
     artist = obj.getString("artist"); 
    } catch (JSONException e) { 

     e.printStackTrace(); 
    } 
    return artist; 

} 

public String getTitle() { 
    try { 
     title = obj.getString("title"); 
    } catch (JSONException e) { 

     e.printStackTrace(); 
    } 
    return title; 
} 

public int getID() { 
    try { 
     audio_id = obj.getInt("aid"); 
    } catch (JSONException e) { 

     e.printStackTrace(); 
    } 
    return audio_id; 
} 

public String getURL() { 
    try { 
     url = obj.getString("url"); 
    } catch (JSONException e) { 

     e.printStackTrace(); 
    } 
    return url; 
} 
    } 
+0

1)您是否对此进行过任何研究......只有在ListView行中有Clickable元素时才会发生......问了很多次...... 2)询问了多次太... ListView是一个智能控制...你必须了解它... http://www.youtube.com/watch?v=wDBM6wVEO70(如果你在'getView'中得到'convertView' - 你会有一个答案......)...无论如何...问题很好写:) – Selvin 2013-03-01 15:28:19

+0

是的,我做到了。我看了这个视频。仍然找不到任何解决方案,将在我的情况下工作 – Droidman 2013-03-01 15:31:57

+0

heh存储播放列表状态(暂停/播放)PlaylistItem对象然后设置此drawable取决于此状态在getView ...和onClick只设置此状态在选定PlayListItem – Selvin 2013-03-01 15:34:22

回答

1

编辑

试试这个

以自定义选择在绘制 button_play。XML

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/pause_button" 
      android:state_selected="true" /> 
    <item android:drawable="@drawable/play_button" /> 
</selector> 

Modifty适配器这样

public class AudioListAdapter extends ArrayAdapter<PlaylistItem> { 
private PlaylistItem pl; 
private Context context; 
private int layoutResourceId; 
private PlaylistItem aud; 
private ArrayList<PlaylistItem> data = null; 
Button previous; 

public AudioListAdapter(Context context, int layoutResourceId, 
     ArrayList<PlaylistItem> data) { 
    super(context, layoutResourceId, data); 
    this.layoutResourceId = layoutResourceId; 
    previous=new Button(context); 
    this.context = context; 
    this.data = data; 

} 

.... 
.... 

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

    pl = new PlaylistItem(); 
    aud = getItem(position); 

    if (convertView == null) { 
     LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
     convertView = inflater.inflate(layoutResourceId, parent, false); 
     pl.btnPlay = (Button) convertView.findViewById(R.id.btn_list_play); 
pl.btnPlay.setBackGroundResouce(R.drawable.button_play); //you can set here or in xml 

     pl.imgSaved = (ImageView) convertView 
       .findViewById(R.id.img_list_audio_saved); 
     pl.tvArtist = (TextView) convertView 
       .findViewById(R.id.tvListItemArtist); 
     pl.tvTitle = (TextView) convertView 
       .findViewById(R.id.tvListItemSong); 

     convertView.setTag(pl); 
    } else { 
     pl = (PlaylistItem) convertView.getTag(); 
    } 


    pl.tvArtist.setText(aud.getArtist() + " " + "-"); 
    pl.tvTitle.setText(aud.getTitle()); 

    pl.btnPlay.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      /*--- vibrate if this option is enabled in the preferences ---*/ 
      if (APP_CONSTANTS.isHapticFeedbackEnabled(getContext())) { 
       APP_CONSTANTS.doVibrate(getContext()); 
      } 
       //for some reason, the background gets changed for every 11th or 12th button in the list 
      Button current=((Button)v); 
       current.setSelected(true); 
       previous.setSelected(false); 
       previous=current; 
     } 
    }); 

    return convertView; 
} 

    } 

之所以你的按钮,列表项无法点击,因为你的名单有一个重点项目按钮,所以你需要setFocusable为= FALSE你的按钮。 尝试在xml中为您的按钮设置focusable = false。如果它不为你工作不是做这样的

在你行的xml文件

1.设置可聚焦=真为你的按钮。 2.在同一组android:descendantFocusability="blocksDescendants"为您的父项目(即您的意见所在的父布局)。

在为按钮设置onclickListener之后,在getView()方法中,为按钮设置focusable false。 它肯定会有效。我希望这会帮助你..

+0

它在行上崩溃previous.setSelected(false);我想这会发生'因为按钮没有被初始化 – Droidman 2013-03-01 17:55:56

+0

你必须初始化它int构造函数... previous = new Button(context); – Pragnani 2013-03-02 04:48:15

+0

post statck trace .. – Pragnani 2013-03-02 05:29:20

1

但是点击某个按钮强制每改变第11或第12个按钮的背景。到目前为止无法弄清楚。

你在与ListViews回收行布局的方式作斗争。
可以这样想:如果你有一个有10000行的ListView,但只能在屏幕上显示9个ListView,那么创建10,000个独特的布局是没有意义的。这只是浪费资源,而不是ListView创建〜10个布局并重用它们。

解决方案:将每行恢复为重用时的默认状态。在getView()添加:

} else { 
    pl = (PlaylistItem) convertView.getTag(); 
    pl.btnPlay.setBackgroundResource(R.drawable.list_button_play); 
    // I guessed at the resource's name   ^^^^^^^^^^^^^^^^ 
} 

(你也可以做一些小的改动,以加快你的代码举例来说,你只需要一个OnClickListener,因为它们都含有相同的代码,使之成为一个类变量,并通过这一点。 )

+0

只是补充说。现在列表的行为非常奇怪,当我点击一个按钮时,它会改变列表中其他按钮的背景,或根本没有任何操作。我想我需要一种方法将每个按钮连接到相应位置的PlaylistItem对象。由于按钮点击应该导致从相应的PlaylistItem对象获取URL并开始播放。当没有其他按钮被点击时,即使用户向上或向下滚动,当前按钮也应该具有可绘制的暂停。 – Droidman 2013-03-01 20:38:52

+0

请用'getView()'中的当前代码更新您的问题,以便我可以看到发生了什么。 – Sam 2013-03-01 20:41:40

+0

这个截图显示了我需要的几乎相同的东西http://cs407217.vk.me/v407217837/7b9b/H9d7TKat74M.jpg – Droidman 2013-03-01 20:41:50