Java 如何在单击ListView中位置的按钮后获取文本?
我有一个问题,我想在单击ListView列表元素中的按钮后获取文本。我试着用这个方法来听,但我可能用得不好,我把它放错了地方,暂时把它拿走了。问题-怎么做 我是Java的新手程序员,所以我的代码可能有点弱。 提前感谢您的帮助 ListFragment.javaJava 如何在单击ListView中位置的按钮后获取文本?,java,android,Java,Android,我有一个问题,我想在单击ListView列表元素中的按钮后获取文本。我试着用这个方法来听,但我可能用得不好,我把它放错了地方,暂时把它拿走了。问题-怎么做 我是Java的新手程序员,所以我的代码可能有点弱。 提前感谢您的帮助 ListFragment.java public class ListFragment extends Fragment { View view; @Nullable @Override public View onCreateV
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" />
公共类ListFragment扩展了片段{
视图;
@可空
@凌驾
创建视图时的公共视图(LayoutFlater充气机、@Nullable ViewGroup容器、@Nullable Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment_列表,容器,错误);
FloatingActionButton studentAddd=view.findViewById(R.id.studentAddToList);
studentAdd.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Intent myIntent=newintent(view.getContext(),StudentAddToList.class);
星触觉(myIntent);
}
});
fillListView();
返回视图;
}
类ListAdapter扩展了BaseAdapter{
公共阵列列表学生列表;
私人语境;
DatabaseHandler DatabaseHandler=新的DatabaseHandler(getContext());
公共ListAdapter(ArrayList列表,上下文){
this.studentsList=列表;
this.context=上下文;
}
@凌驾
public int getCount(){
返回此.studentsList.size();
}
@凌驾
公共对象getItem(int位置){
返回此.studentsList.get(位置);
}
@凌驾
公共长getItemId(int i){
返回i;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
ViewHolder=null;
if(convertView==null){
LayoutInflater inf=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
convertView=inf.inflate(R.layout.custom_列表,空);
holder=新的ViewHolder();
holder.studentId=(TextView)convertView.findViewById(R.id.studentId);
holder.studentFullname=(TextView)convertView.findViewById(R.id.studentFullname);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
学生=学生列表。获取(位置);
holder.studentId.setText(student.getStudentID()+);
holder.studentFullname.setText(student.getFullname());
返回视图;
}
私有类视窗持有者{
公共文本视图学生ID;
公共文本查看学生全名;
}
}
公共无效fillListView(){
ListView ListView=view.findViewById(R.id.ListView);
DatabaseHandler DatabaseHandler=新的DatabaseHandler(view.getContext());
ArrayList studentsList=databaseHandler.getStudents();
ListAdapter=newListAdapter(studentsList,view.getContext());
setAdapter(listAdapter);
}
}
自定义列表.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" />
您可以在列表项中添加一个侦听器。下面是一个类似问题的链接,应该可以帮助您:
您可以在
fullListView()
上向listView
添加onItemClickListener()
这是根据您的代码提供的示例
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);
}
});
}
public void fillListView(){
ListView ListView=view.findViewById(R.id.ListView);
DatabaseHandler DatabaseHandler=新的DatabaseHandler(view.getContext());
ArrayList studentsList=databaseHandler.getStudents();
ListAdapter=newListAdapter(studentsList,view.getContext());
setAdapter(listAdapter);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
//此处,替换为列表对象并收集单击的项目
对象listItem=list.getItemAtPosition(位置);
}
});
}
尝试此代码,希望它能帮助您:
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();
}
});
}
public void fillListView(){
ListView ListView=view.findViewById(R.id.ListView);
DatabaseHandler DatabaseHandler=新的DatabaseHandler(view.getContext());
ArrayList studentsList=databaseHandler.getStudents();
ListAdapter=newListAdapter(studentsList,view.getContext());
setAdapter(listAdapter);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共无效onItemClick(AdapterView AdapterView、View视图、int i、long l){
//此处,替换为列表对象并收集单击的项目
字符串名称=adapterView.getItemAtPosition(i).toString();
Toast.makeText(getApplicationContext,“文本为:”+name,Toast.LENGTH_SHORT).show();
}
});
}
您可以使用studensList
根据位置获取项目
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);
}
});
}
public void fillListView(){
ListView ListView=view.findViewById(R.id.ListView);
DatabaseHandler DatabaseHandler=新的DatabaseHandler(view.getContext());
ArrayList studentsList=databaseHandler.getStudents();
ListAdapter=newListAdapter(studentsList,view.getContext());
setAdapter(listAdapter);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
最终螺柱