请帮帮我..
好的,这是我的问题。我使用tabhost,我有3个标签。
歌曲名单,新歌和最喜欢的作品这是tabhost的代码..
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_fragment_one, container, false);
final TabHost host = (TabHost) rootView.findViewById(R.id.tabHost);
host.setup();
dbHelper = new FragmentOne_DbAdapter(getActivity());
dbHelper.open();
//Clean all data
//dbHelper.deleteAllPlayer1();
//Add some data
dbHelper.insertPlayer1Songlist();
//Generate ListView from SQLite Database
//Tab 1
TabHost.TabSpec spec = host.newTabSpec("SONG LIST");
spec.setContent(R.id.tab1);
spec.setIndicator("SONG LIST");
host.addTab(spec);
listView = (ListView) rootView.findViewById(R.id.slPlayer1ListView);
player1ESearch = (EditText) rootView.findViewById(R.id.player1Search);
ImageButton dplayer1ESearch=(ImageButton) rootView.findViewById(R.id.delete);
dplayer1ESearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
player1ESearch.setText("");
}
});
displayPlayer1ListView();
//Tab 2
spec = host.newTabSpec("NEW SONGS");
spec.setContent(R.id.tab2);
spec.setIndicator("NEW SONGS");
host.addTab(spec);
listViewNew = (ListView) rootView.findViewById(R.id.slPlayer1NewListView);
displayPlayer1NewListView();
//Tab 3
spec = host.newTabSpec("FAVORITES");
spec.setContent(R.id.tab3);
spec.setIndicator("FAVORITES");
host.addTab(spec);
return rootView;
}
在我的标签 SONG LIST 中,我显示数据库中的列表并将其设置为适配器..
在这部分代码中..
private void displayPlayer1ListView() {
Cursor cursor = dbHelper.fetchAllPlayer1();
FragmentOneAdapter = new FragmentOne_Adapter(getActivity(), cursor, 0);
listView.setAdapter(FragmentOneAdapter);
player1ESearch.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
FragmentOneAdapter.getFilter().filter(s.toString());
}
});
FragmentOneAdapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return dbHelper.fetchPlayer1ByTitle(constraint.toString());
}
});
}
这是我的适配器bindview ..
@Override
public void bindView(View view, final Context context, final Cursor cursor) {
// TODO Auto-generated method stub
final ViewHolder holder = (ViewHolder) view.getTag();
final int _id = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
String title = cursor.getString(cursor.getColumnIndexOrThrow("title"));
String artist = cursor.getString(cursor.getColumnIndexOrThrow("artist"));
String volume = cursor.getString(cursor.getColumnIndexOrThrow("volume"));
String type = cursor.getString(cursor.getColumnIndexOrThrow("type"));
final String favorite = cursor.getString(cursor.getColumnIndexOrThrow("favorite"));
String number = cursor.getString(cursor.getColumnIndexOrThrow("number"));
// Populate fields with extracted properties
holder.txtTitle.setText(title);
holder.txtArtist.setText(artist);
holder.txtVolume.setText(volume);
holder.txtNumber.setText(number);
if (type.matches("1")) {
holder.buttonType.setImageResource(R.drawable.newsong);
} else if (type.matches("2")) {
holder.buttonType.setImageResource(R.drawable.practice);
} else if (type.matches("0")) {
holder.buttonType.setImageResource(R.drawable.none);
}
if (favorite.matches("0")) {
holder.buttonHeart.setImageResource(R.drawable.heart);
} else {
if (favorite.matches("1")) {
holder.buttonHeart.setImageResource(R.drawable.heartred);
} else {
holder.buttonHeart.setImageResource(R.drawable.heart);
}
}
holder.buttonHeart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (arg0 != null) {
FragmentOne_DbAdapter database = new FragmentOne_DbAdapter(context);
database.open();
if (favorite.matches("0")) {
database.updateItemFavorite(_id, "1");
holder.buttonHeart.setImageResource(R.drawable.heartred);
} else if (favorite.matches("1")) {
database.updateItemFavorite(_id, "0");
holder.buttonHeart.setImageResource(R.drawable.heart);
}
}
FragmentOne_Adapter.this.changeCursor(dbHelper.fetchAllPlayer1());
}
});
}
正如你在我的适配器中看到的那样,我有 database.updateItemFavorite ,在这一部分中,这是我在数据库中更新字段的地方,如果它是喜欢它将设置为1并且imageview资源将是heartred。
现在我的第二个是 NEW SONGS
在第二个标签中,我在代码的这一部分显示新歌..
private void displayPlayer1NewListView() {
Cursor cursor = dbHelper.fetchAllPlayer1New();
FragmentOneAdapterNew = new FragmentOne_AdapterNew(getActivity(), cursor, 0);
listViewNew.setAdapter(FragmentOneAdapterNew);
}
这是我新歌的bindview ..
@Override
public void bindView(View view, final Context context, final Cursor cursor) {
// TODO Auto-generated method stub
final ViewHolder holder = (ViewHolder) view.getTag();
final int _id = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
String title = cursor.getString(cursor.getColumnIndexOrThrow("title"));
String artist = cursor.getString(cursor.getColumnIndexOrThrow("artist"));
String volume = cursor.getString(cursor.getColumnIndexOrThrow("volume"));
String type = cursor.getString(cursor.getColumnIndexOrThrow("type"));
final String favorite = cursor.getString(cursor.getColumnIndexOrThrow("favorite"));
String number = cursor.getString(cursor.getColumnIndexOrThrow("number"));
// Populate fields with extracted properties
holder.txtTitle.setText(title);
holder.txtArtist.setText(artist);
holder.txtVolume.setText(volume);
holder.txtNumber.setText(number);
if (type.matches("1")) {
holder.buttonType.setImageResource(R.drawable.newsong);
} else if (type.matches("2")) {
holder.buttonType.setImageResource(R.drawable.practice);
} else if (type.matches("0")) {
holder.buttonType.setImageResource(R.drawable.none);
}
if (favorite.matches("0")) {
holder.buttonHeart.setImageResource(R.drawable.heart);
} else {
if (favorite.matches("1")) {
holder.buttonHeart.setImageResource(R.drawable.heartred);
} else {
holder.buttonHeart.setImageResource(R.drawable.heart);
}
}
holder.buttonHeart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (arg0 != null) {
FragmentOne_DbAdapter database = new FragmentOne_DbAdapter(context);
database.open();
if (favorite.matches("0")) {
database.updateItemFavorite(_id, "1");
holder.buttonHeart.setImageResource(R.drawable.heartred);
} else if (favorite.matches("1")) {
database.updateItemFavorite(_id, "0");
holder.buttonHeart.setImageResource(R.drawable.heart);
}
}
FragmentOne_AdapterNew.this.changeCursor(dbHelper.fetchAllPlayer1New());
}
});
}
在我的歌曲列表中,点击我喜欢的图像按钮后。它将我喜欢的字段更新为1,并将资源设置为热心。
现在我的问题是当我点击标签NEW SONGS。 SONG LIST的最新收藏也应出现在我的新歌列表中。
请帮帮我。
答案 0 :(得分:0)
在第二个标签页中,如果您使用片段,则新的歌曲覆盖此方法。
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser){
displayPlayer1NewListView() ;
}
}
当您的第二个标签片段在屏幕上可见并刷新此列表时,将调用此方法。
答案 1 :(得分:0)
对于那些遇到这个问题的人..我找到了答案..
host.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
int i = host.getCurrentTab();
if (i == 0) {
displayPlayer1ListView();
} else if (i == 1) {
displayPlayer1NewListView();
}
}
});