【问题标题】:Transferring user clicked/swiped item from one listview in 1st fragment to another listview in 2nd fragment将用户单击/滑动的项目从第一个片段中的一个列表视图转移到第二个片段中的另一个列表视图
【发布时间】:2016-12-22 10:06:16
【问题描述】:

我需要知道如何将项目从第一个片段的列表视图移动到第二个片段的另一个列表视图。下面是 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 已编辑添加了数组列表。
我应该如何将从 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 列表中滑动/单击一个项目,那么它应该被转移到 listForPresentStudents 列表中,并且该项目应该从缺席学生列表中删除。 我该怎么做..忘记刷卡..这对我来说似乎很遥远.. 请告诉我我应该在listviewonItemclick() 方法上做什么来实现这一点。 我应该使用意图和/或捆绑来传递额外内容..如果是这样的话..? 类似于parent.getItemAt(position)..?

【问题讨论】:

    标签: android listview android-fragments android-intent listadapter


    【解决方案1】:

    我没有得到你在问什么..我假设你的 Fragment 之一包含 ListView..对吗?如果用户单击 ListView 中的任何 Item,则该 Item 应该传递给另一个 Fragment..对吗?

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

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

    onCreateView(..) 方法中 PresentStudentFragment.java 的位置

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

    希望对你有所帮助..

    【讨论】:

    • 不..实际上我想要的是将一个项目从一个列表视图传递到另一个..是的你是对的,两个列表视图都在不同的片段中......问题出在远程通信中......我做到了尝试使用“界面和活动方法”bt 也不起作用......所以最后通过在后端使用 sqlite db 我让它工作......无论如何感谢您的回复..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    相关资源
    最近更新 更多