搜索显示错误项目的ListView

时间:2018-11-20 11:22:25

标签: android listview search searchview

当我搜索事件时,它可以工作,但是当我单击事件时,即使它更改了ListView,但在ListView上却给了我错误的项目。 我应该怎么做才能使其正常工作?

public class Searchable extends Activity {

ListView lv;
SearchView sv;


String[] names={"Gratis mad på Frederik Bajers vej 7","Gratis æbleskiver på basis","Growstack: Vertical Wednesday #5","Videoredigerings kursus","Kursus i Psykisk førstehjælp - modul 1","Nye øjne på jobsøgningen","Vr-turnering i CS samt racersimulator", "Nytårsfest med bowling og spisning- kom og tag kegler","Temamøde om patenter og immaterielle rettigheder i Aalborg","Stationsnær byfortætning- kan vi få det hele?", "Den Gode, den Onde og den Grundige kode", "Foredrag med Kurt Kragh - Kan du tænke som en morder?", "SAVEUS koncert","Varm jazz på en kold vinterdag", "Julekoncert med trompetisten Per Nielsen i Brødrenmenighedens Krike i Christiansfeld", "Hygge Friday 2nd edition","Filmaften: Fantastiske skabninger 2- Grindelwalds forbrydelser","Boblesmagning- find dine nytårsbobler", "Jan Gintberg: Hemmeligheden bag den gode ide","Netværk og uopfordret jobsøgning", "The Magic og Science","2nd Nordic-Italian Polymer Future workshop" };
int[] images={R.drawable.b1,R.drawable.b2,R.drawable.b3,R.drawable.b4,R.drawable.b5,R.drawable.b6,R.drawable.b7,R.drawable.b8,R.drawable.b9,R.drawable.b10,R.drawable.b11,R.drawable.b12,R.drawable.b13,R.drawable.b14,R.drawable.b15,R.drawable.b16,R.drawable.b17,R.drawable.b18,R.drawable.b19,R.drawable.b20,R.drawable.b21,R.drawable.b22};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.searchable);


    lv=(ListView) findViewById(R.id.listView1);
    sv=(SearchView) findViewById(R.id.searchView1);




    //ADASPTER
    final Adapter adapter=new Adapter(this, getPlayers());
    lv.setAdapter(adapter);


    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(position==0){
                Intent intent= new Intent(view.getContext(),Activity1.class);
                startActivityForResult(intent,0);
                MainActivity.editClick++;
            }
            if(position==1){
                Intent intent= new Intent(view.getContext(),Activity2.class);
                startActivityForResult(intent,1);
                MainActivity.editClick++;
            }
            if(position==2){
                Intent intent= new Intent(view.getContext(),Activity3.class);
                startActivityForResult(intent,2);
                MainActivity.editClick++;
            }
            if(position==3){
                Intent intent= new Intent(view.getContext(),Activity4.class);
                startActivityForResult(intent,3);
                MainActivity.editClick++;
            }
            if(position==4){
                Intent intent= new Intent(view.getContext(),Activity5.class);
                startActivityForResult(intent,4);
                MainActivity.editClick++;
            }
            if(position==5){
                Intent intent= new Intent(view.getContext(),Activity6.class);
                startActivityForResult(intent,5);
                MainActivity.editClick++;
            }
            if(position==6){
                Intent intent= new Intent(view.getContext(),Activity7.class);
                startActivityForResult(intent,6);
                MainActivity.editClick++;
            }
            if(position==7){
                Intent intent= new Intent(view.getContext(),Activity8.class);
                startActivityForResult(intent,7);
                MainActivity.editClick++;
            }
            if(position==8){
                Intent intent= new Intent(view.getContext(),Activity9.class);
                startActivityForResult(intent,8);
                MainActivity.editClick++;
            }
            if(position==9){
                Intent intent= new Intent(view.getContext(),Activity10.class);
                startActivityForResult(intent,9);
                MainActivity.editClick++;
            }
            if(position==10){
                Intent intent= new Intent(view.getContext(),Activity11.class);
                startActivityForResult(intent,10);
                MainActivity.editClick++;
            }
            if(position==11){
                Intent intent= new Intent(view.getContext(),Activity12.class);
                startActivityForResult(intent,11);
                MainActivity.editClick++;
            }
            if(position==12){
                Intent intent= new Intent(view.getContext(),Activity13.class);
                startActivityForResult(intent,12);
                MainActivity.editClick++;
            }
            if(position==13){
                Intent intent= new Intent(view.getContext(),Activity14.class);
                startActivityForResult(intent,13);
                MainActivity.editClick++;
            }
            if(position==14){
                Intent intent= new Intent(view.getContext(),Activity15.class);
                startActivityForResult(intent,14);
                MainActivity.editClick++;
            }
            if(position==15){
                Intent intent= new Intent(view.getContext(),Activity16.class);
                startActivityForResult(intent,15);
                MainActivity.editClick++;
            }
            if(position==16){
                Intent intent= new Intent(view.getContext(),Activity17.class);
                startActivityForResult(intent,16);
                MainActivity.editClick++;
            }
            if(position==17){
                Intent intent= new Intent(view.getContext(),Activity18.class);
                startActivityForResult(intent,17);
                MainActivity.editClick++;
            }
            if(position==18){
                Intent intent= new Intent(view.getContext(),Activity19.class);
                startActivityForResult(intent,18);
                MainActivity.editClick++;
            }
            if(position==19){
                Intent intent= new Intent(view.getContext(),Activity20.class);
                startActivityForResult(intent,19);
                MainActivity.editClick++;
            }
            if(position==20){
                Intent intent= new Intent(view.getContext(),Activity21.class);
                startActivityForResult(intent,20);
                MainActivity.editClick++;
            }
            if(position==21){
                Intent intent= new Intent(view.getContext(),Activity22.class);
                startActivityForResult(intent,21);
                MainActivity.editClick++;
            }
        }
    });

    sv.setOnQueryTextListener(new OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String arg0) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean onQueryTextChange(String query) {
            // TODO Auto-generated method stub

            adapter.getFilter().filter(query);

            return false;
        }
    });
}


private ArrayList<ArrayListe> getPlayers()
{
    ArrayList<ArrayListe> ArrayLister=new ArrayList<ArrayListe>();
    ArrayListe p;

    for(int i=0;i<names.length;i++)
    {
        p=new ArrayListe(names[i], images[i]);
        ArrayLister.add(p);

    }

    return ArrayLister;
}



}

和布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorGray"
    tools:context=".MainActivity">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@color/colorWhite">


        <android.support.v7.widget.CardView
            android:id="@+id/cardView"
            android:layout_width="327dp"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="13dp"
            android:clickable="true"
            app:cardBackgroundColor="@color/colorGray"

            app:cardCornerRadius="10dp"
            app:cardUseCompatPadding="true"
            tools:layout_editor_absoluteX="31dp"
            tools:layout_editor_absoluteY="0dp">

            <SearchView
                android:id="@+id/searchView1"
                android:layout_width="wrap_content"
                android:layout_height="32dp"
                android:layout_alignRight="@+id/listView1"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:clickable="true"
                android:iconifiedByDefault="false"
                android:paddingLeft="-15dp"
                android:queryHint="Søg..">

            </SearchView>
        </android.support.v7.widget.CardView>
    </RelativeLayout>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="60dp"
        android:background="@color/colorGray"
        android:backgroundTint="@color/colorGray"
        android:divider="@color/colorGray"
        android:dividerHeight="5dp" />


</RelativeLayout

............................................... ....................... 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。

1 个答案:

答案 0 :(得分:0)

问题已解决。 使用代码:

在arraylist中。

    @Override
    public String toString() {
        return name;
    }

}

在我的可搜索内容中: 添加了这一行。

final Adapter adapter=new Adapter(this, getPlayers());
lv.setAdapter(adapter);


lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //ArrayListe item= (ArrayListe) Adapter.getItem(position);

        String name = lv.getItemAtPosition(position).toString();

        for (int index = 0; index < names.length; index++){
            if (name.equals(names[index])) {
                position = index;
                break;
            }
        }

        if(position==0){
            startActivity(new Intent(view.getContext(), Activity1.class));
            MainActivity.editClick++;
        }