原始帖子:我有一个带有图像和文本的 ListView 和一个 TextView 来按文本过滤行。当我运行我的应用程序时,它不显示图像,而是显示对象引用而不是文本。
编辑后:我更新了 MainActivity.java 并添加了两个新类 MeetingAdapter.java 和 MeetingFilter.java 现在问题是我的应用程序没有根据搜索的文本过滤结果
main_activity_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Meeting name filter -->
<EditText
android:id="@+id/find_meeting_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/find_meeting_name_hint"
android:inputType="text" />
<!-- Meeting list -->
<ListView
android:id="@+id/meeting_name_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
meeting_list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<!-- Meeting image -->
<ImageView
android:id="@+id/meeting_image"
android:layout_width="48dip"
android:layout_height="48dip"
android:contentDescription="@string/meeting_image_description" />
<!-- Meeting name -->
<TextView android:id="@+id/meeting_name"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1.0"
android:textIsSelectable="true"
android:textSize="12sp" />
MainActivity.java
import java.util.ArrayList;
import java.util.Date;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
private EditText findMeetingName;
private MeetingAdapter meetingAdapter;
private ListView meetingNameList;
private ArrayList<Meeting> meetings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity_layout);
findMeetingName = (EditText) findViewById(R.id.find_meeting_name);
findMeetingName.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
MainActivity.this.meetingAdapter.getFilter().filter(charSequence);
meetingAdapter.notifyDataSetChanged();
}
});
meetings = new ArrayList<Meeting>();
Meeting meeting = null;
for (int i = 0; i < 5; i++) {
meeting = new Meeting();
meeting.setId(i + 1);
meeting.setName("Cita #" + (i + 1));
meeting.setDescription("Descripción #" + (i + 1));
meeting.setImage(BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.icon48x48_1));
meeting.setInitialDate(new Date());
meetings.add(meeting);
}
meetingAdapter = new MeetingAdapter(this, meetings);
meetingNameList = (ListView) findViewById(R.id.meeting_name_list);
meetingNameList.setAdapter(meetingAdapter);
}
@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;
}
}
会议适配器.java
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
public class MeetingAdapter extends BaseAdapter implements Filterable {
LayoutInflater inflater;
ArrayList<Meeting> meetings;
public MeetingAdapter(Activity activity, ArrayList<Meeting> meetings) {
this.meetings = meetings;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return meetings.size();
}
@Override
public Object getItem(int position) {
return meetings.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup mainViewGroup) {
View mRow = inflater.inflate(R.layout.meeting_list_item, mainViewGroup, false);
// Se setean los valores para cada componente de la lista
TextView meetingName = (TextView) mRow.findViewById(R.id.meeting_name);
meetingName.setText(meetings.get(position).getName());
ImageView meetingImage = (ImageView) mRow.findViewById(R.id.meeting_image);
meetingImage.setImageBitmap(meetings.get(position).getImage());
return mRow;
}
@Override
public Filter getFilter() {
MeetingFilter filter = new MeetingFilter(meetings);
return filter;
}
}
会议过滤器.java
import java.util.ArrayList;
import android.widget.Filter;
public class MeetingFilter extends Filter {
//MeetingAdapter meetingAdapter;
ArrayList<Meeting> meetings;
public MeetingFilter(ArrayList<Meeting> meetings){
//this.meetingAdapter = meetingAdapter;
this.meetings = meetings;
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filteredResults = new FilterResults();
if(constraint.length() == 0) {
filteredResults.values = meetings;
filteredResults.count = meetings.size();
return filteredResults;
}
ArrayList<Meeting> filteredMeetings = new ArrayList<Meeting>();
for (Meeting meeting : meetings) {
// TODO Revisar problemas de internacionalización
if(meeting.getName().toLowerCase().contains(constraint.toString().toLowerCase())) {
filteredMeetings.add(meeting);
}
}
filteredResults.values = filteredMeetings;
filteredResults.count = filteredMeetings.size();
return filteredResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
meetings = (ArrayList<Meeting>) results.values;
//meetingAdapter.notifyDataSetChanged();
}
}
会议.java
import java.util.Date;
import android.graphics.Bitmap;
public class Meeting {
private int id;
private String name;
private String description;
private Bitmap image;
private Date initialDate;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public Date getInitialDate() {
return initialDate;
}
public void setInitialDate(Date initialDate) {
this.initialDate = initialDate;
}
}
一世