首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ListView setOnItemClickListener没有响应

ListView setOnItemClickListener没有响应
EN

Stack Overflow用户
提问于 2016-08-28 07:20:07
回答 3查看 67关注 0票数 2

在我的应用程序中,我使用一个抽屉布局,里面有一个ListView和RelativeLayout作为汉堡菜单。当单击ListView上的项时,我希望更改为新活动。但是,当我单击ListView上的一个项时,什么都没有发生。我的代码:

activity_main.xml:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:id="@+id/navList"
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="left|start"
    android:background="#ffeeeeee"/>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="b.calvin.com.dirtymoney.MainActivity"
android:focusable="false"
android:focusableInTouchMode="false">


<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/hourlyRate"
    android:layout_toRightOf="@+id/enterHourlyRate"
    android:layout_centerVertical="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enter Hourly Rate:"
    android:id="@+id/enterHourlyRate"
    android:layout_centerVertical="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Make It Rain"
    android:id="@+id/start"
    android:layout_below="@+id/hourlyRate"
    android:layout_toLeftOf="@+id/stop"
    android:layout_toStartOf="@+id/stop"
    android:layout_marginRight="37dp"
    android:layout_marginEnd="37dp"
    android:layout_marginTop="77dp"
    android:visibility="invisible"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Stop"
    android:id="@+id/stop"
    android:layout_alignBottom="@+id/start"
    android:layout_alignRight="@+id/hourlyRate"
    android:layout_alignEnd="@+id/hourlyRate"
    android:layout_marginRight="42dp"
    android:layout_marginEnd="42dp"
    android:layout_alignTop="@+id/start"
    android:visibility="invisible"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Ok"
    android:id="@+id/confirmHourlyRate"
    android:layout_below="@+id/hourlyRate"
    android:layout_alignParentRight="true"/>

<Chronometer
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/chronometer"
    android:layout_above="@+id/stop"
    android:layout_alignLeft="@+id/start"
    android:layout_alignStart="@+id/start" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="$$ Money"
    android:id="@+id/money"
    android:layout_below="@+id/start"
    android:layout_toLeftOf="@+id/stop"
    android:layout_toStartOf="@+id/stop" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Edit"
    android:id="@+id/edit"
    android:layout_below="@+id/hourlyRate"
    android:layout_toLeftOf="@+id/confirmHourlyRate"
    android:layout_toStartOf="@+id/confirmHourlyRate"
    android:visibility="invisible"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Day: $"
    android:id="@+id/dayText"
    android:layout_alignParentTop="true"
    android:layout_alignLeft="@+id/edit"
    android:layout_alignStart="@+id/edit" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Week: $"
    android:id="@+id/weekText"
    android:layout_below="@+id/dayText"
    android:layout_alignLeft="@+id/dayText"
    android:layout_alignStart="@+id/dayText" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Month: $"
    android:id="@+id/monthText"
    android:layout_below="@+id/weekText"
    android:layout_alignLeft="@+id/weekText"
    android:layout_alignStart="@+id/weekText" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Year: $"
    android:id="@+id/yearText"
    android:layout_below="@+id/monthText"
    android:layout_alignLeft="@+id/monthText"
    android:layout_alignStart="@+id/monthText" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Total: $"
    android:id="@+id/totalText"
    android:layout_below="@+id/yearText"
    android:layout_alignLeft="@+id/yearText"
    android:layout_alignStart="@+id/yearText"
    android:textStyle="bold" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:id="@+id/dayMoney"
    android:layout_toRightOf="@+id/dayText"
    android:layout_alignTop="@+id/dayText"
    android:layout_alignBottom="@+id/dayText"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:id="@+id/weekMoney"
    android:layout_toRightOf="@+id/weekText"
    android:layout_alignTop="@+id/weekText"
    android:layout_alignBottom="@+id/weekText"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:id="@+id/monthMoney"
    android:layout_toRightOf="@+id/monthText"
    android:layout_alignTop="@+id/monthText"
    android:layout_alignBottom="@+id/monthText"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:id="@+id/yearMoney"
    android:layout_toRightOf="@+id/yearText"
    android:layout_alignTop="@+id/yearText"
    android:layout_alignBottom="@+id/yearText"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:id="@+id/totalMoney"
    android:layout_toRightOf="@+id/totalText"
    android:layout_alignTop="@+id/totalText"
    android:layout_alignBottom="@+id/totalText"/>

MainActivity.java:

代码语言:javascript
复制
public class MainActivity extends AppCompatActivity {

private static final String TAG = "BroadcastTest";
private Intent intent;

private Button start;
private Button stop;
private Button confirmRate;
private Button edit;
private EditText hourlyRate;
private TextView money;
private TextView dayMoney;
private TextView weekMoney;
private TextView monthMoney;
private TextView yearMoney;
private TextView totalMoney;
private double moneyEarned;
private double secondsRate;
private double hrlyRate;
private double currentDayMoney;
private double currentWeekMoney;
private double currentMonthMoney;
private double currentYearMoney;
private double currentTotalMoney;
private String savedDayMoney;
private String savedWeekMoney;
private String savedMonthMoney;
private String savedYearMoney;
private String savedTotalMoney;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private String mActivityTitle;
private ListView mDrawerList;
private ArrayAdapter<String> mAdapter;
public static final String PREFS_NAME = "MyPrefsFile";

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

    mDrawerList = (ListView)findViewById(R.id.navList);
    start = (Button) findViewById(R.id.start);
    stop = (Button) findViewById(R.id.stop);
    confirmRate = (Button) findViewById(R.id.confirmHourlyRate);
    edit = (Button) findViewById(R.id.edit);
    hourlyRate = (EditText) findViewById(R.id.hourlyRate);
    money = (TextView) findViewById(R.id.money);
    dayMoney = (TextView) findViewById(R.id.dayMoney);
    weekMoney = (TextView) findViewById(R.id.weekMoney);
    monthMoney = (TextView) findViewById(R.id.monthMoney);
    yearMoney = (TextView) findViewById(R.id.yearMoney);
    totalMoney = (TextView) findViewById(R.id.totalMoney);
    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    mActivityTitle = getTitle().toString();

    addDrawerItems();
    setupDrawer();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {

            String item = ((TextView)view).getText().toString();

            Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();

        }
    });

    }

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

private void addDrawerItems() {
    String[] osArray = { "New Shift", "Poop Slips", "Statistics", "Help"};
    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
    mDrawerList.setAdapter(mAdapter);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return true;
}

private void setupDrawer() {
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.string.drawer_open, R.string.drawer_close) {

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            getSupportActionBar().setTitle("Menu");
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            getSupportActionBar().setTitle(mActivityTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };
    mDrawerToggle.setDrawerIndicatorEnabled(true);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
}
EN

回答 3

Stack Overflow用户

发布于 2016-08-28 07:44:20

addDrawerItems();之后添加这一行

代码语言:javascript
复制
mDrawerList.setTextFilterEnabled(true);
mDrawerList.setItemsCanFocus(true);
票数 0
EN

Stack Overflow用户

发布于 2016-08-28 07:52:03

通过添加固定

代码语言:javascript
复制
mDrawerList.bringToFront();
mDrawerLayout.requestLayout();

代码语言:javascript
复制
public void onDrawerOpened(View drawerView)
票数 0
EN

Stack Overflow用户

发布于 2016-08-28 08:19:04

我看不到在OnItemClick方法中启动新活动的任何语句。您只需要获得一个string,并在相同的活动上创建一个Toast。尝试单击id of Item并在该id的基础上启动Activity

这是你的一个小密码。

代码语言:javascript
复制
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(currentActivity.this, parent.class);
            startActivity(intent);
        }
    });
}

如果这段代码能让你的应用程序运行,请告诉我。并标记这个答案被接受。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39188891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档