如何将编辑文本值传递给适配器类 [英] How to pass the edit text value to adapter class
问题描述
我想将文本值从下面的活动传递给TextAdapter类.
公共类SecondActivity扩展了Activity {
I want to pass the text value from below activity to TextAdapter class.
public class SecondActivity extends Activity {
EditText et1;
TextView t1,t2;
Button b1,b2;
String result;
Context context = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sec);
et1 = (EditText) this.findViewById(R.id.editText1);
t1 = (TextView) this.findViewById(R.id.textView1);
t2 = (TextView) this.findViewById(R.id.textView2);
b1 = (Button) this.findViewById(R.id.button1);
b2 = (Button) this.findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t2.setText(et1.getText());
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(v.getContext(),ThirdActivity.class);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
此值将添加到数组中 公共类TextAdapter扩展了PagerAdapter {
This values are to be added in the array public class TextAdapter extends PagerAdapter{
private String[] textViewer = new String[]{
"Hai"--------------(Here i want the edit text value)
};
public TextAdapter (Context context){
this.context= context;
}
@Override
public int getCount() {
return textViewer.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==((TextView)object);
}
public Object instantiateItem(ViewGroup container, int position) {
TextView textView = new TextView(context);
textView.setText(textViewer[position]);
((ViewPager) container).addView(textView, 0);
return textView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((TextView) object);
}
}
推荐答案
在活动中创建上下文,并在PageAdapter类中传递上下文,如下所示:
Create the context in the activity and pass the Context in the PageAdapter class like this:
public class MainActivity extends Activity {
public EditText editText = null;
private Context context = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.xxxx);
context = this;
AnotherClass anotherClassObject = new AnotherClass(context)
}
您的页面适配器类如下所示:
Where your page adapter class will be somewhat like this:
public class AnotherClass extends PageAdapter {
public AnotherClass(Context context)
{
context.editText.getText().toString().trim(); // Here you will get the edittext value
}
}
它可能会要求您键入强制转换上下文,在这种情况下,请使用Activity类名将其强制转换.
It may ask you to type cast the context, in that case type cast it with Activity class name.
已更新:-
公开editText并调用
Make the editText public and call
((SecondActivity)context).et1.getText().toString().trim();
这篇关于如何将编辑文本值传递给适配器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!