简体   繁体   English

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

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

Hi guys I have problem....... I have created fragment activity using viewpager with two tabs,and created listview with two textviews , I want to sent the to texts from tab one to tab two as title and description,I have successfully sent the description but failed to send the title(just I can send only one text) do I need ( Getter()/Setter() ) ? 嗨,大家好,我有问题.......我使用带有两个选项卡的viewpager创建了片段活动,并创建了具有两个textviews listview ,我想将文本从选项卡一发送到选项卡二作为标题和说明,我有成功发送了说明,但未能发送标题(我只能发送一个文本)我需要( Getter()/Setter() )吗? How to do one?? 怎么办??

my fragment_one.java : 我的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");
    }
}

} }

my fragment_two.java : 我的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();

}

} }

my custom_items.xml : 我的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" />

note: I have asked this question many times but nobody has helped me, so you would not put any link to another question if you know the answer helped me or ignore this question. 注意:我已经问过这个问题很多次了,但是没有人帮助过我,因此,如果您知道答案对我有帮助或忽略了这个问题,那么您就不会在其他问题上添加任何链接。

make your SendMessage interface like this 使您的SendMessage界面像这样

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

Now send both title and message to another fragment using bundle. 现在,使用包将标题和消息发送到另一个片段。 I think it will be helpful to you 我认为对您有帮助

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

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