如何通过主要活动更新片段之间的ListView内容 [英] how to update listview contents between fragments through the main activity

查看:219
本文介绍了如何通过主要活动更新片段之间的ListView内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个片段,第二个,第三个,在各自的布局列表视图。

I have three fragments, the second and the third one, have a listView in their respective layout.

开始,无论是第二和第三Frament的ListView中,填充了相同的项目。我换句话说,最初的ListView控件
第二个片段,第三个,包含以下内容,其中CB:是复选框,IV:是ImageView的t为:TextView的,并且SaveButton是布顿

initially, the listView of both "second and third Frament", is populated with the same items. i other words, initially the the listView of the second fragment and the third one, contain the following where CB: is checkBox and IV: is ImageView and t is: textview, and SaveButton is a buton

t1........CB.......IV
t2........CB.......IV
t3........CB.......IV
t4........CB.......IV
t5........CB.......IV
t6........CB.......IV
    SaveButton

我所试图做的是,当我在第二个片段,并选择从ListView的一个项目(S)使用复选框,并点击保存按钮,那么,我选择的项目,应删除从第三个片段列表视图。

what i am trying to do is, while i am in the second fragment and selected an item(s) from the listView "using the checkbox" and clicked "Save" button, then, that item i selected, should be deleted from the listView in the third Fragment.

要实现这一目标,在getview(),我检查,如果该复选框是从第二个片段的Lis​​tView的检查,如果它被选中,我添加了检查项目
到的列表。如图getView():

to achieve this,in getview(), i checked if the the checkBox is checked from the the listView of the second fragment, and if it is checked, i add the checked items to a list. as shown in getView():

第二块碎片

private void setUpListView() {
    // TODO Auto-generated method stub
    this.topicsList = new ArrayList<String>();

    for (int i = 0; i < this.topics.length; i++) {
        this.topicsList.add(topics[i]);
    }

    this.adapter = new ListViewAdapter(getActivity(), this.topicsList, ECO_FRAG_ID);
    this.listView.setAdapter(adapter);
}

第三块碎片

private void setUpListView() {
    // TODO Auto-generated method stub
    this.topicsList = new ArrayList<String>();

    for (int i = 0; i < this.topics.length; i++) {
        this.topicsList.add(topics[i]);
    }

    this.adapter = new ListViewAdapter(getActivity(), this.topicsList, LOGGER_FRAG_ID);
    this.listView.setAdapter(adapter);
}

BaseAdapter

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    if (convertView == null) {
        LayoutInflater layoutinflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = layoutinflater.inflate(R.layout.list_items_layout, null);
    }

    if (this.checkedItemsList1 == null) {
        this.checkedItemsList1 = new ArrayList<String>();
    }

    if (this.checkedItemsList2 == null) {
        this.checkedItemsList2 = new ArrayList<String>();
    }

    final TextView tv = (TextView) convertView.findViewById(R.id.tvlist_topic);
    final CheckBox cb = (CheckBox) convertView.findViewById(R.id.cbList_hook);
    final ImageView iv = (ImageView) convertView.findViewById(R.id.ivList_delete);

    tv.setText(this.topicsList.get(position));

    if (cb.isChecked() && (this.id == 1) ) {
        this.checkedItemsList1.add(this.topicsList.get(position));
        this.setCheckedItemsList1(this.checkedItemsList1);
    }
    if (cb.isChecked() && (this.id == 2) ) {
        this.checkedItemsList2.add(this.topicsList.get(position));
        this.setCheckedItemsList2(this.checkedItemsList2);
    }

    iv.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (cb.isChecked())
                cb.setChecked(false);

            topicsList.remove(position);
            notifyDataSetChanged();
        }
    });

    return convertView;
}

和我创建了一个接口,它是在onAttach(初始化),并要求当我在secondFragment点击savebuton为folows:

And i created an interface, which is initialised in onAttach() and called when i click the savebuton in the secondFragment as folows:

private OnClickListener btnSaveListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(getActivity(), "Save to SQLite", Toast.LENGTH_SHORT).show();
        Log.d(TAG, "size: " + adapter.getCheckedItemsList1().size());
        iCSC.onCheckedStateChanged(adapter.checkedItemsList1, ECO_FRAG_ID);
    }
};

这inerface,在MainActivity实现它,并且在界面的implemntation,我通过列表f从第二个片段的检查项目到第三

this inerface, the mainActivity implements it, and in the implemntation of the interface, i pass the list f the checked items from the second Fragment to the third Fragment through a public method in the third fragmet that updates the list and then assign the list to the adapter, as follows:

@Override
public void onCheckedStateChanged(ArrayList<String> list, int id) {
    // TODO Auto-generated method stub

    switch (id) {
    case 1:
        ((Logger_Settings_Frag) this.fragList.get(2)).updateTopicList(list);
        break;
    case 2:

        break;
    }
}

**updateTopicList in the third fragment**

public void updateTopicList(ArrayList<String> list) {
    for (String str : list) {
        this.topicsList.remove(str);
    }

    this.adapter = new ListViewAdapter(getActivity(), this.topicsList, LOGGER_FRAG_ID);
    this.listView.setAdapter(adapter);
}

updateTopicList第三片段

public void updateTopicList(ArrayList<String> list) {
    for (String str : list) {
        this.topicsList.remove(str);
    }

    this.adapter = new ListViewAdapter(getActivity(), this.topicsList, LOGGER_FRAG_ID);
    this.listView.setAdapter(adapter);
}

当我运行code,在第二个片段的saveButton侦听器,则log.d消息显示应包含被检查的项目列表的大小,为大小为零?!

when i run that code, in the saveButton listener of the second fragment, the log.d message displays that the size of the list that should contain the items that was checked, is zero size?!

请看看在code,让我知道我缺少什么?

please have a look at the code and let me know what i am missing?

推荐答案

试试这个:有两个片段其样本项目OneActivity:
    公众的ArrayList myBeansList_frgnt1;
    公众的ArrayList myBeansList_frgnt2;
通过点击保存按钮迭代myBeansList_frgnt1并检查任何项目被选择或不的条件。如果项目被选中我加入该项目,以myBeansList_frgnt2在fragment2 .showing。

try this: Its a sample Project OneActivity with two Fragments: public ArrayList myBeansList_frgnt1; public ArrayList myBeansList_frgnt2; by clicking the save button iterate the myBeansList_frgnt1 and checking the condition that any item is selected or not. if item is selected i am adding that item to myBeansList_frgnt2 .showing in fragment2.

MainActivity:

MainActivity:

public class MainActivity extends FragmentActivity {
private FragmentManager mFragmentManager;
private MyFragment1 myFragment1;
public ArrayList<MyBean> myBeansList_frgnt1;
public ArrayList<MyBean> myBeansList_frgnt2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mFragmentManager = getSupportFragmentManager();
    myBeansList_frgnt1 = new ArrayList<MyBean>();
    myBeansList_frgnt2 = new ArrayList<MyBean>();

}

@Override
protected void onResume() {
    super.onResume();
    myFragment1 = new MyFragment1();
    FragmentTransaction mTransaction = mFragmentManager.beginTransaction();
    mTransaction.replace(R.id.framid, myFragment1, "applock").commit();
}

}

MyBaseAdpter:保持美国在MyBean对象

MyBaseAdpter: maintaining the states in the MyBean object

@Override
public View getView(int position, View view, ViewGroup parent) {
    final ViewHolder holder;
    if (view == null) {
        holder = new ViewHolder();
        view = LayoutInflater.from(mContext).inflate(R.layout.list_item, null);
        holder.mTextView = (TextView) view.findViewById(R.id.textView1);
        holder.mBox = (CheckBox) view.findViewById(R.id.checkBox1);
        holder.mImageView = (ImageView) view.findViewById(R.id.imageView1);
        view.setTag(holder);

    } else {
        holder = (ViewHolder) view.getTag();
    }
    final MyBean mBean = mBeansList.get(position);

    holder.mBox.setOnCheckedChangeListener(null);
    holder.mBox.setChecked(mBean.isChecked());
    holder.mBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                mBean.setChecked(true);
            } else {
                mBean.setChecked(false);
            }
        }
    });
    // setting text and image
    holder.mTextView.setText(mBean.getmName());
    holder.mImageView.setImageResource(mBean.getmImgId());
    return view;
}
private class ViewHolder {
    TextView mTextView;
    CheckBox mBox;
    ImageView mImageView;
}

片段1:

public class MyFragment1 extends Fragment {
private ListView mListView;
private MyBaseAdpter myBaseAdpter;
private Button mButton;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_layout, null);
    mListView = (ListView) view.findViewById(R.id.listviewid);
    mButton = (Button) view.findViewById(R.id.bttnid);
    return view;
}

@Override
public void onResume() {
    super.onResume();
    final MainActivity mActivity = (MainActivity) getActivity();
    myBaseAdpter = new MyBaseAdpter(mActivity.myBeansList_frgnt1, getActivity());
    mListView.setAdapter(myBaseAdpter);
    mButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            int size = mActivity.myBeansList_frgnt1.size();
            for (int i = 0; i < size; i++) {
                MyBean mMyBean = mActivity.myBeansList_frgnt1.get(i);
                if (mMyBean.isChecked()) {
                    mActivity.myBeansList_frgnt2.add(mMyBean);
                }
            }
        }
    });
}

}

Fragment2:

Fragment2:

public class MyFragment2 extends Fragment {
private ListView mListView;
private MyBaseAdpter myBaseAdpter;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_layout, null);
    mListView = (ListView) view.findViewById(R.id.listviewid);
    return view;
}

@Override
public void onResume() {
    super.onResume();
    MainActivity mActivity = (MainActivity) getActivity();
    myBaseAdpter = new MyBaseAdpter(mActivity.myBeansList_frgnt2, getActivity());
    mListView.setAdapter(myBaseAdpter);
}

}

MyBean:

public class MyBean {
private String mName;
private int mImgId;
private boolean isChecked;

public String getmName() {
    return mName;
}

public void setmName(String mName) {
    this.mName = mName;
}

public int getmImgId() {
    return mImgId;
}

public void setmImgId(int mImgId) {
    this.mImgId = mImgId;
}

public boolean isChecked() {
    return isChecked;
}

public void setChecked(boolean isChecked) {
    this.isChecked = isChecked;
}

public MyBean() {
    super();
    // TODO Auto-generated constructor stub
}

}

这篇关于如何通过主要活动更新片段之间的ListView内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆