Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何访问适配器类中的活动对象_Java_Android_Listview_Android Listview_Baseadapter - Fatal编程技术网

Java 如何访问适配器类中的活动对象

Java 如何访问适配器类中的活动对象,java,android,listview,android-listview,baseadapter,Java,Android,Listview,Android Listview,Baseadapter,如何访问基本适配器类中的活动对象。我使用适配器类作为列表视图的适配器。我想访问位于listview之外的textview,但listview和textview处于同一活动中。我在适配器类中尝试了如下操作: holder.grandTotal = (TextView) ShopCartActivity.findViewById(R.id.txtGrandTotal); holder.grandTotal.setText(String.valueOf(new

如何访问基本适配器类中的活动对象。我使用适配器类作为列表视图的适配器。我想访问位于listview之外的textview,但listview和textview处于同一活动中。我在适配器类中尝试了如下操作:

            holder.grandTotal = (TextView) ShopCartActivity.findViewById(R.id.txtGrandTotal);
        holder.grandTotal.setText(String.valueOf(new DecimalFormat("##.##").format(grandTotal)));
// Get the rootView from the activity
final View rootView = ((Activity)mContext).getWindow().getDecorView().findViewById(android.R.id.content);

// Get the textView from the rootView
TextView mTextView = rootView.findViewById(R.id.your_text_view);

// Do something with it
mTextView.setText("Hello my friend!");
但是这个语法出现了错误:

ShopCartActivity. //ERROR
我也试过这样:

ShopCartActivity.this or ShopCartActivity.class
我在适配器类的构造函数中尝试了这个方法,它可以工作(但值尚未计算),但当我将它放在执行所有计算的getView()方法中时,它就不工作了


基本上,我想在循环返回基本适配器中的值之后设置textview的值。有没有一种方法可以使用findviewbyid方法访问对象

传递上下文创建适配器时,使用该上下文获取膨胀视图

Adapter adapter = new Adapter(this);
然后在Adpater类构造函数中:

public Adapter(Context context){
context.findViewById(R.id.textview);
}

不应尝试从适配器中访问活动。这是糟糕的编程。如果您想向活动传递一些值并在其中执行一些操作,请使用一些回调机制(抽象类或接口)将值传递给活动,然后让活动更新
TextView
的文本

使用抽象类的示例代码:

public abstract class AdapterHandler
{
    public void updateText(String text) {}
}
然后在适配器中创建此类的对象:

public AdapterHandler adapterhandler;
然后在初始化适配器后,在
活动中设置处理程序:

adapter.adapterhandler = new AdapterHandler() {
    @Override
    public void updateText(String text) {
        ShopCartActivity.this.grandTotal.setText(text);
    }
};
然后在适配器中,在需要的地方,像这样调用它:

if (this.adapterhandler != null) {
    this.adapterhandler.updateText(String.valueOf(new DecimalFormat("##.##").format(grandTotal)));
}

代码相对较长,但这是正确且更具可伸缩性的方法。

如果您有活动的上下文,您可以获得文本视图和ListView之外的其他视图,如下所示:

            holder.grandTotal = (TextView) ShopCartActivity.findViewById(R.id.txtGrandTotal);
        holder.grandTotal.setText(String.valueOf(new DecimalFormat("##.##").format(grandTotal)));
// Get the rootView from the activity
final View rootView = ((Activity)mContext).getWindow().getDecorView().findViewById(android.R.id.content);

// Get the textView from the rootView
TextView mTextView = rootView.findViewById(R.id.your_text_view);

// Do something with it
mTextView.setText("Hello my friend!");

在调用构造函数时,可以在设置适配器之前从类中发送任何对象,然后可以在适配器中接收这些参数

final ChannelsAdapter channelAdapter = new ChannelsAdapter(allChannelList, SoftKeyboard.this);
channelRecyclerView.setAdapter(channelAdapter);

将上下文传递给适配器类构造函数。然后像下面这样使用上下文
context.findviewbyd(R.id.txtgrandtoal)我在构造函数中传递了上下文,但在getView方法中没有传递,请解释更多!这是adapterhandler!=如果我将其放在getView()内的click方法中,null将变为null?在初始化和设置适配器的活动中发布代码。你一定是做错了什么。哦,等等,这可能不是问题所在。在click方法中,您必须像
AdapterClassName那样进行操作。this.adapterhandler
(请参见上面的答案,与我在
updateText
override in Activity中所做的相同)我尝试了它,但仍然出错,不管怎样,我在下面找到了简短的解决方案感谢感谢您的方法很短,并且非常简单,无法将上下文传递给适配器。这是内存泄漏。在适配器类中保留上下文不会导致内存泄漏。如需澄清,请查看此帖子的答案