ListView
があります。そのすべての項目(つまりTextView
)には歌詞のテキストが入力されています。アイテムをクリックするたびに強調表示され、以前に選択されたアイテムの背景をデフォルトに変更する必要があります。このため、私は以下のようにListView.setOnItemClickListener
を使用:(ビュー)adapter.getChildAt(6); 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、から){ スーパー(コンテキストを拡張します、アイテム、リソース、from、to); } iは6項目を選択し、再度iはコード行に、第5の項目を選択すると、アイテムをリストする大きなフォントサイズを設定
@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;
}
}
:
oldSelectedView = (View)adapter.getChildAt(songNo);
oldSelectedView
はnull
として返されます。しかし、私は小さなフォントを使用すると、リストは1つの画面と6番目の項目のseletctionに収まるし、7番目または5番目の選択does't何か問題を作る。私はこの問題を解決してください
..
public class SpecialAdapter extends SimpleAdapter {public SpecialAdapter(Context context、items、int resource、String [] from、int [] to){スーパー(コンテキスト、アイテム、リソース、from、to); } @Override public View getView(int position、View convertView、ViewGroup parent){ ビュービュー= super.getView(position、convertView、parent); if(position == songNo) { view.setBackgroundColor(0xFFFFFFFF); } else view.setBackgroundColor(0x00000000); リターンビュー。 } } これはSpecialAdapterのコードです。私は問題を見つけることができません。 – Tahir
私の質問に明確にコードを見てください。 – Tahir
私の問題の解決には、私のリストadapter.notifyDataSetChanged() ! – Tahir