gpt4 book ai didi

java - Android自定义ListView创建

转载 作者:行者123 更新时间:2023-12-01 13:40:56 27 4
gpt4 key购买 nike

我创建了自己的 ListView,但存在一个问题,即对于我自己的列表,单击事件处理程序(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>

最佳答案

为 note.xml 中的所有可点击组件设置 android:focusable="false"。您不会收到对 onListItemClick() 的回调,因为这些组件(ImageButton)正在接受单击事件。希望这会有所帮助。

关于java - Android自定义ListView创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20782612/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com