繁体   English   中英

将用户单击/滑动的项目从第一个片段的一个列表视图转移到第二个片段的另一个列表视图

[英]Transferring user clicked/swiped item from one listview in 1st fragment to another listview in 2nd fragment

我需要知道如何将项目从第一个片段的列表视图移动到第二个片段的另一个列表视图。以下是2个片段的代码以及包含列表项视图的xml文件。 AbsentStudentFragment.java

编辑:我已经用数组列表替换了所有数组,并且正在使用带有简单POJO类的Arraylist为列表适配器生成数据。 请检查以下内容。我的问题是如何使用从名称,信心等列表项中获取的值或我获得的StudentDetails类型的POJO并将这些值添加到PresentStudent片段中的PresentStudent数组中。

public class AbsentStudentFragment extends Fragment
{
ListView listForAbsentStudents;
Context context;


    public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState)
    {
       final ArrayList<StudentDetails> stuAbsent= new ArrayList<>();

    stuAbsent.add(new StudentDetails("name","nameNot",R.mipmap.user));
    stuAbsent.add(new StudentDetails("81","checkName",R.mipmap.user));
    stuAbsent.add(new StudentDetails("60","3rd",R.mipmap.user));

    View view = inflater.inflate(R.layout.fragment_absent_students, container,false);
    context=getActivity();
    listForAbsentStudents=(ListView)view.findViewById(R.id.listview_for_absent_students);
  final AdapterForAbsentList adapterForAbsentList=new    AdapterForAbsentList(context,stuAbsent);
       listForAbsentStudents.setAdapter(adapterForAbsentList);
        listForAbsentStudents.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                 //what to do here.?
                //Added below code now
            StudentDetails studentDetails4;
            studentDetails4=stuAbsent.get(position);

            String temName=studentDetails4.getStudentName();
            String tempConfidence=studentDetails4.getStudentConfidence();
            int tempImage=studentDetails4.getStudentImage();
 //Now how can I add these values to presentstudent array list in PresentStudent.java fragment.?

                         //presentStudentFragment.addToPresentStudents(temName,tempConfidence,tempImage);
                   Toast.makeText(getContext(),"value"+temName,Toast.LENGTH_SHORT).show();
        }
    });
        return view;
    }
    public class AdapterForAbsentList extends BaseAdapter
    {

  public AdapterForAbsentList(Context   mContext,ArrayList<StudentDetails>absentStudents) 
{
        this.absentStudents2 = absentStudents;

this.mContext = mContext;
    }

    ArrayList<StudentDetails> absentStudents2;
    private Context mContext;



    @Override
    public int getCount() {
        return absentStudents2.size();;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        View viewForListView;
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null)
        {
 viewForListView =inflater.inflate(R.layout.custom_row_for_absent_list,null);
            TextView txtConfidence=(TextView)viewForListView.findViewById(R.id.txtConfidence_Temp);
            TextView txtName=(TextView)viewForListView.findViewById(R.id.txtName_Temp);
            ImageView imgStudentThumbnail=(ImageView)viewForListView.findViewById(R.id.imgFile_Temp);
           txtConfidence.setText(absentStudents2.get(i).getStudentConfidence());
            txtName.setText(absentStudents2.get(i).getStudentName());
            imgStudentThumbnail.setImageResource(absentStudents2.get(i).getStudentImage());

        }
        else
        {
            viewForListView = convertView;
        }
        return viewForListView;
    }
    }
    }

并且PresentStudentFragment.java Edited添加了arraylist。
如何将从AbsentStudentFragment.java的数组列表中获取的值添加到ArrayList listPresentStudents ..?

public class PresentStudentFragment extends Fragment {
ListView listForPresentStudents;
 public ArrayList<StudentDetails>listPresentStudents=new ArrayList<>();
Context context;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.fragment_present_students, container, false);
    context=getActivity();
        listPresentStudents.add(new StudentDetails("80","saurabh",R.mipmap.user));
    listPresentStudents.add(new StudentDetails("60","pqr",R.mipmap.user));

    listForPresentStudents=(ListView)view.findViewById(R.id.listview_for_present_students);

 AdapterForPresentList adapterForPresentListNew=new AdapterForPresentList(context,listPresentStudents);

   listForPresentStudents.setAdapter(adapterForPresentList);
    return view;
    }
 public class AdapterForPresentList extends BaseAdapter 
 {

    private Context mContext;
    ArrayList<StudentDetails>abc;


    public AdapterForPresentList(Context context,    ArrayList<StudentDetails>pqr) 
 {
        this.mContext=context;
        this.abc = pqr;
 }
    @Override
    public int getCount() {
        return abc.size();;
    }
    @Override
    public Object getItem(int i) {
        return null;
    }
    @Override
    public long getItemId(int i) {
        return 0;
    }
    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup)
    {
        View viewForListView;
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null)
        {
            viewForListView = inflater.inflate(R.layout.custom_row_for_present_list, null);
            TextView txtConfidence=(TextView)viewForListView.findViewById(R.id.txtConfidence_Temp2);
            TextView txtName=(TextView)viewForListView.findViewById(R.id.txtName_Temp2);
            ImageView imgStudentThumbnail=(ImageView)viewForListView.findViewById(R.id.imgFile_Temp2);

       txtName.setText(abc.get(i).getStudentName());
            txtConfidence.setText(abc.get(i).getStudentConfidence());
              imgStudentThumbnail.setImageResource(abc.get(i).getStudentImage());


        } else
        {
            viewForListView = convertView;
        }
        return viewForListView;
    }


    }
    }

POJO类StudentDetails.java

 public class StudentDetails {
 String studentName,studentConfidence;
 int studentImage;

 public StudentDetails() {
 }

 public String getStudentName() {
    return studentName;
 }

 public void setStudentName(String studentName) {
    this.studentName = studentName;
 }

 public String getStudentConfidence() {
    return studentConfidence;
 }

 public void setStudentConfidence(String studentConfidence) {
    this.studentConfidence = studentConfidence;
 }

 public int getStudentImage() {
    return studentImage;
 }

 public void setStudentImage(int studentImage) {
    this.studentImage = studentImage;
 }

 public StudentDetails(String studentName, String studentConfidence, int studentImage) {
    this.studentName = studentName;
    this.studentConfidence = studentConfidence;
    this.studentImage = studentImage;
 }
 }

以及列表项的视图custom_row_list

 <ImageView
    android:id="@+id/imgFile_Temp2"
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:src="@mipmap/user"
    android:padding="10dp"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/imgFile_Temp2"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txtConfidence_Temp2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textColor="@android:color/black"
        android:textSize="16sp" />


    <TextView
        android:id="@+id/txtName_Temp2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textColor="@android:color/black"
        android:textSize="16sp" />

我需要的是,如果我从listForAbsentStudents列表中滑动/单击了某个项目, listForAbsentStudents将其转移到listForPresentStudents列表中,并且该项目应从缺席的学生列表中删除。 我该怎么做..忘了刷卡..这似乎对我来说很遥远..请告诉我我应该如何使用listview onItemclick()方法来实现此目的。 我应该使用intent和/或bundle传递Extras ..如果是的话..? parent.getItemAt(position) ..

我没有得到你在问什么..我以为你的一个片段包含ListView ..对吗? 如果用户单击ListView中的任何项目,则该项目应传递到另一个片段。

请在AbsentStudentFragment.java中的onItemClick(..)方法中使用它

 PresentStudentFragment fragObj = new PresentStudentFragment();
 Bundle args = new Bundle();
 args.putString("YourKey", "YourValue");
 //as many as putString() you want  
 fragObj.setArguments(args);

在PresentStudentFragment.java中onCreateView(..)方法中的何处

String value = getArguments().getString("YourKey");

希望这对您有帮助。

暂无
暂无

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

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