如何通过主要活动更新片段之间的列表视图内容

时间:2015-02-02 10:13:17

标签: android listview android-fragments android-listview

我有三个片段,第二个和第三个片段,在各自的布局中都有一个listView。

最初,“second and third Frament”的listView填充了相同的项目。换句话说,最初是listView的 第二个片段和第三个片段,包含以下内容:CB:是checkBox和IV:是ImageView,t是:textview,而SaveButton是一个buton

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

我想要做的是,当我在第二个片段中并从listView中选择一个项目“使用复选框”并单击“保存”按钮,然后,我选择的那个项目应该被删除来自第三个片段中的listView。

为了实现这一点,在getview()中,我检查了是否从第二个片段的listView中检查了checkBox,如果选中它,我添加了检查的项目 到列表。如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时调用:

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);
    }
};

这个表面,mainActivity实现它,并且在接口的实现中,我将列表f从第二个片段传递到第三个片段到第三个片段 通过第三个fragmet中的公共方法片段更新列表,然后将列表分配给适配器,如下所示:

@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);
}

当我运行该代码时,在第二个片段的saveButton侦听器中,log.d消息显示应该包含已检查项目的列表大小是零大小?!

请查看代码,让我知道我错过了什么?

1 个答案:

答案 0 :(得分:0)

试试这个:它是一个带有两个片段的Project OneActivity示例:     public ArrayList myBeansList_frgnt1;     public ArrayList myBeansList_frgnt2; 通过单击保存按钮迭代myBeansList_frgnt1并检查是否选择了任何项目。如果选择了item,我将该项添加到myBeansList_frgnt2。在fragment2中显示。

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对象中的状态

@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:

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
}

}