Reputation: 31
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>
Upvotes: 2
Views: 2372
Reputation: 17615
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
}
}
});
}
Upvotes: 1