简体   繁体   中英

ListActivity with header and toast selecting wrong list item

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);

Try using this

mLV_list.setOnItemClickListener(this);

and

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

    String element = (String)getListAdapter().getItem(arg2);
    Toast.makeText(this, element+" clicked ", Toast.LENGTH_SHORT).show();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM