我创建了自己的ListView,但是有一个问题,我自己的列表不能使用click事件处理程序(onListItemClick)。
源代码:
的 MainActivity.java 的
package com.example.notes;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
public static final String EXTRA_DATE = "Date";
public static final String EXTRA_SHORT = "ShortMessage";
public static final String EXTRA_LONG = "LongMessage";
private ArrayList<NoteItem> list;
//String [] names = {"Test1", "Test2", "Test3"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//ArrayAdapter<String> my = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names);
list = new ArrayList<NoteItem>();
try {
XmlPullParser parser = getResources().getXml(R.xml.notes);
while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equals("note")) {
list.add(new NoteItem(parser.getAttributeValue(0), parser.getAttributeValue(1), parser.getAttributeValue(2)));
}
parser.next();
}
} catch(Throwable t) {
Toast.makeText(this, getResources().getString(R.string.errMsg), Toast.LENGTH_SHORT)
.show();
}
NoteArrayAdapter adapter = new NoteArrayAdapter(this, R.layout.note, list);
setListAdapter(adapter);
Toast.makeText(this, "oncreate", Toast.LENGTH_LONG).show();
//setListAdapter(my);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.d("ListItem", "LIST ITEM CLICKED!!!");
Toast.makeText(this, "Item clicked!", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
的 NoteArrayAdapter.java 的
package com.example.notes;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class NoteArrayAdapter extends ArrayAdapter<NoteItem> {
private Context context;
private List<NoteItem> list;
public NoteArrayAdapter(Context context, int resource,
List<NoteItem> objects) {
super(context, resource, objects);
this.list = objects;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.note, parent, false);
}
TextView textDate = (TextView) rowView.findViewById(R.id.textDate);
TextView textShort = (TextView) rowView.findViewById(R.id.textShort);
textDate.setText(list.get(position).getDate());
textShort.setText(list.get(position).getShortMessage());
return rowView;
//return super.getView(position, convertView, parent);
}
}
的 NoteItem.java 的
package com.example.notes;
public class NoteItem {
private String date;
private String shortMessage;
private String longMessage;
public NoteItem(String date, String shortMessage, String longMessage) {
this.date = date;
this.shortMessage = shortMessage;
this.longMessage = longMessage;
}
public String getDate() {
return date;
}
public String getShortMessage() {
return shortMessage;
}
public String getLongMessage() {
return longMessage;
}
}
的 note.xml 的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<ImageButton
android:id="@+id/editButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@android:drawable/ic_menu_edit"/>
<TextView
android:id="@+id/textDate"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="test"
android:gravity="left|center"/>
<ImageButton
android:id="@+id/deleteButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:src="@android:drawable/btn_dialog"/>
</LinearLayout>
<TextView
android:id="@+id/textShort"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Test"/>
</LinearLayout>
的 notes.xml 的
<?xml version="1.0" encoding="utf-8"?>
<Notes>
<note
date="26.08.1991"
shortMessage="birthday"
longMessage="Maxim Lazarenko birthday"/>
<note
date="04.01.1988"
shortMessage="birthday"
longMessage="Svetlana Rudaya birthday"/>
<note
date="26.08.1991"
shortMessage="birthday"
longMessage="Maxim Lazarenko birthday"/>
<note
date="04.01.1988"
shortMessage="birthday"
longMessage="Svetlana Rudaya birthday"/>
<note
date="26.08.1991"
shortMessage="birthday"
longMessage="Maxim Lazarenko birthday"/>
<note
date="04.01.1988"
shortMessage="birthday"
longMessage="Svetlana Rudaya birthday"/>
<note
date="26.08.1991"
shortMessage="birthday"
longMessage="Maxim Lazarenko birthday"/>
<note
date="04.01.1988"
shortMessage="birthday"
longMessage="Svetlana Rudaya birthday"/>
</Notes>
答案 0 :(得分:1)
为note.xml中的所有可点击组件设置android:focusable="false"
。
你没有得到onListItemClick()的回调,因为这些组件(ImageButton)正在进行click事件。
希望这会有所帮助。
答案 1 :(得分:0)
尝试使用它:
@Override
protected void onListItemClick (ListView l, View v, int position, long id) {
Toast.makeText(this, "Clicked row " + position, Toast.LENGTH_SHORT).show();
}
更多详情请访问此链接: