ListActivity用头和吐司选择错误列表项 [英] ListActivity with header and toast selecting wrong list item

查看:150
本文介绍了ListActivity用头和吐司选择错误列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写应用程序Menu.java扩展ListActivity,我已经在我上面的列表活动上添加一个TextView头。

标题和列表活动的布局正是我想要的,但是当我点击说项目1烤面包弹出,并说选择了项目2。当我点击项目2 ...项目3选择。但是,当我点击项目3,应用程序崩溃,并显示了一堆运行时错误的。

它没有做到这一点,我有头实施之前。我已搜查,搜查计算器和Android开发网站,似乎无法找到答案。

我已经做了头部设置为false,使其无法点击。

在此先感谢!很抱歉,如果我错过了一些东西很明显...新手在这里。 :P

  //这是Menu.java


包com.example.mytexts;

进口android.app.ListActivity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.AdapterView;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;
进口android.widget.TextView;
进口android.widget.Toast;
进口android.widget.AdapterView.OnItemClickListener;

公共类菜单扩展ListActivity {

的String []值=新的String [] {项目1,项目2,项目3};

公共无效的onCreate(包饼干){
    super.onCreate(饼干);

    的setContentView(R.layout.text_layout);

    ListView的LV = getListView();
    LayoutInflater充气= getLayoutInflater();
    查看标题= inflater.inflate(R.layout.header,
            (ViewGroup中)findViewById(R.id.header__root));
    lv.addHeaderView(头,空,假);

    ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(这一点,
            android.R.layout.simple_list_item_1,价值观);
    setListAdapter(适配器);
}

保护无效onListItemClick(ListView的L,视图V,INT位置,长的id){
    super.onListItemClick(L,V,位置ID);
    奶酪条=(字符串)getListAdapter()的getItem(位置)。
    Toast.makeText(这一点,奶酪+选择,Toast.LENGTH_SHORT).show();

    尝试 {
        类ourClass =的Class.forName(com.example.addsubtract。+奶酪);
        意图ourIntent =新的意图(菜单。该,ourClass);
        startActivity(ourIntent);

    }赶上(ClassNotFoundException异常E){
        e.printStackTrace();
    }
  }
}


//这是header.xml


< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / header__root
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@彩色/黑白
机器人:方向=垂直>

<的TextView
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:背景=@色/深蓝色
    机器人:重力=中心
    机器人:填充=10dp
    机器人:文本=消息
    机器人:TEXTSIZE =20dp>
< / TextView的>

< / LinearLayout中>


//这是text_layout.xml


< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直>

<的ListView
    机器人:ID =@ + ID / Android的:清单
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:滚动条=无>
< / ListView控件>

<的TextView
    机器人:ID =@ + ID /安卓:空
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文本=没有要显示>
< / TextView的>

< / LinearLayout中>
 

解决方案

的位置(通过给 onListItemClick )被捆绑与项目在ListView包括头数(和页脚),而不是与适配器

相反的:

 字符串奶酪=(字符串)getListAdapter()的getItem(位置)。
 

使用:

 字符串奶酪=(字符串)l.getItemAtPosition(位置);
 

Writing an application Menu.java extending ListActivity and I have added a TextView header at the top above my list activity.

The layout of header and list activity are exactly what I want, but when I click on say Item1 the toast pops up and says "Item2" selected. When I click on Item2... "Item3" Selected. But when I click on Item3, the app crashes and displays a bunch of Runtime Errors.

It did not do this before I had the header implemented. I have searched and searched stackoverflow and the android dev site and can not seem to find the answer.

I have already made the header set to false so that it is not clickable.

Thanks in advance! Sorry if I have missed something obvious... newbie here. :P

//this is Menu.java


package com.example.mytexts;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Menu extends ListActivity {

String[] values = new String[] { "Item1", "Item2", "Item3" };

public void onCreate(Bundle biscuits) {
    super.onCreate(biscuits);

    setContentView(R.layout.text_layout);

    ListView lv = getListView();
    LayoutInflater inflater = getLayoutInflater();
    View header = inflater.inflate(R.layout.header,
            (ViewGroup) findViewById(R.id.header__root));
    lv.addHeaderView(header, null, false);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
}

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    String cheese = (String) getListAdapter().getItem(position);
    Toast.makeText(this, cheese + " selected", Toast.LENGTH_SHORT).show();

    try {
        Class ourClass = Class.forName("com.example.addsubtract." + cheese);
        Intent ourIntent = new Intent(Menu.this, ourClass);
        startActivity(ourIntent);

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
  }
}


//this is header.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/header__root"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/black"
android:orientation="vertical" >

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/darkblue"
    android:gravity="center"
    android:padding="10dp"
    android:text="Messages"
    android:textSize="20dp" >
</TextView>

</LinearLayout>


//this is text_layout.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ListView
    android:id="@+id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbars="none" >
</ListView>

<TextView
    android:id="@+id/android:empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Nothing to Display" >
</TextView>

</LinearLayout>

解决方案

The position (giving by onListItemClick) is tied up with the number of items in the ListView including header (and footer) and not with the adaptor.

Instead of:

String cheese = (String) getListAdapter().getItem(position);

Use:

String cheese = (String) l.getItemAtPosition(position);

这篇关于ListActivity用头和吐司选择错误列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆