我在使用searchbox时如何在Listview上使用intent

时间:2017-04-24 23:07:46

标签: java android android-layout listview android-intent

我的代码存在问题,但仍然对每个项目都有自己的布局的意图感到困惑,就像产品有自己的描述一样 有谁知道如何解决我的问题?谢谢你

这是我的main_activity代码

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;

import static com.example.vinznolimit.herbindonesia.R.id.theList;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private ArrayAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView list= (ListView) findViewById(theList);
        EditText theFilter = (EditText) findViewById(R.id.searchFilter);
        Log.d(TAG, "onCreate: Started.");

        ArrayList<String> names = new ArrayList<>();
        names.add("Mitch");
        names.add("Blake");
        names.add("Blade");
        names.add("Bruno");
        names.add("Joe");
        names.add("Barry");

        adapter = new ArrayAdapter(this,R.layout.list_item,names);
        list.setAdapter(adapter);



theFilter.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                (MainActivity.this).adapter.getFilter().filter(s);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });



    }
}

1 个答案:

答案 0 :(得分:0)

试试这个

list.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v,
        int position, long id) {
        Intent myIntent = new Intent(v.getContext(), UserSubmissionLog.class);
        startActivity(myIntent);

        }
    }
);