gpt4 book ai didi

android - 在 Android 中使用自定义 ListView 的 Textwatcher

转载 作者:行者123 更新时间:2023-11-29 01:47:35 25 4
gpt4 key购买 nike

我有一个自定义 ListView ,其中加载了项目。我有两个 TextView 和一个编辑文本。第一个 TextView 包含从父列表预加载的数据。用户必须在 edittext 中输入文本。输入edittext后,第一个textview和edittext的乘法结果必须显示在另一个textview中。到目前为止,我无法得到结果。我检查了 toast 的结果。但它只为一个输入生成结果,即列表的最后一项。所以帮帮我..

这是我的代码..我正在使用首选项从以前的 Activity 中获取数据。

公共(public)类 CurrencyCount 扩展 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;


}
}

这是我的主要 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>

这是我的自定义 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>

最佳答案

总是为列表的最后一项生成结果的原因是在匿名内部类中使用了成员变量。

mtext_currencymtext_currencyresult成员变量在匿名内部类TextWatcher中使用。当为列表中的最后一个可见项目调用 getView 时,mtext_currency 将引用最后一个项目的 TextView。同样的原因也适用于 mtext_currencyresult

要解决这个问题,您需要在 TextWatcher 中使用 final 引用。

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
}
}
});
}

关于android - 在 Android 中使用自定义 ListView 的 Textwatcher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20468145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com