单击列表视图中的某些项目时如何影响列表视图之外的元素?

Posted

技术标签:

【中文标题】单击列表视图中的某些项目时如何影响列表视图之外的元素?【英文标题】:How to affect elements outside of a listview when some items in the listview are clicked? 【发布时间】:2016-03-30 18:26:11 【问题描述】:

我的 content_main.xml 布局中有一个 ListView。此列表视图的内容是通过 listview.setAdapter(myCustomAdapter) 设置的。我在 content_main.xml 布局中也有一个 TextView。

我创建了自己的自定义适配器,称为 ArrayAdapterItem,它扩展了 ArrayAdapter。在此适配器的 getView() 方法中,我试图访问 content_main.xml 中包含的 TextView。我不知道该怎么做。我需要能够做到这一点,因为如果单击列表中的某些项目,我想用新信息更新此 TextView。但是我无法通过 parent.findViewbyId(R.id.main_activity_textView) 访问它。此方法在运行时返回 null。

我尝试在谷歌上搜索,但只能获得与我无关的不同主题的答案。

我是安卓新手。解决这个问题的方法可能很简单,但我在 *** 上找不到任何与此相关的教程或问题。

MainActivity.java

package com.example.simran.listviewtest;

    import android.app.AlertDialog;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.ListView;

    public class MainActivity extends AppCompatActivity 
        AlertDialog alertDialogStores;
        @Override
        protected void onCreate(Bundle savedInstanceState) 
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            showList();

            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View view) 
                    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                
            );
        

        public void showList()

            // add your items, this can be done programatically
            // your items can be from a database
            ObjectItem[] ObjectItemData = new ObjectItem[20];

            ObjectItemData[0] = new ObjectItem(91, "Mercury");
            ObjectItemData[1] = new ObjectItem(92, "Watson");
            ObjectItemData[2] = new ObjectItem(93, "Nissan");
            ObjectItemData[3] = new ObjectItem(94, "Puregold");
            ObjectItemData[4] = new ObjectItem(95, "SM");
            ObjectItemData[5] = new ObjectItem(96, "7 Eleven");
            ObjectItemData[6] = new ObjectItem(97, "Ministop");
            ObjectItemData[7] = new ObjectItem(98, "Fat Chicken");
            ObjectItemData[8] = new ObjectItem(99, "Master Siomai");
            ObjectItemData[9] = new ObjectItem(100, "Mang Inasal");
            ObjectItemData[10] = new ObjectItem(101, "Mercury 2");
            ObjectItemData[11] = new ObjectItem(102, "Watson 2");
            ObjectItemData[12] = new ObjectItem(103, "Nissan 2");
            ObjectItemData[13] = new ObjectItem(104, "Puregold 2");
            ObjectItemData[14] = new ObjectItem(105, "SM 2");
            ObjectItemData[15] = new ObjectItem(106, "7 Eleven 2");
            ObjectItemData[16] = new ObjectItem(107, "Ministop 2");
            ObjectItemData[17] = new ObjectItem(108, "Fat Chicken 2");
            ObjectItemData[18] = new ObjectItem(109, "Master Siomai 2");
            ObjectItemData[19] = new ObjectItem(110, "Mang Inasal 2");

            // our adapter instance
            ArrayAdapterItem adapter = new ArrayAdapterItem(this, R.layout.list_view_row_item, ObjectItemData);

            // create a new ListView, set the adapter and item click listener
            ListView listViewItems = (ListView)findViewById(R.id.main_activity_listView);
            listViewItems.setAdapter(adapter);
            listViewItems.setOnItemClickListener(new OnItemClickListenerListViewItem());

        

        @Override
        public boolean onCreateOptionsMenu(Menu menu) 
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        

        @Override
        public boolean onOptionsItemSelected(MenuItem item) 
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) 
                return true;
            

            return super.onOptionsItemSelected(item);
        
    

ArrayAdapterItem.java

            package com.example.simran.listviewtest;

        import android.app.Activity;
        import android.content.Context;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.ArrayAdapter;
        import android.widget.TextView;

        import org.w3c.dom.Text;

        /**
         * Created by simra on 12/23/2015.
         */
        public class ArrayAdapterItem extends ArrayAdapter<ObjectItem> 
            Context mContext;
            int layoutResourceId;
            ObjectItem data[] = null;

            public ArrayAdapterItem(Context mContext, int layoutResourceId, ObjectItem[] data) 
                super(mContext, layoutResourceId, data);

                this.layoutResourceId = layoutResourceId;
                this.mContext = mContext;
                this.data = data;
            


            @Override
            public View getView(int position, View convertView, ViewGroup parent) 

                /*
                 * The convertView argument is essentially a "ScrapView" as described is Lucas post
                 * http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
                 * It will have a non-null value when ListView is asking you recycle the row layout.
                 * So, when convertView is not null, you should simply update its contents instead of inflating a new row layout.
                 */
                if (convertView == null) 
                    // inflate the layout
                    LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
                    convertView = inflater.inflate(layoutResourceId, parent, false);
                

                //trying set the text of the textView in the main_activity.
                TextView mainActivityTextView = (TextView) parent.findViewById(R.id.main_activity_textView);
                mainActivityTextView.setText("some new text");

                // object item based on the position
                ObjectItem objectItem = data[position];

                // this sets the text and tag for the item in the list. Works fine
                TextView textViewItem = (TextView)   convertView.findViewById(R.id.textViewItem);
                textViewItem.setText(objectItem.itemName);
                textViewItem.setTag(objectItem.itemId);

                return convertView;

            
        

content_main.xml

     `<?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_
            android:layout_
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:context="com.example.simran.listviewtest.MainActivity"
            tools:showIn="@layout/activity_main">

            <TextView
                android:id="@+id/hello_world"
                android:layout_
                android:layout_
                android:text="Hello World!"
                android:layout_alignParentTop="true"/>

            <ListView
                android:id="@+id/options_list"
                android:layout_
                android:layout_
                android:layout_below="@id/hello_world">

            </ListView>

        </RelativeLayout>`

list_view_row_item.xml

        <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_
        android:layout_
        android:padding="10dp" >

        <TextView
            android:id="@+id/textViewItem"
            android:layout_
            android:layout_
            android:layout_alignParentTop="true"
            android:text="Item name here..."
            android:textSize="15dp" />
    </RelativeLayout>

ObjectItem.java

    package com.example.simran.listviewtest;

/**
 * Created by simra on 12/23/2015.
 */
public class ObjectItem 
    public int itemId;
    public String itemName;

    // constructor
    public ObjectItem(int itemId, String itemName) 
        this.itemId = itemId;
        this.itemName = itemName;
    

【问题讨论】:

你应该为适配器实现一个监听器,当在 listView 中单击某些项目时,它将更改 textView @PankajNimgade 我不知道你的意思是什么?您是否想说在适配器的 getView() 方法中为每个单独的项目添加一个侦听器?你能详细说明你在说什么或者它在代码中的样子吗?或者您是说将侦听器添加到 mainActivity 类中的适配器?我是 Android 新手,所以我不明白你的意思。 @simcity,很抱歉有歧义,您只需添加一行代码即可完成您的要求,您将完成,我已经回答了,请检查它 【参考方案1】:

您有三个选项(仅三个示例):

使用 ListView 提供的点击监听器。 (首选方法) 通过适配器构造函数注入依赖。 使用侦听器模式通知活动。

注意:我不确定为什么在调用 getView 方法时需要更新 TextView,getView 可以经常被调用退出(例如滚动时),这意味着 TextView 将几乎随机更新。我会选择选项 1,这是对列表中项目的点击做出反应的最可靠和首选的方式。

选项 1:

public class ExampleActivity extends Activity implements OnItemClickListener 

    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        //Set layout etc

        textView = (TextView) findViewById(some id); //Find the TextView

        ListView list = (ListView) findViewById(some id); //Find the ListView
        //Set adapter etc.
        list.setOnItemClickListener(this);
    


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int pos, long id) 
        textView.setText("Some text"); //Get the text from your adapter for example
    

选项 2:

public ArrayAdapterItem(Context mContext, int layoutResourceId, ObjectItem[] data, TextView textview) 
    //etc.

选项 3:

适配器:

    package com.example.simran.listviewtest;

    import android.app.Activity;
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.TextView;

    import org.w3c.dom.Text;

    /**
     * Created by simra on 12/23/2015.
     */
    public class ArrayAdapterItem extends ArrayAdapter<ObjectItem> 
        Context mContext;
        int layoutResourceId;
        ObjectItem data[] = null;
        ArrayAdapterItemCallback callback;

        public interface ArrayAdapterItemCallback 
            updateText(String text);
        



        public ArrayAdapterItem(Context mContext, int layoutResourceId, ObjectItem[] data, ArrayAdapterItemCallback callback) 
            super(mContext, layoutResourceId, data);

            this.layoutResourceId = layoutResourceId;
            this.mContext = mContext;
            this.data = data;
            this.callback = callback;
        


        @Override
        public View getView(int position, View convertView, ViewGroup parent) 

            /*
             * The convertView argument is essentially a "ScrapView" as described is Lucas post
             * http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
             * It will have a non-null value when ListView is asking you recycle the row layout.
             * So, when convertView is not null, you should simply update its contents instead of inflating a new row layout.
             */
            if (convertView == null) 
                // inflate the layout
                LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
                convertView = inflater.inflate(layoutResourceId, parent, false);
            

            //trying set the text of the textView in the main_activity.
            callback.updateText("some new text");

            // object item based on the position
            ObjectItem objectItem = data[position];

            // this sets the text and tag for the item in the list. Works fine
            TextView textViewItem = (TextView)   convertView.findViewById(R.id.textViewItem);
            textViewItem.setText(objectItem.itemName);
            textViewItem.setTag(objectItem.itemId);

            return convertView;

        
    

活动:

public class ExampleActivity extends Activity implements UpdateActivity 
    //etc.

    TextView text; //Init this in the onCreate

    @Override
    void updateText(String text)
         text.setText(text);
    

【讨论】:

感谢您的回答。我曾考虑过这一点,但我想也许Android中有一种我不知道的更简单的方法。再次感谢您的快速回复。 @simcity 我为你添加了一个额外的选项;)【参考方案2】:

在 Android 中使用 listview 适配器时,它们应该几乎总是被放置为使用它们的类的内部类 - 在这种情况下,它是您的 MainActivity 类。当适配器是内部类时,它可以使用封闭类的所有属性。如果您要更新的 textView 不存在,那么它应该在那里。

【讨论】:

【参考方案3】:

您可以使用 LocalBroadCastManager,这样您就可以在您的应用程序中从任何地方到任何地方进行通信

你可以通过this查看,如何使用LocalBraodcastManager。

how to use LocalBroadcastManager?

【讨论】:

【参考方案4】:

你需要的不是很努力尝试重新思考你到底想要什么,

您拥有的是TextView 来填充实例ObjectItem 类型的实例变量中的信息,

您可以像这样轻松地从单击的列表中获取对象

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
                ObjectItem singleItem = (ObjectItem) listView.getItemAtPosition(position);
                textView.setText(""+singleItem.itemName);
                //this is the textView which you should already get the reference in Main Activity

            

        );

当你评估你需要什么时,很容易减少代码,没有必要实现一些你可能不会用于任何其他工作的代码。很抱歉发表评论,但这是另一种让你的车掉头的方法:)

【讨论】:

【参考方案5】:

你应该替换:

parent.findViewById(...) by convertView.findViewById(...)

【讨论】:

以上是关于单击列表视图中的某些项目时如何影响列表视图之外的元素?的主要内容,如果未能解决你的问题,请参考以下文章

如何禁用单击列表视图中的特定项目

单击 CursorAdapter 中的按钮时如何在列表视图项目行中执行更新和删除操作

单击空白区域时如何处理事件?

在 android 中单击按钮时将列表视图中的选中项目转移到另一个列表视图

当我单击列表视图项目时,如何进入其他活动?

如何知道单击项目在列表视图中相对于android中的屏幕的y位置