我有一個ListView,我正在使用自定義適配器和OnItemClickListener方法不起作用。ListView OnItemClickListener不能使用自定義適配器
我試着在StackOverFlow上搜索,但沒有任何答案似乎工作。
這裏是我的代碼:
row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="@+id/name"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="10sp"
android:layout_width="150sp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_marginRight="15sp" />
<TextView
android:id="@+id/record"
android:layout_height="wrap_content"
android:layout_marginLeft="15sp"
android:layout_gravity="right"
android:textSize="18sp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="150sp" />
</LinearLayout>
CustomAdapter:
package com.stackoverflow;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<DataItem> {
private ArrayList<DataItem> objects;
public CustomAdapter(Context context, int textViewResourceId, ArrayList<DataItem> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.row, null);
}
DataItem i = objects.get(position);
if (i != null) {
TextView nameView = (TextView) v.findViewById(R.id.name);
TextView recordView = (TextView) v.findViewById(R.id.record);
if (nameView != null){
nameView.setText(i.getname());
nameView.setTextSize(18);
nameView.setTextColor(Color.WHITE);
}
if (recordView != null){
recordView.setText(i.getrecord());
recordView.setTextSize(18);
recordView.setTextColor(Color.WHITE);
}
}
return v;
}
}
的DataItem:
public class DataItem {
private String name;
private String record;
public DataItem() {
}
public DataItem(String n, String r) {
this.name = n;
this.record = r;
}
public String getname() {
return name;
}
public void setname(String name) {
this.name = name;
}
public String getrecord() {
return record;
}
public void setrecord(String record) {
this.record = record;
}
MainActivity:
listView = (ListView) findViewById(R.id.listView);
DataItem obj1 = new DataItem("Example 1", "$12");
DataItem obj2 = new DataItem("Example 2", "$23");
data.add(obj1);
data.add(obj2);
CustomAdapter adapter = new CustomAdapter(this, R.layout.row, data);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.i("App Info: ", "Clicked!");
}
});
基本上,這裏的問題是,我不能讓我的onItemClickListener工作;當我點擊listView中的一個項目時,沒有任何反應。有誰能夠幫助我?
感謝, KPS
也許你是Android的新手,但很長一段時間沒有人使用ListView,因爲它太粗糙和過時。看看RecyclerView(https://developer.android.com/training/material/lists-cards.html),它更好,更現代,更靈活。 –
它的工作....我已經測試你的代碼..我已經把listview上onItemClick..t烤麪包和吐司出現,當我點擊listview項目...無論問題是我米仍然張貼您的代碼作爲答案。 –
感謝VortexHeatKiller的想法。我其實並不知道RecyclerViews,因爲我接受Udemy的課程從未涉及過他們。謝謝! – kps2501