我正在開發應用程序,該應用程序應該顯示會議的會話列表,而另一個列表僅包含會議的文件。鏡像複選框與Android的不同片段狀態
我貼幾張截圖這樣一個問題:Save checkboxes states through fragments in Android
我的目標是能夠也«鏡»的複選框狀態在兩個列表,這樣,如果紙張在會談名單核對,它在論文列表中被檢查。
你有什麼想法我可以做到這一點?
該列表是使用兩個片段與自己的適配器實現的。
這裏是我的代碼:
主要活動:
package be.unamur.confpers;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
SqlHandler sqlHandler;
// Tab titles
private String[] tabs = { "Talks", "Papers", "Ma sélection"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialization database
sqlHandler = new SqlHandler(this);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
TalksFragment:
public class TalksFragment extends Fragment {
private TalkAdapter talkAdapter;
List<Talk> talks;
private List<Talk> talksParser(){
try{
XMLParser parser = new XMLParser();
talks = parser.parse(getActivity().getApplicationContext().getAssets().open("talks.xml"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return talks;
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_talks, container, false);
ExpandableListView lv = (ExpandableListView) v.findViewById(R.id.listTalks);
talks = talksParser();
talkAdapter = new TalkAdapter (getActivity(),talks);
lv.setAdapter(talkAdapter);
return v;
}
}
TalkAdapter:
public class TalkAdapter extends BaseExpandableListAdapter {
private Context context;
private List<Talk> talks;
private LayoutInflater inflater;
private Button changeScreen;
int checked = 0;
CheckBox cb;
public TalkAdapter(Context context,
List<Talk> talks) {
this.context = context;
this.talks = talks;
inflater = LayoutInflater.from(context);
}
public Paper getChild(int groupPosition, int childPosition) {
return talks.get(groupPosition).getPapers().get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return (long)(groupPosition*1024+childPosition); // Max 1024 children per group
}
private class ViewHolder {
TextView title;
TextView author;
CheckBox name;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View v = null;
ViewHolder holder = null;
if(convertView == null){
v = inflater.inflate(R.layout.child_row, parent, false);
Paper p = getChild(groupPosition, childPosition);
cb = (CheckBox)v.findViewById(R.id.check1);
//cb.setChecked(p.getState());
TextView paper = (TextView)v.findViewById(R.id.papername);
if(paper != null)
paper.setText(p.getTitle());
TextView author = (TextView)v.findViewById(R.id.authorname);
if(author!= null)
author.setText(p.getAuthor());
holder = new ViewHolder();
holder.name = (CheckBox) v.findViewById(R.id.check1);
holder.title = (TextView) v.findViewById(R.id.papername);
holder.author= (TextView) v.findViewById(R.id.authorname);
v.setTag(holder);
holder.name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) v;
Paper paper = (Paper) cb.getTag();
String title = paper.getTitle();
String author = paper.getAuthor();
if (cb.isChecked()){
Toast.makeText(context, "Papier "+title+ " ajouté", Toast.LENGTH_SHORT).show();
//String query = "INSERT INTO SELECTED_PAPERS(title,author) values ('"
//+ title +"','" + author +"')";
//sqlHandler.executeQuery(query);
}
if (cb.isChecked()==false){
Toast.makeText(context, "Papier "+title+ " retiré", Toast.LENGTH_SHORT).show();
}
}
});
}
else{
v = convertView;
holder = (ViewHolder) v.getTag();
}
Paper paper = getChild(groupPosition, childPosition);
holder.author.setText(paper.getAuthor());
holder.title.setText(paper.getTitle());
holder.name.setTag(paper);
return v;
}
public int getChildrenCount(int groupPosition) {
return talks.get(groupPosition).getPapers().size();
}
public Talk getGroup(int groupPosition) {
return talks.get(groupPosition);
}
public int getGroupCount() {
return talks.size();
}
public long getGroupId(int groupPosition) {
return (long)(groupPosition*1024); // To be consistent with getChildId
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View v = null;
if(convertView != null)
v = convertView;
else
v = inflater.inflate(R.layout.group_row, parent, false);
Talk t = getGroup(groupPosition);
TextView colorGroup = (TextView)v.findViewById(R.id.papername);
if(t != null)
colorGroup.setText(t.getName());
return v;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public void onGroupCollapsed (int groupPosition) {}
public void onGroupExpanded(int groupPosition) {}
}
PapersFragment:
public class PapersFragment extends Fragment {
private PaperAdapter listAdapter;
private Context myContext;
List<Paper> papers = null;
private List<Paper> papersParser() {
try {
XMLParser2 parser = new XMLParser2();
papers = parser.parse(getActivity().getApplicationContext().getAssets().open("talks.xml"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return papers;
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_papers, container, false);
ListView lv = (ListView) v.findViewById(R.id.list);
papers = papersParser();
listAdapter = new PaperAdapter(getActivity(),papers);
lv.setAdapter(listAdapter);
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Si un jour je veux mettre une description
}
});
if (papers == null) {
Toast.makeText(getActivity().getApplicationContext(), "Papers vide", Toast.LENGTH_LONG).show();
}
return v;
}
}
PaperAdapter:
public class PaperAdapter extends BaseAdapter implements OnClickListener {
private Context context;
private List<Paper> papers;
private LayoutInflater inflater;
CheckBox cb;
TextView paper;
SqlHandler sqlHandler;
public PaperAdapter(Context context, List<Paper> papers) {
this.context = context;
this.papers = papers;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount(){
return papers.size();
}
@Override
public Object getItem(int position) {
return papers.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
TextView title;
TextView author;
CheckBox name;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
// We only create the view if its needed
if (view == null) {
view = inflater.inflate(R.layout.child_row, null);
// Set the click listener for the checkbox
//view.findViewById(R.id.check1).setOnClickListener(this);
Paper p = (Paper) getItem(position);
// Set the example text and the state of the checkbox
CheckBox cb = (CheckBox) view.findViewById(R.id.check1);
//cb.setChecked(p.isSelected());
// We tag the data object to retrieve it on the click listener.
paper = (TextView)view.findViewById(R.id.papername);
if (paper != null)
paper.setText(p.getTitle());
TextView author = (TextView)view.findViewById(R.id.authorname);
if(author!= null)
author.setText(p.getAuthor());
holder = new ViewHolder();
holder.name = (CheckBox) view.findViewById(R.id.check1);
holder.title = (TextView) view.findViewById(R.id.papername);
holder.author= (TextView) view.findViewById(R.id.authorname);
view.setTag(holder);
holder.name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) v;
Paper paper = (Paper) cb.getTag();
String title = paper.getTitle();
String author = paper.getAuthor();
if (cb.isChecked()){
Toast.makeText(context, "Papier "+title+ " ajouté", Toast.LENGTH_SHORT).show();
//String query = "INSERT INTO SELECTED_PAPERS(title,author) values ('"
//+ title +"','" + author +"')";
//sqlHandler.executeQuery(query);
}
if (cb.isChecked()==false){
Toast.makeText(context, "Papier "+title+ " retiré", Toast.LENGTH_SHORT).show();
}
}
});
}
else{
holder = (ViewHolder) view.getTag();
}
Paper paper = papers.get(position);
holder.author.setText(paper.getAuthor());
holder.title.setText(paper.getTitle());
holder.name.setTag(paper);
return view;
}
/*@Override
/** Will be called when a checkbox has been clicked. */
public void onClick(View view, int position) {
/*TextView p = (TextView) view.findViewById(R.id.papername);
TextView a = (TextView) view.findViewById(R.id.authorname);
CheckBox cb = (CheckBox) view.findViewById(R.id.check1);
String title = p.getText().toString();
String author = a.getText().toString();
String query = "INSERT INTO SELECTED_PAPERS(title,author) values ('"
+ title +"','" + author +"')";
sqlHandler.executeQuery(query);*/
//TextView p = (TextView) view.findViewById(R.id.papername);
//TextView a = (TextView) view.findViewById(R.id.authorname);
//String title = p.getText().toString();
//String author = a.getText().toString();
Paper p = (Paper) this.getItem(position);
String title = p.getTitle();
Toast.makeText(context, "Papier ajouté", Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
/*
private void savePrefs(String key, String checked) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
Editor edit = sp.edit();
edit.putString (key, checked);
edit.commit();
}*/
}
非常感謝!
不確定問題是什麼? :) –
我希望如果您在會談列表中查看«紙張»,它也會在紙張列表中檢查:) –
看起來,您永遠無法同時打開兩個列表,所以問題是,爲什麼這些列表使用不同的數據源,如果你正在使用相同的講座。如果數據的顯示方式不同,數據源應該是相同的,因此當您選中一個框時,您會通知適配器數據已更改,而另一個片段應該立即將其提取出來,我認爲您採用的方法錯誤這就是爲什麼這個相對簡單的事情開始變得複雜。 –