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

时间:2017-10-10 07:04:44

标签: java android android-layout listview android-fragments

大家好我有问题....... 我使用带有两个标签的viewpager创建了片段活动,并使用两个listview创建了textviews,我想将标签从标签1发送到标签2作为标题和说明,我有已成功发送说明但未能发送标题(我只能发送一个文字)我是否需要( Getter()/Setter() )?怎么办?

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

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" />

注意:我已多次提出这个问题,但没有人帮助我,所以如果你知道答案对我有帮助或忽略了这个问题,你就不会把任何链接放到另一个问题上。

1 个答案:

答案 0 :(得分:0)

  

像这样制作你的SendMessage界面

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

现在使用bundle将标题和消息发送到另一个片段。 我认为这会对你有所帮助