Textwatcher与Android的定制列表视图 [英] Textwatcher with custom listview in android
问题描述
我有哪些项目被加载自定义列表视图。我有两个textviews和一个EditText。第一TextView中包含来自父列表ploaded数据$ P $。用户在EditText上输入文字。进入的EditText后,第一TextView的和的EditText的乘法结果具有要显示在另一个的TextView。到目前为止,我不能得到的结果。我检查结果在祝酒。但它产生的结果只有一个列表的最后一个项目即投入。所以,帮助我..
这是我的code ..我用preferences摆脱previous活动数据。
公共类CurrencyCount延伸活动{
公众共享preferences getTextData;
公众共享preferences的getData;
公众共享preferences getCoinData;
私人列表<串GT; mlist1;
私人的ListView mlistview1;
私人ArrayAdapter<串GT;适配器1;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.next);
mlistview1 =(ListView控件)findViewById(R.id.listView1);
mlist1 =新的ArrayList<串GT;();
的getData = getShared preferences(MainActivity.MY_ preF1,MODE_PRIVATE);
getCoinData = getShared preferences(MainActivity.MY_ preF2,MODE_PRIVATE);
getTextData = getShared preferences(MainActivity.MY_ preF3,MODE_PRIVATE);
的for(int i = 0; I< = 1000;我++)
{
字符串键=将String.valueOf(I)
字符串STR1 = getData.getString(键,);
字符串STR2 = getCoinData.getString(键,);
如果(!str1.equals()){
mlist1.add(STR1);
} 如果(!str2.equals()){
mlist1.add(STR2);
}
//Toast.makeText(Next.this的数目为+ STR,Toast.LENGTH_SHORT).show();
} //适配器1 =新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,mlist1);
适配器1 =新ListAdapter(CurrencyCount.this,R.layout.custom_list,mlist1);
mlistview1.setAdapter(适配器1);
}
类ListAdapter扩展ArrayAdapter<串GT;
{
私人TextView的mtext_currency;
私人TextView的mtext_currencyresult;
私人的EditText medit_currencycount;
私人查看MV;
私人LayoutInflater MLI;
私人语境mcontext;
私人列表<串GT; mlist;
公共ListAdapter(上下文的背景下,INT textViewResourceId,列表与LT;弦乐>名单){
超(背景下,R.layout.custom_list,清单);
this.mcontext =背景;
this.mlist =清单; // TODO自动生成构造函数存根
} @覆盖
公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
// TODO自动生成方法存根 MLI =(LayoutInflater)mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
MV = mli.inflate(R.layout.custom_list,父母,假);
mtext_currency =(TextView中)mv.findViewById(R.id.textView1);
medit_currencycount =(EditText上)mv.findViewById(R.id.editText1);
mtext_currencyresult =(TextView中)mv.findViewById(R.id.textView4);
mtext_currency.setText(mlist.get(位置)); medit_currencycount.addTextChangedListener(新TextWatcher(){ @覆盖
公共无效onTextChanged(CharSequence的为arg0,ARG1 INT,INT ARG2,诠释ARG3){
// TODO自动生成方法存根 } @覆盖
公共无效beforeTextChanged(CharSequence的为arg0,ARG1 INT,INT ARG2,
INT ARG3){
// TODO自动生成方法存根 } @覆盖
公共无效afterTextChanged(编辑为arg0){
// TODO自动生成方法存根
如果(arg0.length()!= 0)
{
字符串S1 = arg0.toString();
。字符串s2 = mtext_currency.getText()的toString();
INT结果=(的Integer.parseInt(S1))*(的Integer.parseInt(S2));
串S =将String.valueOf(结果);
Toast.makeText(CurrencyCount.this,该产品的结果是+ S3,Toast.LENGTH_SHORT).show();
mtext_currencyresult.setText(S3);
}
} }); 返回MV;
}
}
}
这是我主要的XML。
<?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent>
< ListView控件
机器人:ID =@ + ID / ListView1的
机器人:layout_width =180dp
机器人:layout_height =190dp
机器人:layout_alignLeft =@ + ID / textView1
机器人:layout_alignParentTop =真
机器人:layout_marginTop =27dp>
< /&的ListView GT;
这是我的自定义XML
<?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =40dp> <的TextView
机器人:ID =@ + ID / textView1
机器人:layout_width =40dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =10dp
机器人:文字=
机器人:textAppearance =机器人:ATTR / textAppearanceMedium/> <的TextView
机器人:ID =@ + ID / textView2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginLeft =5DP
机器人:layout_marginTop =10dp
机器人:layout_toRightOf =@ + ID / textView1
机器人:文字=@字符串/ MUL
机器人:textAppearance =机器人:ATTR / textAppearanceMedium/> <的EditText
机器人:ID =@ + ID / editText1
机器人:layout_width =50dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:layout_marginLeft =16DP
机器人:layout_toRightOf =@ + ID / textView2
机器人:EMS =10
安卓的inputType =号> < requestFocus的/>
< /&的EditText GT; <的TextView
机器人:ID =@ + ID / textView3
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:layout_marginLeft =5DP
机器人:layout_marginTop =10dp
机器人:layout_toRightOf =@ + ID / editText1
机器人:文字=@字符串/平等
机器人:textAppearance =机器人:ATTR / textAppearanceMedium/> <的TextView
机器人:ID =@ + ID / textView4
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:layout_marginLeft =10dp
机器人:layout_marginTop =10dp
机器人:layout_toRightOf =@ + ID / textView3
机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>< / RelativeLayout的>
而总是列表的最后一个项目产生的原因,其结果是,在匿名内部类中使用成员变量。
mtext_currency
和 mtext_currencyresult
成员变量在匿名内部类中使用 TextWatcher
。当 getView
被要求在列表中的最后一个可见条目, mtext_currency
必须将<$ C $参考C>的TextView 的最后一个项目。同样的道理也适用于 mtext_currencyresult
。
要克服这个问题,你需要使用最后
引用在 TextWatcher
。
公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
//其他code
最终的TextView货币= mtext_currency; //保存最终的参考
最终的TextView currencyresult = mtext_currencyresult; //保存最终的参考 medit_currencycount.addTextChangedListener(新TextWatcher(){ @覆盖
公共无效afterTextChanged(编辑为arg0){
// TODO自动生成方法存根
如果(arg0.length()!= 0)
{
字符串S1 = arg0.toString();
。字符串s2 = currency.getText()的toString(); //这里使用最终参考
INT结果=(的Integer.parseInt(S1))*(的Integer.parseInt(S2));
串S =将String.valueOf(结果);
Toast.makeText(MainActivity.this,该产品的结果为+ S3 +地址为+货币,Toast.LENGTH_SHORT).show();
currencyresult.setText(S3); //这里使用最终参考
}
}
});
}
I have a custom listview in which items are loaded. I have two textviews and an edittext. first textview contains data preloaded from parent list. the user has to enter text in edittext. after entering in edittext, multiplication result of first textview and edittext has to be displayed in another textview. So far i cant get the result. I checked for result in toast. but it generates result for only one input i.e for last item of the list. So help me..
This is my code.. i am using preferences to get data from previous activity.
public class CurrencyCount extends Activity {
public SharedPreferences getTextData;
public SharedPreferences getData;
public SharedPreferences getCoinData;
private List<String> mlist1;
private ListView mlistview1;
private ArrayAdapter<String>adapter1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
mlistview1=(ListView)findViewById(R.id.listView1);
mlist1 = new ArrayList<String>();
getData=getSharedPreferences(MainActivity.MY_PREF1,MODE_PRIVATE);
getCoinData=getSharedPreferences(MainActivity.MY_PREF2,MODE_PRIVATE);
getTextData=getSharedPreferences(MainActivity.MY_PREF3,MODE_PRIVATE);
for(int i=0;i<=1000;i++)
{
String key = String.valueOf(i);
String str1=getData.getString(key, "");
String str2=getCoinData.getString(key, "");
if(!str1.equals("")) {
mlist1.add(str1);
}
if(!str2.equals("")) {
mlist1.add(str2);
}
//Toast.makeText(Next.this, "The number is "+str, Toast.LENGTH_SHORT).show();
}
//adapter1=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mlist1);
adapter1 = new ListAdapter(CurrencyCount.this, R.layout.custom_list, mlist1);
mlistview1.setAdapter(adapter1);
}
class ListAdapter extends ArrayAdapter<String>
{
private TextView mtext_currency;
private TextView mtext_currencyresult;
private EditText medit_currencycount;
private View mv;
private LayoutInflater mli;
private Context mcontext;
private List<String>mlist;
public ListAdapter(Context context, int textViewResourceId, List<String> list) {
super(context, R.layout.custom_list, list);
this.mcontext=context;
this.mlist=list;
// TODO Auto-generated constructor stub
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
mli=(LayoutInflater)mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mv=mli.inflate(R.layout.custom_list, parent,false);
mtext_currency=(TextView)mv.findViewById(R.id.textView1);
medit_currencycount=(EditText)mv.findViewById(R.id.editText1);
mtext_currencyresult=(TextView)mv.findViewById(R.id.textView4);
mtext_currency.setText(mlist.get(position));
medit_currencycount.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
if(arg0.length()!=0)
{
String s1=arg0.toString();
String s2=mtext_currency.getText().toString();
int result=(Integer.parseInt(s1))*(Integer.parseInt(s2));
String s3=String.valueOf(result);
Toast.makeText(CurrencyCount.this, "The product result is "+s3, Toast.LENGTH_SHORT).show();
mtext_currencyresult.setText(s3);
}
}
});
return mv;
}
}
}
This is my main xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listView1"
android:layout_width="180dp"
android:layout_height="190dp"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentTop="true"
android:layout_marginTop="27dp" >
</ListView>
This is my custom xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/textView1"
android:text="@string/mul"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/textView2"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/editText1"
android:text="@string/equal"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/textView3"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
The reason for the result being always generated for last item of the list is, the use of member variables in the anonymous inner class.
mtext_currency
and mtext_currencyresult
member variables are used in the anonymous inner class TextWatcher
. When getView
is called for the last visible item in the list, mtext_currency
will have a reference to the TextView
of the last item. Same reason applies to mtext_currencyresult
.
To overcome the issue, you need to use final
references in TextWatcher
.
public View getView(final int position, View convertView, ViewGroup parent) {
// Other code
final TextView currency = mtext_currency; // Hold a final reference
final TextView currencyresult = mtext_currencyresult; // Hold a final reference
medit_currencycount.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
if(arg0.length()!=0)
{
String s1=arg0.toString();
String s2=currency.getText().toString(); // Use final reference here
int result=(Integer.parseInt(s1))*(Integer.parseInt(s2));
String s3=String.valueOf(result);
Toast.makeText(MainActivity.this, "The product result is "+s3 + " address is " + currency, Toast.LENGTH_SHORT).show();
currencyresult.setText(s3); // Use final reference here
}
}
});
}
这篇关于Textwatcher与Android的定制列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!