繁体   English   中英

使列表视图接收我正在使用片段的两个文本

[英]make the listview receive two texts I'm using fragment

嗨,大家好,我有问题.......我使用带有两个选项卡的viewpager创建了片段活动,并创建了具有两个textviews listview ,我想将文本从选项卡一发送到选项卡二作为标题和说明,我有成功发送了说明,但未能发送标题(我只能发送一个文本)我需要( Getter()/Setter() )吗? 怎么办??

我的fragment_one.java:

public class FragmentOne extends Fragment {
SendMessage SM;
ViewPager viewPager;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(
            R.layout.fragment_one, container, false);
    return rootView;


}


@Override
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    viewPager = (ViewPager)view.findViewById(R.id.viewPager);
    Button btnPassData = (Button) view.findViewById(R.id.btnPassData);

    final EditText inData = (EditText) view.findViewById(R.id.inMessage);
    btnPassData.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SM.sendData(inData.getText().toString().trim());
        }
    });

}

interface SendMessage {
    void sendData(String message);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    try {
        SM = (SendMessage) getActivity();
    } catch (ClassCastException e) {
        throw new ClassCastException("Error in retrieving data. Please try again");
    }
}

}

我的fragment_two.java:

public class FragmentTwo extends Fragment {

ListView listView;
ArrayList<String> arrayList = new ArrayList<>();
ArrayAdapter<String> adapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(
            R.layout.fragment_two, container, false);
    return rootView;


}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    listView = (ListView) view.findViewById(R.id.list_view);
    adapter = new ArrayAdapter<String>(getActivity(), R.layout.single_item,R.id.tvdesc, arrayList);

    listView.setAdapter(adapter);
}

protected void displayReceivedData(String message) {
    arrayList.add(0,message);
    adapter.notifyDataSetChanged();

}

}

我的custom_items.xml:

<TextView
    android:id="@+id/tvtitle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dip"
    android:layout_toLeftOf="@+id/tvdate"
    android:layout_toRightOf="@+id/image"
    android:maxLines="1"
    android:text="title"
    android:textColor="@android:color/black"
    android:textSize="15dip"
    android:textStyle="bold" />

<TextView
    android:id="@+id/tvdesc"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tvtitle"
    android:layout_toRightOf="@+id/image"
    android:ellipsize="end"
    android:maxLines="3"
    android:text="desc"
    android:textColor="@android:color/black"
    android:textSize="13dip"
    android:lines="3" />

<TextView
    android:id="@+id/tvdate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dip"
    android:text="date"
    android:textColor="@android:color/black"
    android:textSize="12dip" />

注意:我已经问过这个问题很多次了,但是没有人帮助过我,因此,如果您知道答案对我有帮助或忽略了这个问题,那么您就不会在其他问题上添加任何链接。

使您的SendMessage界面像这样

 interface SendMessage {
    void sendData(String title,String message);
}

现在,使用包将标题和消息发送到另一个片段。 我认为对您有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM