如何使ListView项目可点击?

时间:2016-02-23 04:10:25

标签: java android listview arraylist

我想知道如何使ListView项目可点击。当按下项目时,它会将您发送到另一个活动。

这是我的java编码:

public class Cantos extends AppCompatActivity {

ListView lv;
SearchView sv;
String[] cantos={"1: Abre Tu Oido", "2: A Cristo Quiero", "3: Acerquese Mi Clamor", "4: A Cristo Yo Alabare",
        "5: Acude Dios", "6: Adelante", "7: A Dios Canto", "8: Adios Para Siempre", "9: Ahora Senor", "10: A Jesucristo Ven",
        "11: Alabad A Dios" };
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cantos);


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setLogo(R.mipmap.ic_launcher);
    toolbar.setTitle("Cantos");


    lv = (ListView) findViewById(R.id.listView);
    sv = (SearchView) findViewById(R.id.searchView);

    adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1, cantos);
    lv.setAdapter(adapter);

    sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String text) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String text) {

            adapter.getFilter().filter(text);

            return false;
        }
    });
}
}

这是XML编码:

<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:background="#E0E0E0"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.ccb.lldm.lldmhimnario.Cantos">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:elevation="25dp"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimaryDark"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

<SearchView
    android:layout_width="match_parent"
    android:id="@+id/searchView"
    android:queryHint="Busque Canto..."
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">
</SearchView>

<ListView
    android:layout_width="match_parent"
    android:id="@+id/listView"
    android:layout_height="wrap_content"
    android:layout_below="@+id/searchView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true">

</ListView>


</RelativeLayout>

提前谢谢你。 P.s如何更改ListView项目和颜色的字体? 谢谢!

2 个答案:

答案 0 :(得分:1)

使用此代码制作可点击的列表视图。

lv = (ListView) findViewById(R.id.listView);
  lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                 public void onItemClick(AdapterView <? > arg0, View view, int position, long id) {
                     // When clicked, show a toast with the TextView text
                     Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
                         Toast.LENGTH_SHORT).show();

                 }

             });

答案 1 :(得分:1)

使用listview的 OnItemClickListener

lv.setOnItemClickListener(new OnItemClickListener()
   {
      @Override
      public void onItemClick(AdapterView<?> adapter, View v, int position,
            long arg3) 
      {
            // based on the item clicked go to the relevant activity
            String clickedItem = (String)adapter.getItemAtPosition(position);
      }
   });