原帖: 我有一个带有图像和文本的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;
}
}
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;
}
}
我
答案 0 :(得分:2)
正如上面提到的@Pragnani,您需要创建一个这样的自定义适配器:
public class customAdapter extends BaseAdapter
{
public int getCount()
{
return list.length;
}
public Object getItem(int itemData)
{
return null;
}
public long getItemId(int itemPosition)
{
return itemPosition;
}
public View getView(int position, View mainView, ViewGroup mainViewGroup)
{
LayoutInflater maniInFlater = getLayoutInflater();
View mRow = maniInFlater.inflate(R.layout.yourlayout_layout, mainViewGroup, false);
TextView sampleText = (TextView) mRow.findViewById(R.id.sample_id);
return mRow;
}
}
然后将其设置为ListView
适配器:
lv.setAdapter(new customAdapter());
答案 1 :(得分:1)
因为您只在TextView
中使用ArrayAdapter
即
arrayAdapter = new ArrayAdapter<Meeting>(this, R.layout.meeting_list_item, R.id.meeting_name, meetings);
您正在尝试仅在TextView中打印会议名称,ArrayAdapter将使用TextView作为资源来填充列表中的数据。
所以try to take a custom Adapter which extends ArrayAdapter or Base Adapter
或只是使用SimpleAdapter