我创建了一个导航抽屉,它工作正常,列表视图的所有属性都显示但是当我实现OnItemClickListener时,它没有执行操作
Toast.makeText(getApplicationContext(), array[position] + "is selected", Toast.LENGTH_SHORT).show();
selectItem(position);
这是完整的代码,
package com.hamza.aurasalon;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnItemClickListener ,OnClickListener {
private DrawerLayout drawerLayout;
private ListView listView1;
//,listView2;
String[] array = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpAttributes();
}
private void setUpAttributes() {
listView1= (ListView) findViewById(R.id.drawerList1);
//listView2= (ListView) findViewById(R.id.drawerList2);
array = getResources().getStringArray(R.array.mumtazFeatures1);
listView1.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, array));
listView1.setOnItemClickListener(this);
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), array[position] + "is selected", Toast.LENGTH_SHORT).show();
selectItem(position);
}
xml代码,
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="@+id/drawerFrame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:background="#FF8800"
android:id="@+id/drawerList1"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left">
</ListView>
<ListView
android:id="@+id/drawerList2"
android:background="#FFCC00"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:entries="@array/mumtazFeatures2" >
</ListView>
</android.support.v4.widget.DrawerLayout>
答案 0 :(得分:0)
试试这个:
listView1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), array[position] + "is selected", Toast.LENGTH_SHORT).show();
selectItem(position);
}
});