简体   繁体   English

如何从listView中按下的按钮访问主活动中的数据?

[英]How to access data in main Activity from a button pressed in listView?

So I have a listView with a custom adapter and I want that whenever I press a button I could listen it from the OnItemClickListener from the MainActivity. 因此,我有一个带有自定义适配器的listView,我希望每当我按下一个按钮时,都可以从MainActivity的OnItemClickListener监听它。

So my question is what will I pass while calling onClick on the Button present in my ListView so that I can read it from the main Activity? 所以我的问题是,当调用ListView中存在的Button的onClick时,我将通过什么内容,以便可以从主Activity中读取它?

 @Override
public View getView(final int pos, View convertView, ViewGroup parent) {
    Holder holder = new Holder();
    final CustomProductAdapter p=this;
    final View rowView;
    rowView = inflater.inflate(R.layout.product_list_test, null);
    holder.productView=(TextView) rowView.findViewById(R.id.productView);
    holder.descView=(TextView) rowView.findViewById(R.id.descView);
    holder.imageView=(ImageView) rowView.findViewById(R.id.imageView);
    holder.productCost=(TextView) rowView.findViewById(R.id.productCost);
    holder.iButton=(ImageButton) rowView.findViewById(R.id.imageButton);
    holder.iButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


           //What will I put here?


        }
    });
    holder.productView.setText(proNames[pos]);
    holder.descView.setText(R.string.description_product);
    holder.productCost.setText("0.00");
    ImageView image =holder.imageView;
    imageloader.DisplayImage(images[pos],image);

    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(context, "You Clicked "+proNames[pos], Toast.LENGTH_SHORT).show();
        }
    });
    return rowView;
}

so that I can read it from here like this(I dont know if it is the corect way) 这样我就可以从这里读取它(我不知道这是否是核心方法)

 listView = (ListView)findViewById(R.id.listView);
    listView.setAdapter(new CustomProductAdapter(this, images, proNames1, desc));
     AdapterView.OnItemClickListener mMessageClickedHandler = new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            // Do something in response to the click
            long ids=v.getId();
            if(ids==R.id.imageButton){
                Toast.makeText(getBaseContext(),"Button clicked at "+position,Toast.LENGTH_SHORT).show();
            }
        }
    };
    listView.setOnItemClickListener(mMessageClickedHandler);

in my MainActivity.java. 在我的MainActivity.java中。 I am a beginner so please help me out if I am not doing it correct. 我是初学者,所以如果我做的不正确,请帮助我。

Using an interface to communicate is one of the ways to achieve this. 使用接口进行通信是实现此目的的方法之一。

//create an interface
interface ButtonClickNotify{
   void onBUttonClick(int position);
}

Then implement it in the main activity 然后在主要活动中实施

public calss MainActivity extends AppCompatActivity implements ButtonClickNotify{

    //Override the interface method
    @Override
    public void onButtonClick(int position){
        //do something
    }
}

Then call the interface from the adapter. 然后从适配器调用接口。

//declare an instance variable
ButtonClickNotify buttonNotify;
//Initiate the iterface in the constructor
public MyListAdapter(Context context /*and other parameters*/){
   //after other constructor methods including calling super
   try{
        buttonNotify=(MainActivity)context;
   }catch(Throwable e){
       //interface is not implemented
   }
}

//at the button onclick
holder.iButton.setOnClickListener(new View.OnClickListener{
     @Overrride 
     public void onClick(View v){
         try{
            buttonNotify.onButtonClick(position);
         }catch (Throwable e){
            //interface can be null
         }
     }
});

And you don't need to have an onClick on rowView as listView.setOnItemClickListener does the same. 而且您不需要在rowViewrowView onClick ,因为listView.setOnItemClickListener做到这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据从 Main 活动保存到 ListView 活动 - Save data from Main activity to a ListView activity 如何从Android中的片段访问主要活动按钮 - how to access main activity button from fragment in android Android:通过“主要活动”按钮确定在列表视图中选中了哪个复选框 - Android : Determine which checkbox is checked in listview from the Main activity button 如何通过单击添加按钮将数据从一个活动移动到下一个活动(ListView)? - How to move data from one activity to next activity (ListView) by clicking an add button? 如何在与 activity_main 不同的活动中从 SQLite 访问数据? - How to access data from SQLite in different activity than activity_main? 如何暂停具有main()的程序,直到按下GUI中的按钮? - How to pause program having main() until a button from GUI is pressed? 如何使用主活动中的按钮增加其他活动中的TextView值? - How to increment a TextView value in a different activity with a button from the Main Activity? 如何从主Activity中刷新片段中的ListView? - How can I refresh a ListView in a Fragment from the main Activity? 如何在不切换活动的情况下使用来自另一个活动的数据和按钮单击从主活动编辑数组? - How can I edit an Array from Main Activity with the data and button click from another Activity without switching between Activities? 检测上一个活动是否按下了“后退”按钮 - Detecting if the Back Button was pressed from previous activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM