简体   繁体   中英

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() ) ? How to do one??

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

}

}

my 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

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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