基本上我正在尝试使用在数组适配器内部轻敲的项目打开视图。它声明我需要使用
setOnItemClickListener()
我不确定在哪里放置该方法,因为它只适用于listview。
package com.icemalta.dylan.memorybuddy;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.text.SimpleDateFormat;
public class NoteList extends ListActivity {
private static final int ADD_NOTE_REQUEST = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_list);
loadNotes();
}
private void loadNotes() {
// ArrayList<Note> notes = StorageHelper.loadNotes(this);
ArrayList<Note> notes =
new MemoryBuddyContract.MemoryBuddyDbHelper(this).getNotes();
if (notes.size() > 0) {
Note.notes = notes;
ArrayAdapter notesAdapter = new ArrayAdapter(
this,
android.R.layout.simple_list_item_1,
Note.notes
);
this.setListAdapter(notesAdapter);
}
}
public void showAddNoteView(View v) {
Intent intAddNote = new Intent(this, AddNote.class);
this.startActivityForResult(intAddNote, ADD_NOTE_REQUEST);
}
public void showViewNoteView(View v) {
Intent intViewNote = new Intent(this, AddNote.class);
this.startActivityForResult(intViewNote, ADD_NOTE_REQUEST);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ADD_NOTE_REQUEST && resultCode == RESULT_OK) {
String title = data.getStringExtra("title");
String desc = data.getStringExtra("desc");
String dateString = data.getStringExtra("date");
try {
Date date = new SimpleDateFormat("d/M/yyyy H:m").parse(dateString);
Note n = new Note(title, desc, date);
//StorageHelper.saveNotes(this, Note.notes);
new MemoryBuddyContract.MemoryBuddyDbHelper(this).addNote(n);
ArrayAdapter<Note> noteAdapater = new ArrayAdapter(
this,
android.R.layout.simple_list_item_1,
Note.notes
);
this.setListAdapter(noteAdapater);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
}
答案 0 :(得分:1)
只需为listitem创建自定义适配器,然后就可以使用点击监听器,尝试这种方式
public class Myadapter extends ArrayAdapter<String> {
Context context;
int layoutResourceId;
ArrayList<String> data = null;
WeatherHolder holder;
public Myadapter(Context context, int layoutResourceId,
ArrayList<String> data) {
// super(context, layoutResourceId, data, coeff);
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new WeatherHolder();
holder.name = (TextView) row.findViewById(R.id.item_cours_name);
holder.b = (ImageButton) row.findViewById(R.id.button);
holder.b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("Clicked");
}
});
row.setTag(holder);
} else {
holder = (WeatherHolder) row.getTag();
}
holder.b.setTag(holder);
String name1 = data.get(position);
holder.name.setText(name1);
return row;
}
static class WeatherHolder {
TextView name;
ImageButton b;
}
}
答案 1 :(得分:0)
将自定义适配器设为
public class ListAdapter extends ArrayAdapter<SMSData> {
// List context
private final Context context;
// List values
private final List<SMSData> smsList;
public ListAdapter(Context context, List<SMSData> smsList) {
super(context, R.layout.activity_main, smsList);
this.context = context;
this.smsList = smsList;
}
@SuppressLint("ViewHolder")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.activity_main, parent, false);
TextView senderNumber = (TextView) rowView.findViewById(R.id.smsNumberText);
senderNumber.setText(smsList.get(position).getNumber());
return rowView;
}
}
并将其设为..
setListAdapter(new ListAdapter(context, yourlist));
并覆盖方法..活动
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
SMSData sms = (SMSData)getListAdapter().getItem(position);
Toast.makeText(getApplicationContext(), sms.getBody(), Toast.LENGTH_SHORT).show();
}