我有一個ListView
,其中的每個項目(即TextView
)都充滿了Song的歌詞文本。每當我點擊一個項目時,它應該被突出顯示,並且之前選擇的項目背景應該被更改爲默認值。爲此,我如下使用ListView.setOnItemClickListener
:;返回null
public int songNo;
public int totalSongs;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lyric_display);
songNo=0;
totalSongs=7;
ListView songLyricsListView = (ListView) findViewById(R.id.LyricDisplayListView);
MediaPlayer soundPlayer = new MediaPlayer();
songLyricsListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View selectedView, int pos,
long arg3) {
**//set background to older selection**
View oldSelectedView;
oldSelectedView = (View)adapter.getChildAt(songNo);
oldSelectedView.setBackgroundColor(0x00000000);
songNo = pos;
selectedView.setBackgroundColor(0xFFFFFFFF);
songLyricsListView.smoothScrollToPosition(songNo);
try {
soundPlayer.reset();
soundPlayer.setDataSource("/sdcard/0"+Integer.toString(songNo)+".mp3");
soundPlayer.prepare();
soundPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
String[] from = new String[] {"lyrics","translation"};
int[] to = new int[] {R.id.songTextView, R.id.translationTextView};
try {
InputStream is = getResources().openRawResource(R.raw.lyric_text);
BufferedInputStream bis = new BufferedInputStream(is);
int myInt=0;
for(int songno=0; songno<=totalSongs; songno++)
{
HashMap<String, String> map = new HashMap<String, String>();
ByteArrayBuffer myByteArray = new ByteArrayBuffer(100);
while((myInt = bis.read())!=-1)
{
if(myInt == 10) break;
myByteArray.append((byte)myInt);
}
String lyricsText = new String(myByteArray.toByteArray(), 0, myByteArray.length(), "UTF-8");
myByteArray.clear();
map.put("lyrics",lyricsText);
map.put("translation", Integer.toString(songno)+"- sample translation");
list.add((HashMap<String, String>)map);
}
bis.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
adapter = new SpecialAdapter(
this,
list,
R.layout.lyrics_display_item,
from,
to);
songLyricsListView.setAdapter(adapter);
公共類SpecialAdapter延伸SimpleAdapter { 公共SpecialAdapter(上下文上下文中,列表>項,INT資源,字符串[]從,INT []到){ 超級(上下文,項目,資源,從,到); }
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if(position == songNo)
{
view.setBackgroundColor(0xFFFFFFFF);
}else
view.setBackgroundColor(0x00000000);
return view;
}
}
當我設置更大的字體大小,列出項目,當我選擇第6項,我再次選擇第五項,代碼行:
oldSelectedView = (View)adapter.getChildAt(songNo);
oldSelectedView
返回爲null
。但是,當我使用小字體,列表適合一個屏幕和第六項seletction,然後第七或第五選擇沒有任何問題。
請幫我解決這個問題..
public class SpecialAdapter extends SimpleAdapter {public SpecialAdapter(Context context,List> items,int resource,String [] from,int [] to){super(context,items,resource,from,to); } @Override public View getView(int position,View convertView,ViewGroup parent){ View view = super.getView(position,convertView,parent); if(position == songNo) { view.setBackgroundColor(0xFFFFFFFF); } else view.setBackgroundColor(0x00000000); 返回視圖; }} 這 是SpecialAdapter代碼,請查看它,我無法找到任何問題 – Tahir 2012-02-07 17:12:49
,請在我的問題編輯清楚地看到代碼.. – Tahir 2012-02-07 17:14:33
我的名單中onItemClickListner()adapter.notifyDataSetChanged()解決了這個問題對我來說! – Tahir 2012-02-07 18:32:26