【问题标题】:ListView only one list entry play button worksListView 只有一个列表条目播放按钮有效
【发布时间】:2013-04-08 03:20:38
【问题描述】:

我正在尝试按照本网站的示例进行 ListView 活动

http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/

其中我使用数据库中的数据并使用 BaseAdapter 一切似乎都工作正常(onListItemClick,加载我的数据的文本视图)但我在列表项右侧的“播放”按钮不起作用。我如果有帮助,我会在我的列表项 xml 中使用框架布局。

---更新---

这些答案都没有解决我的问题,但在摆弄之后,我得到了第一个入口播放按钮,但其他入口播放按钮不起作用,只有第一个起作用。 这是我的列表活动和 xml 的新代码。

PlayAFriend 活动

    package com.fullfrontalgames.numberfighter;
import com.fullfrontalgames.numberfighter.DBAdapter;
import com.fullfrontalgames.numberfighter.R;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;



import android.os.Bundle;

import android.support.v4.widget.SimpleCursorAdapter;


import android.view.View;


import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;

import android.widget.Toast;





public class PlayAFriend extends ListActivity{
    DBAdapter DBAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_items);

        final DBAdapter db = new DBAdapter(this);
        DBAdapter = db.open();




        ListView FriendLV = (ListView) findViewById(android.R.id.list);
        Button playbutton = (Button) findViewById(R.id.playbutton);



        FriendLV.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                Toast.makeText(getBaseContext(), "test", Toast.LENGTH_SHORT).show();
            }

        });
        playbutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent("com.fullfrontalgames.numberfighter.Fightattacker"));
            }
        });


Cursor friendslist = db.GetAllFriends();

        String[] from = new String[] {"FRIENDS"};   // your column/columns here
        int[] to = new int[] {R.id.textview_friends};

        @SuppressWarnings("deprecation")
        ListAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.list_items, friendslist, from, to,0);
          FriendLV.setAdapter(cursorAdapter);








    }


}

list_items xml

    <?xml version="1.0" encoding="utf-8"?>


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

    <ListView 
            android:id="@android:id/list"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="vertical" >

        </ListView>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:background="@drawable/searchimageview"
        android:orientation="vertical" >



<TextView 
    android:id="@+id/textview_friends"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    android:textStyle="bold"
    android:textSize="40dp"
    android:layout_gravity="center" />

<Button
        android:id="@+id/playbutton"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="right"
        android:background="@drawable/playbutton" />


    </FrameLayout>



</LinearLayout>

【问题讨论】:

  • 您的播放按钮位于列表视图之外。列表中的每一行是否都有播放按钮
  • @Raghunandan 我的播放按钮在列表视图之外,因为我不能在列表视图中使用按钮小部件,我是否必须为列表视图中的每个条目制作一个播放按钮?请记住我的列表是从 SQLite 数据库中,当玩家将某人添加到他们的好友列表时,根据这个人的不同,可能有 5 个好友到 100 个好友。
  • 您提供了一个链接,该链接在每个列表视图行中都有一个按钮。所以如果按钮在外面。然后点击监听器将适用于该按钮而不是行中的每个项目。
  • @Raghunandan 有没有办法用我拥有的代码来完成这个?我知道在我给它的链接中,当你在字符串数组中添加条目时如何完成这个,但是如何我从用户输入的条目中实现了同样的效果。

标签: java android eclipse listview


【解决方案1】:

可能是你的方式不对。你必须尝试这个由textView 插入的链接,你必须使用buttonhttp://wowjava.wordpress.com/2011/03/26/dynamic-listview-in-android/

如果您有任何问题,请告诉我。

【讨论】:

    【解决方案2】:

    试试下面的

    在getView()中

     Button b=(Button) convertView.findViewById(R.id.playbutton);
     b.setOnClickListener(new OnClickListener()
     {
           if (position != ListView.INVALID_POSITION) {
                            startActivity(newIntent("com.fullfrontalgames.numberfighter.Fightattacker"));
                    }
     });
    

    您还应该使用 ViewHolder 来实现平滑滚动和性能。下面的链接中提供了详细信息。

    http://developer.android.com/training/improving-layouts/smooth-scrolling.html

    【讨论】:

      【解决方案3】:

      更新

      抱歉更新晚了,但我自己解决了这个问题,方法是向我的播放按钮添加一个 android:onClick 属性,然后在我的类中定义公共 onButtonClick 方法。

      <Button
          android:id="@+id/playbutton"
          android:layout_width="60dp"
          android:layout_height="60dp"
          android:layout_gravity="right"
          android:onClick="OnButtonClick"
          android:background="@drawable/playbutton" />
      

      public void OnButtonClick(View view) {
          startActivity(new Intent(
                  "com.fullfrontalgames.numberfighter.Fightattacker"));
      }
      

      【讨论】:

        猜你喜欢
        • 2022-06-10
        • 2011-07-28
        • 1970-01-01
        • 2016-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多