Android 如何将ImageView与ListView的适配器分离
我有一个带有和适配器的listview,它可以更改所有单个项目的标题TextView和图像ImageView。但我有一个问题,我也有和InputSearchEditText的函数TextChangedListener来搜索列表视图的任何单个项目。但实际上这是TextChangedListener的mi代码:Android 如何将ImageView与ListView的适配器分离,android,android-listview,android-arrayadapter,Android,Android Listview,Android Arrayadapter,我有一个带有和适配器的listview,它可以更改所有单个项目的标题TextView和图像ImageView。但我有一个问题,我也有和InputSearchEditText的函数TextChangedListener来搜索列表视图的任何单个项目。但实际上这是TextChangedListener的mi代码: /** * Enabling Search Filter * */ inputSearch.addTextChangedListener(new TextWat
/**
* Enabling Search Filter
* */
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Alimentacion.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
这在此行中生成一个空指针:Alimentacion.This.adapter.getFilter.filtercs;我认为空指针是因为当我要搜索某个东西时,我只想按标题搜索,但在适配器中,我有标题和图像,所以我想,我会尝试搜索标题和图像,然后返回空指针。
怎么能把这些东西分开只搜索标题呢?
我的完整代码:
**OncreateView of myfragment.xml**列表视图位于片段中
// List view
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
ArrayAdapter<String> adapter2;
// Search EditText
EditText inputSearch;
// Set the Text to try this out
lv = (ListView)myInflatedView.findViewById(R.id.list_view);
inputSearch = (EditText) myInflatedView.findViewById(R.id.inputSearch);
// Listview Data
final String categorias[] = {"1. Huevos y Lacteos", "2. Carnes y Derivados", "3. Pescados y Mariscos", "4. Aceites y grasos", "5. Verduras y hortalizas",
"6. Frutas", "7. Bebidas",
"8. Comida Rapida", "9. Pasta y Cereales", "10. Bolleria y Snacks"};
Integer ImgCategorias[] = {R.drawable.cat1,R.drawable.cat2,R.drawable.cat3,R.drawable.cat4,R.drawable.cat5,R.drawable.cat6
,R.drawable.cat7,R.drawable.cat8,R.drawable.cat9,R.drawable.cat10};
// Pass results to ListViewAdapter Class
CustomList adapter = new
CustomList(this.getActivity(), categorias, ImgCategorias);
// Binds the Adapter to the ListView
lv.setAdapter(adapter);
// OLD ADAPTER (where only change the text, it works to the TextChangeListener)
/* adapter2 = new ArrayAdapter<String>(this.getActivity(), R.layout.adapter_categorias, R.id.TVTitulo, categorias);
lv.setAdapter(adapter2); */
/**
* Enabling Search Filter
* */
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text (LINE OF THE NULLPOINTER)
Alimentacion.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
以及CustomList.java
总之,我希望如果可以,在不同的适配器中放置图像和标题,并且在搜索函数中只放置标题的适配器。或者可以让我毫无错误地搜索标题的东西。。
我看到这个问题:
但我不太明白如何在我的应用程序中实现它
谢谢。我已经修改了下面的代码,现在工作正常。请检查并让我知道:
public class Image {
String categorias;
Integer ImgCategorias;
public String getCategorias() {
return categorias;
}
public void setCategorias(String categorias) {
this.categorias = categorias;
}
public Integer getImgCategorias() {
return ImgCategorias;
}
public void setImgCategorias(Integer imgCategorias) {
ImgCategorias = imgCategorias;
}
}
您的适配器类:
import java.util.ArrayList;
import java.util.Locale;
import android.annotation.SuppressLint;
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.ImageView;
import android.widget.TextView;
public class CustomList extends BaseAdapter {
private Context context;
private ArrayList<Image> data;
private ArrayList<Image> listImage;
public CustomList(Context context, ArrayList<Image> listImage) {
this.context = context;
data = listImage;
this.listImage = new ArrayList<Image>();
this.listImage.addAll(listImage);
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View rowView = inflater
.inflate(R.layout.adapter_categorias, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.TVTitulo);
ImageView imageView = (ImageView) rowView
.findViewById(R.id.IVCategoria);
txtTitle.setText(data.get(position).getCategorias());
imageView.setImageResource(data.get(position).getImgCategorias());
return rowView;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressLint("DefaultLocale")
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
data.clear();
if (charText.trim().length() == 0) {
data.addAll(listImage);
} else {
for (Image wp : listImage) {
if (wp.getCategorias().toLowerCase().contains(charText)) {
data.add(wp);
}
}
}
notifyDataSetChanged();
}
}
活动类别:
import java.util.ArrayList;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class Alimentacion extends Activity {
Context context;
TextView txtTVtexto;
EditText txtInputSearch;
ListView list;
CustomList adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_alimentacion);
context = Alimentacion.this;
txtTVtexto = (TextView) findViewById(R.id.TVtexto);
txtInputSearch = (EditText) findViewById(R.id.inputSearch);
list = (ListView) findViewById(R.id.list_view);
ArrayList<Image> listImage = prepareImageList();
adapter = new CustomList(context, listImage);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id) {
Image image = (Image) list.getItemAtPosition(pos);
Toast.makeText(context, "Name: "+image.getCategorias(), Toast.LENGTH_LONG).show();
}
});
txtInputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
String text = "";
try {
text = txtInputSearch.getText().toString().toLowerCase(Locale.getDefault());
} catch ( Exception e ) {
e.printStackTrace();
}
adapter.filter(text);
}
});
}
private ArrayList<Image> prepareImageList() {
ArrayList<Image> listImage = new ArrayList<Image>();
Image image = new Image();
image.setCategorias("1. Huevos y Lacteos");
image.setImgCategorias(R.drawable.ic_launcher);
listImage.add(image);
image = new Image();
image.setCategorias("2. Carnes y Derivados");
image.setImgCategorias(R.drawable.ic_launcher);
listImage.add(image);
image = new Image();
image.setCategorias("3. Pescados y Mariscos");
image.setImgCategorias(R.drawable.ic_launcher);
listImage.add(image);
image = new Image();
image.setCategorias("4. Aceites y grasos");
image.setImgCategorias(R.drawable.ic_stub);
listImage.add(image);
image = new Image();
image.setCategorias("5. Verduras y hortalizas");
image.setImgCategorias(R.drawable.share);
listImage.add(image);
image = new Image();
image.setCategorias("6. Frutas");
image.setImgCategorias(R.drawable.ic_launcher);
listImage.add(image);
image = new Image();
image.setCategorias("7. Bebidas");
image.setImgCategorias(R.drawable.ic_launcher);
listImage.add(image);
image = new Image();
image.setCategorias("8. Comida Rapida");
image.setImgCategorias(R.drawable.ic_launcher);
listImage.add(image);
image = new Image();
image.setCategorias("9. Pasta y Cereales");
image.setImgCategorias(R.drawable.ic_launcher);
listImage.add(image);
image = new Image();
image.setCategorias("10. Bolleria y Snacks");
image.setImgCategorias(R.drawable.ic_launcher);
listImage.add(image);
return listImage;
}
}
布局:
fragment_alimentacion.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="es.bawi.testdeporte.Alimentacion" >
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/TVtexto"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:gravity="center_horizontal|center_vertical"
android:text="Blank Fragment" />
<!-- Editext for Search -->
<EditText
android:id="@+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/TVtexto"
android:hint="Productos"
android:inputType="textVisiblePassword" />
<!-- List View -->
<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/inputSearch" />
</RelativeLayout>
adapter_categorias.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_light"
android:paddingBottom="2dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:background="@android:color/holo_green_light"
android:layout_height="70dp">
<ImageView
android:id="@+id/IVCategoria"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher"
android:focusable="false" />
<TextView
android:id="@+id/TVTitulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/IVCategoria"
android:text="Categoria"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/TVCalorias"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/TVTitulo"
android:paddingTop="0dp"
android:text="Nº Productos"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</LinearLayout>
这是一个例外,因为:Alimentacion.this.adapter.getFilter.filtercs;评论这行。您需要在PostTextChanged内部编写以下代码:String text=inputSearch..getText.toString.ToLowerCaseScale.getDefault;adapter.filtertext;检查它是否工作否,我有相同的错误,如果你在我的代码中看到我有一个适配器2,如果我放这个适配器它工作,但这个适配器只放标题和图像是相同的所有单个项目。。是否可以选择放置适配器2并更改单个项目的图像?谢谢你的回答。如果可以在dropbox中发送整个代码,那么我可以调试和修复。我最近也做了同样的事情,它只有一个文本和图像,我可以根据它进行搜索。我不能发送所有的项目。。但是在这个zip中是片段的完整oncreateview代码、片段的布局、单一项xml的适配器和CustimList.java。。我可以在这里将其作为活动而不是片段吗?由你决定。现在我可以毫无问题地写作了。但是过滤器不好用,我总是有同样的结果。查看:在getView中更改txtTitle.setTextcategorias.getposition;到txtitle.setTextdata.getposition;如果解决了您的问题,请将答案标记为已解决。好的,我标记为已解决,因为现在可以了,但我有一个小问题,我不知道您是否可以帮助我。现在,当我搜索某个东西时,它会改变图像的外观:例如,数字3。图标是一条鱼,但如果我搜索3。图标更改为蛋编号1:D我想建议只使用一个。我知道为什么会这样。提示:准备一个类来设置图像和名称。然后将ArrayList传递给适配器,而不是单独的数组。它会解决你的问题。如果你做不到,请告诉我。我将更新我的代码ehre.bff。。很抱歉我没有在安卓系统中使用arrayList等。。我不知道该怎么做。。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_light"
android:paddingBottom="2dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:background="@android:color/holo_green_light"
android:layout_height="70dp">
<ImageView
android:id="@+id/IVCategoria"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher"
android:focusable="false" />
<TextView
android:id="@+id/TVTitulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/IVCategoria"
android:text="Categoria"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/TVCalorias"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/TVTitulo"
android:paddingTop="0dp"
android:text="Nº Productos"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</LinearLayout>