java - 单击ListView中位置的按钮后如何获取文本?
问题描述
我有一个问题,我想在单击 ListView 列表元素中的按钮后获取文本。我尝试了onItemClickListener的方式,但我可能用得不好,我把它放在错误的地方并暂时将它删除。问题 - 怎么做?
我是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
<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" />
解决方案
试试这个代码,希望它能帮助你:
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();
}
});
}
推荐阅读
- c# - 无法向空 TcpClient 发送数据(检查是否调用了 Connect)”
- javascript - 如何从网站获取html数据
- python - 如何在 PyCharm 中通过 Tensorflow 访问 GPU?
- vba - Networkdays 循环 - 我想获得 Networkdays 循环。但给出一个错误。我正在使用office 2016。提前感谢您的努力
- xcode - websocket 适用于所有浏览器,但不适用于cordova + 模拟器?
- typescript - React Native 上的 GraphQL Apollo 类型生成
- asp.net - 尝试发布 .net 5 Web 应用程序时出错
- ruby-on-rails - ArgumentError:使用 `bundle exec rake db:migrate` 的参数数量错误
- c - 生成所有 10 位电话号码 - 回溯
- python - Fabric2 在嵌套 SSH 到 AWS EC2 实例上的本地主机(端口 2022)期间抛出“paramiko.ssh_exception.AuthenticationException”