gpt4 book ai didi

java - Android:onItemClick 和 onItemLongClick 没有响应

转载 作者:行者123 更新时间:2023-12-02 11:10:50 28 4
gpt4 key购买 nike

我的 Android 应用程序由 SQLite 数据库组成,该数据库使用用户保存的数据填充各个 ListView 项。这些项目可在 activity_main.xml 中显示。我有一个名为 RecordsListFragment 的类,其中包含两个有问题的方法:onItemClickonItemLongClick。以下是该类的完整内容:

package com.example.benignfella.projectworkinghoursapplication.Fragment;

import android.app.Activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

import com.example.benignfella.projectworkinghoursapplication.R;
import com.example.benignfella.projectworkinghoursapplication.Adapter.RecordsListAdapter;
import com.example.benignfella.projectworkinghoursapplication.Database.RecordsDAO;
import com.example.benignfella.projectworkinghoursapplication.GetSet.Records;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

public class RecordsListFragment extends Fragment implements OnItemClickListener, OnItemLongClickListener {

public static final String ARGUMENT_ITEM_ID = "records_list";

Activity activity;
ListView recordsListView;
ArrayList<Records> records;

RecordsListAdapter recordsListAdapter;
RecordsDAO recordsDAO;

private GetRecordsTask task;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

activity = getActivity();
recordsDAO = new RecordsDAO(activity);
}

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

task = new GetRecordsTask(activity);
task.execute((Void) null);
return view;
}

private void findViewsById(View view) {
recordsListView = (ListView) view.findViewById(R.id.list_records);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Records record = (Records) parent.getItemAtPosition(position);

if (records != null) {
Bundle arguments = new Bundle();
arguments.putParcelable("selectedRecord,", record);

CustomRecordsDialogFragment customRecordsDialogFragment = new CustomRecordsDialogFragment();
customRecordsDialogFragment.setArguments(arguments);
customRecordsDialogFragment.show(getFragmentManager(), CustomRecordsDialogFragment.ARGUMENT_ITEM_ID);
}
}

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Records records = (Records) parent.getItemAtPosition(position);

recordsDAO.deleteRecord(records);
recordsListAdapter.remove(records);

return true;
}


public class GetRecordsTask extends AsyncTask<Void, Void, ArrayList<Records>> {

private final WeakReference<Activity> activityWeakRef;

public GetRecordsTask(Activity context) {
this.activityWeakRef = new WeakReference<Activity>(context);
}

@Override
protected ArrayList<Records> doInBackground(Void... arg0) {
ArrayList<Records> recordsArrayList = recordsDAO.getRecords();
return recordsArrayList;
}

@Override
protected void onPostExecute(ArrayList<Records> empList) {
if (activityWeakRef.get() != null && !activityWeakRef.get().isFinishing()) {
records = empList;
if (empList != null) {
if (empList.size() != 0) {
recordsListAdapter = new RecordsListAdapter(activity, empList);
recordsListView.setAdapter(recordsListAdapter);
} else {
Toast.makeText(activity, "No Records about records... wait wot m8?",
Toast.LENGTH_LONG).show();
}
}
}
}
}

public void updateView() {
task = new GetRecordsTask(activity);
task.execute((Void) null);
}

public void onResume() {
getActivity().setTitle(R.string.app_name);
getActivity().getActionBar().setTitle(R.string.app_name);
super.onResume();
}
}

这是带有 FrameLayoutactivity_main.xml:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</RelativeLayout>

包含 ListView 的布局资源文件称为 fragment_records_list.xml,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f9f9f9">

<ListView
android:id="@+id/list_records"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="10dp"
android:drawSelectorOnTop="true"
android:footerDividersEnabled="false"
android:padding="10dp"
android:scrollbarStyle="outsideOverlay"/>

</RelativeLayout>

最后,有一个包含单个项目布局的资源文件,list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ededed"
android:descendantFocusability="blocksDescendants">

<RelativeLayout
android:id="@+id/layout_item"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/text_record_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="ID"
android:textSize="20dp"/>

<TextView
android:id="@+id/text_record_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/text_record_id"
android:padding="5dp"
android:text="Date"
android:textColor="#00942b"
android:textSize="20dp"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/text_record_date"
android:drawableStart="@drawable/ic_date_range_black_24dp"
android:padding="5dp"
/>

<TextView
android:id="@+id/text_record_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text_record_id"
android:padding="5dp"
android:text="Description"
/>

<TextView
android:id="@+id/text_record_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text_record_description"
android:padding="5dp"
android:text="17:00"
android:textSize="16dp"
android:textColor="#004561"
/>

<TextView
android:id="@+id/text_record_dash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text_record_description"
android:layout_toRightOf="@id/text_record_start"
android:padding="5dp"
android:text="-"
android:textSize="16dp"
/>

<TextView
android:id="@+id/text_record_finish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text_record_description"
android:layout_toRightOf="@id/text_record_dash"
android:padding="5dp"
android:text="20:00"
android:textSize="16dp"
android:textColor="#c7002a"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_below="@id/text_record_description"
android:layout_toRightOf="@id/text_record_finish"
android:drawableStart="@drawable/ic_timer_black_24dp"
/>
</RelativeLayout>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@id/layout_item"
android:background="#000000"
/>

</RelativeLayout>

我还没有找到问题的明确答案,所以我在这里寻求一些帮助。

最佳答案

您需要将OnItemClickListenerOnLongItemClickListener设置为ListView,我在初始化变量ListView中编辑了您的方法:

private void findViewsById(View view) {
recordsListView = (ListView) view.findViewById(R.id.list_records);
recordsListView.setOnItemClickListener(this);
recordsListView.setOnItemLongClickListener(this);
}

关于java - Android:onItemClick 和 onItemLongClick 没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50637495/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com