我创建了一个包含ListView的Fragment,并且我已经创建了一个costumAdapter来填充它。
当我点击ListView上的某个项目时,我在空对象引用上遇到错误。
我点击的项目的位置是正确的,但是当我尝试访问listView的对象时,我收到了错误。
这是我的片段
public class PartiteDisponibiliFragment extends Fragment {
public PartiteDisponibiliFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_partite_disponibili, container, false);
final databasehelper databaseHelper = new databasehelper(this.getContext());
final ListView simpleList = view.findViewById(R.id.ListView01);
ArrayList<String> list_data = new ArrayList<String>();
ArrayList<String> list_luogo = new ArrayList<String>();
ArrayList<String> list_orario = new ArrayList<String>();
Button btn_addpartita = view.findViewById(R.id.btn_addpartita);
final Cursor b = databaseHelper.getPartiteDisponibili();
try {
while (b.moveToNext()) {
list_luogo.add(b.getString(0));
list_orario.add(b.getString(1));
list_data.add(b.getString(2));
}
} finally {
b.close();
}
MyAdapterMatch customAdapter =
new MyAdapterMatch(this.getContext(),
list_luogo, list_orario, list_data);
simpleList.setAdapter(customAdapter);
btn_addpartita.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(getContext(), AddPartitaActivity.class);
startActivity(myIntent);
}
});
simpleList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Toast.makeText(getContext(), "Foo", Toast.LENGTH_SHORT).show();
Object o = simpleList.getItemAtPosition(position);
String s = o.toString();
}
});
return view;
}
}
这是我的costumAdapter
public class MyAdapterMatch extends BaseAdapter {
private Context context;
private int resource;
private ArrayList<String> list_luogo;
private ArrayList<String> list_orario;
private ArrayList<String> list_data;
private LayoutInflater inflter;
//static ArrayList<Integer> selectedAnswers;
MyAdapterMatch(Context applicationContext,
ArrayList<String> list_luogo,
ArrayList<String> list_orario,
ArrayList<String> list_data) {
this.context = context;
this.resource = resource;
this.list_data = list_data;
this.list_orario = list_orario;
this.list_luogo = list_luogo;
// initialize arraylist and add static string for all the questions
/*selectedAnswers = new ArrayList<>();
for (int i = 0; i < list_data.size(); i++) {
selectedAnswers.add(0);
}*/
inflter = (LayoutInflater.from(applicationContext));
}
@Override
public int getCount() {
return list_data.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@SuppressLint("ViewHolder")
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.partite_item, null);
// get the reference of TextView and Button's
TextView tv_data = view.findViewById(R.id.tv_data);
TextView tv_luogo = view.findViewById(R.id.tv_luogo);
TextView tv_orario = view.findViewById(R.id.tv_orario);
tv_data.setText(list_data.get(i));
tv_luogo.setText(list_luogo.get(i));
tv_orario.setText(list_orario.get(i));
return view;
}
}
这是我的partite_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
android:orientation="horizontal">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Luogo:"
android:textColor="@android:color/black"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Data:"
android:textColor="@android:color/black"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Orario:"
android:textColor="@android:color/black"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<TextView
android:id="@+id/tv_luogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/tv_luogo" />
<TextView
android:id="@+id/tv_orario"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintStart_toEndOf="@+id/textView4"
app:layout_constraintTop_toBottomOf="@+id/tv_data" />
答案 0 :(得分:1)
我认为,这段代码是一个问题:
@Override
public Object getItem(int i) {
return null;
}
答案 1 :(得分:1)
你的问题是
@Override
public Object getItem(int i) {
return null;
}
你正在返回null,而是返回列表位置,即i。
@Override
public long getItemId(int i) {
return 0;
}
你正在返回null,而是返回id即。,i。
见下面的代码:
@Override
public Object getItem(int i) {
return list_data.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
答案 2 :(得分:0)
试试这个
将接口插入适配器以进行点击处理。
onItemClickListner onItemClickListner;
public void setOnItemClickListner(CommentsAdapter.onItemClickListner onItemClickListner) {
this.onItemClickListner = onItemClickListner;
}
public interface onItemClickListner {
void onClick(Contact contact);//pass your object types.
}
适配器中的和getView()。
outerContiner.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemClickListner.onClick(contact);
}
});
,最后将适配器绑定到列表视图中,然后调用下面的代码。
adapter.setOnItemClickListner(new CommentsAdapter.onItemClickListner() {
@Override
public void onClick(Contact contact) {
// here get click item object data.
}
});