简体   繁体   English

单击ListView中位置的按钮后如何获取文本?

[英]How to get the text after clicking the button in the position in the ListView?

I have a problem that I want to get text after clicking a button in the ListView list element.我有一个问题,我想在单击 ListView 列表元素中的按钮后获取文本。 I tried the way with onItemClickListener, but I probably used it badly, I put it in the wrong place and removed it for the moment.我用onItemClickListener的方式试过,但是我可能用的不好,我把它放在错误的地方,暂时删除了它。 Question - how to do it?问题 - 怎么做?

I'm a novice programmer in Java, so my code may be a bit weak.我是 Java 的新手程序员,所以我的代码可能有点弱。 Thank you in advance for your help.预先感谢您的帮助。

ListFragment.java ListFragment.java

    public class ListFragment extends Fragment {

    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_list, container, false);

        FloatingActionButton studentAdd = view.findViewById(R.id.studentAddToList);
        studentAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), StudentAddToList.class);
                startActivity(myIntent);
            }
        });

        fillListView();

        return view;

    }

    class ListAdapter extends BaseAdapter {

        public ArrayList<Student> studentsList;
        private Context context;
        DatabaseHandler databaseHandler = new DatabaseHandler(getContext());

        public ListAdapter(ArrayList<Student> list, Context context) {
            this.studentsList = list;
            this.context = context;
        }

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

        @Override
        public Object getItem(int position) {
            return this.studentsList.get(position);
        }

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

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;

            if (convertView == null) {

                LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inf.inflate(R.layout.custom_list, null);

                holder = new ViewHolder();
                holder.studentId = (TextView) convertView.findViewById(R.id.studentId);
                holder.studentFullname = (TextView) convertView.findViewById(R.id.studentFullName);

                convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }


            Student student = studentsList.get(position);
            holder.studentId.setText(student.getStudentID() + "");
            holder.studentFullname.setText(student.getFullname());

            return convertView;
        }

        private class ViewHolder {
            public TextView studentId;
            public TextView studentFullname;
        }

    }

    public void fillListView () {
        ListView listView = view.findViewById(R.id.listView);
        DatabaseHandler databaseHandler = new DatabaseHandler(view.getContext());

        ArrayList<Student> studentsList = databaseHandler.getStudents();

        ListAdapter listAdapter = new ListAdapter(studentsList, view.getContext());
        listView.setAdapter(listAdapter);
    }

}

    <TextView
    android:id="@+id/studentId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="ID"
    android:textSize="24sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.069"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.041" />

custom_list.xml custom_list.xml

<TextView
    android:id="@+id/studentFullName"
    android:layout_width="153dp"
    android:layout_height="32dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:gravity="center_vertical"
    android:text="Full Name"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.339"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.042" />

<ImageButton
    android:id="@+id/studentDeleteFromList"
    android:layout_width="33dp"
    android:layout_height="29dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:background="@null"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.937"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.04"
    app:srcCompat="@drawable/delete" />

You can add a OnItemClickListener to your list items.您可以将 OnItemClickListener 添加到您的列表项。 Here is a link to a similar question that should help you out:这是一个类似问题的链接,应该可以帮助您:

setOnItemClickListener on custom ListView 自定义 ListView 上的 setOnItemClickListener

You can add onItemClickListener() to listView on fullListView() .您可以添加onItemClickListener()listViewfullListView()

Here's the sample according to your code.这是根据您的代码的示例。

public void fillListView () {
        ListView listView = view.findViewById(R.id.listView);
        DatabaseHandler databaseHandler = new DatabaseHandler(view.getContext());
        ArrayList<Student> studentsList = databaseHandler.getStudents();
        ListAdapter listAdapter = new ListAdapter(studentsList, view.getContext());
        listView.setAdapter(listAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              //here, replace with your list object and collect clicked items
              Object listItem = list.getItemAtPosition(position);
           } 
        });
}

Try this code hope it will help you:试试这个代码希望它会帮助你:

public void fillListView () {
    ListView listView = view.findViewById(R.id.listView);
    DatabaseHandler databaseHandler = new DatabaseHandler(view.getContext());
    ArrayList<Student> studentsList = databaseHandler.getStudents();
    ListAdapter listAdapter = new ListAdapter(studentsList, view.getContext());
    listView.setAdapter(listAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
       @Override
       public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
          //here, replace with your list object and collect clicked items
          String name = adapterView.getItemAtPosition(i).toString();
          Toast.makeText(getApplicationContext,"text is :"+name, Toast.LENGTH_SHORT).show();
       } 
    });
 }

You can use studensList to get the item based on the position您可以使用studensList根据position获取项目

   public void fillListView () {
            ListView listView = view.findViewById(R.id.listView);
            DatabaseHandler databaseHandler = new DatabaseHandler(view.getContext());
            ArrayList<Student> studentsList = databaseHandler.getStudents();
            ListAdapter listAdapter = new ListAdapter(studentsList, view.getContext());
            listView.setAdapter(listAdapter);

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
               @Override
               public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                  final Student student = studentsList.get(pos);
               } 
            });
    }

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

相关问题 单击按钮后如何显示文本 - How to make text appear after clicking a button 单击按钮后如何显示文本 - How to make text show after clicking button 单击listView上的按钮后获取_id和position项目 - Android - Get _id and position item after click button on listView - Android 如何通过单击将增加数组位置的按钮来在textView上的数组中设置文本 - How to set text in array on a textView by clicking a button that will increment the array position 获取在ListView中单击的按钮的位置 - Get the position of a button clicked in ListView 如何在显示文本或单击按钮时获得振动? - How do I get vibration when displaying text or clicking a button? 单击RC中的按钮后查找文本的问题 - Issue with finding text after clicking a button in RC 单击列表视图后,显示带有按钮和Edittext的迷你表单 - Show mini form with button and Edittext inside after clicking the listview 单击“删除”按钮后如何删除按钮? - How to delete button after clicking on “delete” button? 单击列表视图中的项目后如何传递字符串并获取其数据,必须根据单击的项目获取各自的数据 - How to pass the string and get their data after clicking the item in an listview,it has to take their respective data according to the click item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM