0

大家好。我创建了一个带有图像、文本和搜索框的列表视图。这是我的代码:java 和布局。所以,我需要一些帮助。我不知道如何让我的搜索框开始工作。我需要一个代码。

我的 MainActivity.java

package android.codigos;

import android.app.Activity;
import android.codigos.objetos.ItemListView;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;



public class MainActivity extends Activity implements OnItemClickListener {

private ListView listView;
private EditText ed;
private AdapterListView adapterListView;
private ArrayList<ItemListView> itens;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //carrega o layout onde contem o ListView
    setContentView(R.layout.main);

    //Pega a referencia do ListView
    listView = (ListView) findViewById(R.id.list);
    ed = (EditText)findViewById(R.id.EditText01);
    //Define o Listener quando alguem clicar no item.
    listView.setOnItemClickListener(this);

    createListView();
}

private void createListView() {
    //Criamos nossa lista que preenchera o ListView
    itens = new ArrayList<ItemListView>();
    ItemListView item1 = new ItemListView("Bahia", R.drawable.bahia);
    ItemListView item2 = new ItemListView("Minas Gerais", R.drawable.minasgerais);
    ItemListView item3 = new ItemListView("Rio de Janeiro", R.drawable.riodejaneiro);
    ItemListView item4 = new ItemListView("São Paulo", R.drawable.saopaulo);

    itens.add(item1);
    itens.add(item2);
    itens.add(item3);
    itens.add(item4);

    //Cria o adapter
    adapterListView = new AdapterListView(this, itens);

    //Define o Adapter
    listView.setAdapter(adapterListView);
    //Cor quando a lista é selecionada para rolagem.
    listView.setCacheColorHint(Color.TRANSPARENT);
}

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    //Pega o item que foi selecionado.
    ItemListView item = adapterListView.getItem(arg2);
    //Demostração
    Toast.makeText(this, "Você Clicou em: " + item.getTexto(),    Toast.LENGTH_LONG).show();
}

}

AdapterListView.java

package android.codigos;

import android.codigos.objetos.ItemListView;
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;
import java.util.ArrayList;

/**
*
*/
public class AdapterListView extends BaseAdapter {

private LayoutInflater mInflater;
private ArrayList<ItemListView> itens;

public AdapterListView(Context context, ArrayList<ItemListView> itens) {
    //Itens que preencheram o listview
    this.itens = itens;
    //responsavel por pegar o Layout do item.
    mInflater = LayoutInflater.from(context);
}

/**
 * Retorna a quantidade de itens
 *
 * @return
 */
public int getCount() {
    return itens.size();
}

/**
 * Retorna o item de acordo com a posicao dele na tela.
 *
 * @param position
 * @return
 */
public ItemListView getItem(int position) {
    return itens.get(position);
}

/**
 * Sem implementação
 *
 * @param position
 * @return
 */
public long getItemId(int position) {
    return position;
}

public View getView(int position, View view, ViewGroup parent) {
    //Pega o item de acordo com a posção.
    ItemListView item = itens.get(position);
    //infla o layout para podermos preencher os dados
    view = mInflater.inflate(R.layout.item_list, null);

    //atravez do layout pego pelo LayoutInflater, pegamos cada id relacionado
    //ao item e definimos as informações.
    ((TextView) view.findViewById(R.id.text)).setText(item.getTexto());
    ((ImageView)  view.findViewById(R.id.imagemview)).setImageResource(item.getIconeRid());

    return view;
}

}

项目列表视图.java

package android.codigos.objetos;

/**
*
*/
public class ItemListView {

private String texto;
private int iconeRid;

public ItemListView() {
}

public ItemListView(String texto, int iconeRid) {
    this.texto = texto;
    this.iconeRid = iconeRid;
}

public int getIconeRid() {
    return iconeRid;
}

public void setIconeRid(int iconeRid) {
    this.iconeRid = iconeRid;
}

public String getTexto() {
    return texto;
}

public void setTexto(String texto) {
    this.texto = texto;
}

}

主.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#FFFFFFFF"
>    

    <EditText
android:id="@+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Type here to search."
>

</EditText>


    <ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="#ECECEC"
    android:dividerHeight="1sp"
    />    
</LinearLayout>

item_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="5sp">
    <ImageView
        android:id="@+id/imagemview"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        />

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="5sp"
        android:gravity="center_vertical"
        android:textColor="#FF000000"
    />
</LinearLayout>
</LinearLayout>
4

1 回答 1

1

一般来说,算法如下:

  1. 从 EditText 获取文本。
  2. 从 ArrayList 中删除所有与搜索查询不匹配的项目。
  3. 使用方法 adapter.notifyDataSetChanged() 刷新您的适配器

如果您需要源代码示例,我可以附上。

于 2012-08-02T14:39:26.520 回答