我在这里绑定,我想在我的CustomAdapter中集成OnItemClickListener,所以当点击某个项目时,它会显示一些消息或其他内容。但我不知道在哪里设置它。我看过一堆教程和一些帖子,但这只会让我更加困惑。因此,我需要指导如何以及在何处设置它以使其工作。这是我的代码:
MainActivity.java
ListView listView;
listView = (ListView) findViewById(R.id.list_item);
new ReadRSS(MainActivity.this, listView, "http://malaysiakini.com/en/news.rss").execute();
ReadRSS.java
@Override
protected void onPostExecute(Void aVoid) {
//Dismiss progress dialog
super.onPostExecute(aVoid);
progressDialog.dismiss();
if (feedItems != null) {
CustomAdapter customAdapter = new CustomAdapter(context,R.layout.activity_listview,feedItems);
listView.setAdapter(customAdapter);
}
}
CustomAdapter.java
public class CustomAdapter extends ArrayAdapter<FeedItem> {
public CustomAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public CustomAdapter(Context context, int resource, List<FeedItem> items) {
super(context, resource, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.activity_listview, null);
}
FeedItem p = getItem(position);
if (p != null) {
TextView tt1 = (TextView) v.findViewById(R.id.title_text);
TextView tt2 = (TextView) v.findViewById(R.id.date_text);
if (tt1 != null) {
tt1.setText(p.getTitle());
}
if (tt2 != null) {
tt2.setText(p.getPubDate());
}
}
return v;
}
}
答案 0 :(得分:2)
这不是您问题的直接答案,而是我自己的代码示例,因此您可以查看它并获得想法;
ListAdapter adapter = new ArrayAdapter<String>(this,R.layout.listword,R.id.wordView1, companies);
ListView listView = (ListView) findViewById(R.id.secondListView1);
listView.setAdapter(adapter);
// Item Click listener
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> ok, View v, int position, long id){
// Do your stuff here
}
});
答案 1 :(得分:0)
您没有在适配器内设置它,但在您定义ListView的活动中:
print(max(abs(NewCont - NewCont2)))
##[1] 4.547474e-13
答案 2 :(得分:0)
就像这样,希望它有所帮助:
public class CustomAdapter extends ArrayAdapter<FeedItem> implements OnClickListener{
private Context mContext;
public CustomAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
mContext = context;
}
public CustomAdapter(Context context, int resource, List<FeedItem> items) {
super(context, resource, items);
mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.activity_listview, null);
}
FeedItem p = getItem(position);
v.setTag(p);
v.setOnClickListener(this);
if (p != null) {
TextView tt1 = (TextView) v.findViewById(R.id.title_text);
TextView tt2 = (TextView) v.findViewById(R.id.date_text);
if (tt1 != null) {
tt1.setText(p.getTitle());
}
if (tt2 != null) {
tt2.setText(p.getPubDate());
}
}
return v;
}
@Override
public void onClick(View v){
FeedItem p = (FeedItem) v.getTag();
// TODO,do something with p and mContext
}
}
但我认为你应该使用listview.setOnItemClickListener(listener)
来完成这项工作。
答案 3 :(得分:0)
您可以对CustomAdapter类的各个组件执行任务。检查以下代码。
public class CustomAdapter extends ArrayAdapter<FeedItem> {
public CustomAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public CustomAdapter(Context context, int resource, List<FeedItem> items) {
super(context, resource, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.activity_listview, null);
}
FeedItem p = getItem(position);
if (p != null) {
TextView tt1 = (TextView) v.findViewById(R.id.title_text);
TextView tt2 = (TextView) v.findViewById(R.id.date_text);
if (tt1 != null) {
tt1.setText(p.getTitle());
}
if (tt2 != null) {
tt2.setText(p.getPubDate());
}
tt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Perform your task
});
tt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Perform your task
});
}
return v;
}
}
答案 4 :(得分:0)
将此代码写在您的set adapter方法下面的活动中。
设置适配器方法
private void setAdapter() {
try {
DoctorAppointmentListAdapter doctorAppointmentListAdapter = new DoctorAppointmentListAdapter(getContext(), doctorAppointmentObjectArrayList, selectedItemClickListener, screen);
appointmentListView.setAdapter(doctorAppointmentListAdapter);
}catch (Exception e){}
}
服装点击方法
//ListAdapter Click Listener
DoctorAppointmentListAdapter.SelectItemClickListener selectedItemClickListener = new DoctorAppointmentListAdapter.SelectItemClickListener() {
@Override
public void itemClickedAtIndex(int index, String operation) {
if (operation.equalsIgnoreCase("00")) {
m_handler.removeCallbacks(m_handlerTask);
viewDetailsScreen(index);
} else if (operation.equalsIgnoreCase("11")) {
m_handler.removeCallbacks(m_handlerTask);
//Video Call Operation
patirntId = doctorAppointmentObjectArrayList.get(index).getAppointment_patient_id();
videocallService(doctorAppointmentObjectArrayList.get(index).getAppointment_patient_id(), doctorAppointmentObjectArrayList.get(index).getAppointment_id());
} else if (operation.equalsIgnoreCase("33")) {
m_handler.removeCallbacks(m_handlerTask);
//Video Call Operation
patirntId = doctorAppointmentObjectArrayList.get(index).getAppointment_patient_id();
videocallCompletedService(doctorAppointmentObjectArrayList.get(index).getAppointment_patient_id(), doctorAppointmentObjectArrayList.get(index).getAppointment_id());
} else if (operation.equalsIgnoreCase("22")) {
m_handler.removeCallbacks(m_handlerTask);
//Phone Call Operation
if (doctorAppointmentObjectArrayList.get(index).getPatient_contact_no().length() > 0) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + doctorAppointmentObjectArrayList.get(index).getPatient_contact_no()));
startActivity(intent);
} else {
}
}
}
};
这是适配器类
public class DoctorAppointmentListAdapter extends BaseAdapter {
private LayoutInflater inflater = null;
private Context context;
private int screen;
private ArrayList<DoctorAppointmentObject> doctorAppointmentObjectArrayList;
private SelectItemClickListener itemClickListener;
public DoctorAppointmentListAdapter(Context context, ArrayList<DoctorAppointmentObject> doctorAppointmentObjectArrayList, SelectItemClickListener listener, int screen) {
this.context = context;
this.screen = screen;
this.doctorAppointmentObjectArrayList = doctorAppointmentObjectArrayList;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
try {
if (itemClickListener != null) {
itemClickListener = null;
}
itemClickListener = listener;
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getCount() {
return doctorAppointmentObjectArrayList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
public interface SelectItemClickListener {
public void itemClickedAtIndex(int index, String operation);
}
private class ViewHolder {
private TextView doctorNameTextView, numberTextView, datetimeTextView, enteryTextView;
private TextView viewDetailTextView, videoCallTextView, phoneCallTextView;
private CircleImageView profile_image;
private ImageView onlineStatusImageView;
private CircularProgressView progress_view;
}
ViewHolder holder = null;
@Override
public View getView(final int i, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.adapter_doctor_appointment_list, parent, false);
holder = new ViewHolder();
holder.doctorNameTextView = (TextView) convertView
.findViewById(R.id.doctorNameTextView);
holder.numberTextView = (TextView) convertView
.findViewById(R.id.numberTextView);
holder.datetimeTextView = (TextView) convertView
.findViewById(R.id.datetimeTextView);
holder.enteryTextView = (TextView) convertView
.findViewById(R.id.enteryTextView);
holder.viewDetailTextView = (TextView) convertView
.findViewById(R.id.viewDetailTextView);
holder.videoCallTextView = (TextView) convertView
.findViewById(R.id.videoCallTextView);
holder.phoneCallTextView = (TextView) convertView
.findViewById(R.id.phoneCallTextView);
holder.profile_image = (CircleImageView) convertView
.findViewById(R.id.profile_image);
holder.progress_view = (CircularProgressView) convertView.findViewById(R.id.progress_view);
holder.onlineStatusImageView = (ImageView) convertView.findViewById(R.id.onlineStatusImageView);
holder.onlineStatusImageView.setVisibility(View.INVISIBLE);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (screen == 1) {
holder.videoCallTextView.setVisibility(View.GONE);
}
try {
Picasso.with(context).load(doctorAppointmentObjectArrayList.get(i).getPatient_profile_pic()).into(holder.profile_image, new Callback() {
@Override
public void onSuccess() {
holder.progress_view.setVisibility(View.GONE);
}
@Override
public void onError() {
holder.progress_view.setVisibility(View.GONE);
holder.profile_image.setImageResource(R.mipmap.ic_launcher);
}
});
} catch (Exception e) {
holder.profile_image.setImageResource(R.mipmap.ic_launcher);
holder.progress_view.setVisibility(View.GONE);
e.printStackTrace();
}
holder.doctorNameTextView.setText(doctorAppointmentObjectArrayList.get(i).getPatient_display_name());
holder.numberTextView.setText(doctorAppointmentObjectArrayList.get(i).getPatient_contact_no());
if (doctorAppointmentObjectArrayList.get(i).getAppointment_time().equalsIgnoreCase("")) {
holder.numberTextView.setText("");
} else {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date testDate = null;
try {
testDate = sdf.parse(doctorAppointmentObjectArrayList.get(i).getAppointment_time());
} catch (Exception ex) {
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy hh:mm a");
String newFormat = formatter.format(testDate);
holder.numberTextView.setText("" + newFormat);
}
holder.datetimeTextView.setText(doctorAppointmentObjectArrayList.get(i).getAppointment_reason());
if (screen==0){
holder.enteryTextView.setText("Entry Time :" + doctorAppointmentObjectArrayList.get(i).getAppointment_entry_time());
}else {
holder.enteryTextView.setText("");
}
holder.viewDetailTextView.setId(i);
holder.viewDetailTextView.setOnClickListener(viewDetailSelectedListener);
holder.videoCallTextView.setId(i);
holder.videoCallTextView.setOnClickListener(videoCallSelectedListener);
holder.phoneCallTextView.setId(i);
holder.phoneCallTextView.setOnClickListener(phoneCallSelectedListener);
return convertView;
}
View.OnClickListener viewDetailSelectedListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
int index = (Integer) v.getId();
itemClickListener.itemClickedAtIndex(index, "00");
} catch (Exception e) {
e.printStackTrace();
}
}
};
View.OnClickListener videoCallSelectedListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
int index = (Integer) v.getId();
if (screen == 2) {
itemClickListener.itemClickedAtIndex(index, "33");
} else {
itemClickListener.itemClickedAtIndex(index, "11");
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
View.OnClickListener phoneCallSelectedListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
int index = (Integer) v.getId();
itemClickListener.itemClickedAtIndex(index, "22");
} catch (Exception e) {
e.printStackTrace();
}
}
};
}