我已经附加了一个xml文件,以修改创建自定义ArrayAdapter类的列表视图中显示的对象的样式。现在,我想设置一个侦听器,告诉我用户选择了哪个项目。它不仅给出错误消息,而且不执行该方法内的任何代码。我尝试回到没有自定义适配器的简单listView(使用默认适配器之一),并且它可以正常工作。我已经在这里查找并搜索了错误消息,这似乎是与SELinux权限有关的问题,但是我也没有更改任何这些内容,因为我不知道该怎么做。
这是错误消息:
W / RenderThread:类型= 1400审核(0.0:5784638):AVC:拒绝{读取}名称=“ 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 =文件许可= 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;
}
}
答案 0 :(得分:0)
[array([12, 15]), array([ 3, 7, 18, 18]), array([ 9, 16]), array([15, 19])]
也可以使用
@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);