Original post: 我有一個ListView與圖像和文本和一個TextView按文本過濾行。 當我運行我的應用程序時,它不顯示圖像,而是顯示對象引用的文本。ListView帶圖像和文本文本過濾
帖子編輯: 我已經更新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;
}
}
MeetingAdapter.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;
}
}
MeetingFilter.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();
}
}
Meeting.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;
}
}
我
我有更新MainActivity.java和我添加了兩個新類MeetingAdapter.java和MeetingFilter.java現在的問題是,我的應用程序沒有根據搜索到的文本篩選結果 – 2013-03-27 23:36:03
@JohnBe這是另一個問題,我建議你爲這個問題開一個新的問題。 – NullPointer 2013-03-28 08:17:56