簡體   English   中英

帶有標題和toast的ListActivity選擇錯誤的列表項

[英]ListActivity with header and toast selecting wrong list item

編寫應用程序Menu.java擴展ListActivity,我在列表活動的頂部添加了一個TextView標題。

標題和列表活動的布局正是我想要的,但是當我點擊說Item1時,會彈出toast並說“Item2”被選中。 當我點擊Item2 ...“Item3”選中。 但是當我點擊Item3時,應用程序崩潰並顯示一堆運行時錯誤。

在我實現標頭之前,它沒有這樣做。 我搜索並搜索了stackoverflow和android dev網站,似乎無法找到答案。

我已經將標頭設置為false,因此它不可點擊。

提前致謝! 對不起,如果我錯過了一些明顯的東西......新手在這里。 :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>

位置(由onListItemClick )與ListView中的項目數包括標題(和頁腳)而不是適配器。

代替:

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

采用:

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

試試這個

mLV_list.setOnItemClickListener(this);

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM