【问题标题】:Textwatcher with custom listview in androidandroid中带有自定义列表视图的Textwatcher
【发布时间】:2014-05-21 22:46:35
【问题描述】:

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

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

公共类 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>

【问题讨论】:

    标签: android


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多