使用setOnItemClickListener时,Android ListView与自定义ArrayAdapter错误

问题描述 投票:0回答:1

我附加了一个xml文件来修改列表视图中显示的对象样式,创建一个自定义ArrayAdapter类。现在我想设置一个监听器,告诉我用户选择了哪个项目。它不仅提供错误消息,而且还不执行方法中的任何代码。我尝试回到一个简单的listView,没有自定义适配器(使用默认适配器之一),它的工作原理。我已经在这里查看并搜索了错误消息,这似乎是与SELinux权限相关的问题但我没有改变任何这些也因为我不知道如何做到这一点。

这是错误消息:

W / RenderThread:type = 1400 audit(0.0:5784638):avc:denied {read} for name =“u:object_r:vendor_default_prop:s0”dev =“tmpfs”ino = 21556 scontext = u:r:untrusted_app:s0: c164,c256,c512,c768 tcontext = u:object_r:vendor_default_prop:s0 tclass = file permissive = 0

这是适配器:

public class CustomAdapter extends ArrayAdapter<Schedule> {

public CustomAdapter(Context context, ArrayList<Schedule> items) {
    super(context, R.layout.custom_row, items);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater myInflater = LayoutInflater.from(getContext());

    View customView = myInflater.inflate(R.layout.custom_row, parent, false);

    Schedule item = getItem(position);

    TextView scheduleName = customView.findViewById(R.id.customRow_scheduleName);
    TextView scheduleStart = customView.findViewById(R.id.customRow_startDate);
    TextView scheduleEnd = customView.findViewById(R.id.customRow_endDate);

    scheduleName.setText(item.getName());
    scheduleStart.setText(item.getStartDate());
    scheduleEnd.setText(item.getEndDate());

    return customView;
}}

这是包含listView的主类

public class MainActivity extends AppCompatActivity implements CreateNewScheduleFragment.OnInputListener {

private ArrayList<Schedule> scheduleCollection;

private ListAdapter customAdapter;

ListView scheduleListView;
Button newScheduleBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    newScheduleBtn = findViewById(R.id.addNewSchedule);

    scheduleCollection = new ArrayList<>();

    //String[] array = {"pippo", "pluto"};

    customAdapter = new CustomAdapter(this, scheduleCollection);
    scheduleListView = findViewById(R.id.scheduleListView);
    scheduleListView.setAdapter(customAdapter);//new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array));

    // #################### DEBUG
    scheduleCollection.add(new Schedule("Scheda 1", "14-3", "21-04", "ciao"));
    scheduleCollection.add(new Schedule("Scheda 2", "22-05", "5-06", "ciao"));
    ((ArrayAdapter<Schedule>)customAdapter).notifyDataSetChanged();
    // #################### DEBUG

    scheduleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.i("info", "I'm item in position " + String.valueOf(position));
        }
    });}

这是布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:background="@drawable/fragment_rounded_corners"
    android:padding="8dp"
    android:layout_margin="10dp">

    <TextView
        android:id="@+id/customRow_scheduleName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="8dp"
        android:layout_marginTop="0dp"
        android:maxWidth="200dp"
        android:text="Scheda 1"
        android:textColor="@color/colorTextIcons"
        android:textSize="35dp" />

    <TextView
        android:id="@+id/customRow_startDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="20dp"
        android:layout_toStartOf="@+id/customRow_endDate"
        android:text="14-03"
        android:textColor="@color/colorTextIcons"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/customRow_endDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:text="21-05"
        android:textColor="@color/colorTextIcons"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/customRow_startDate"
        android:layout_alignStart="@+id/customRow_startDate"
        android:layout_marginBottom="10dp"
        android:text="period:"
        android:textColor="@color/colorTextIcons" />

    <Button
        android:id="@+id/customRow_cancelBtn"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:background="@drawable/ic_cancel_black_24dp"/>

</RelativeLayout>

这是自定义对象类:

package com.example.fabri.gymine;

import android.util.Log;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Schedule {

    private ArrayList<Workout> workouts;
    private String name;
    private String startDate, endDate, filePath;

    public Schedule(String name, String startDate, String endDate, String filePath) {
        this.name = name;
        this.startDate = startDate;
        this.endDate = endDate;
        this.filePath = filePath;
        workouts = new ArrayList<>();

        //loadScheduleFromFile();
    }

    public void removeWorkout(Workout w){
        workouts.remove(w);
    }

    public int workoutCount(){
        return workouts.size();
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public String getStartDate() {
        return startDate;
    }

    public String getEndDate() {
        return endDate;
    }

    private String myReadLine(BufferedReader br) throws IOException{
        String tmp = br.readLine();
        Log.i("Letto", tmp);
        return tmp;
    }
}
android android-listview android-arrayadapter
1个回答
0
投票
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Check if an existing view is being reused, otherwise inflate the view
    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.custom_row, parent, false);
    }
}

也用

   scheduleListView.setAdapter(customAdapter);

scheduleCollection.add(new Schedule("Scheda 1", "14-3", "21-04", "ciao"));
scheduleCollection.add(new Schedule("Scheda 2", "22-05", "5-06", "ciao"));
© www.soinside.com 2019 - 2024. All rights reserved.