I have created a custom list view with a image view, 2 text view,one edit text one is for displaying brand names a string value another for displaying loyalty points, edit text for entering the quantity by the user which will be multiplied with loyalty point from text view.I have tried to multiplying the user entered value in each list item (from edit text)with the text view loyalty points value,I have implemented my own logic for multiplying it but it is not working, I have posted my code here for your reference correct me if any thing wrong in the code
MainActivity.java
public class MainActivity extends Activity {
ListView lv;
Context context;
static TextView ttv;
ArrayList prgmName;
public static int [] prgmImages={R.drawable.images,R.drawable.images1,R.drawable.images2,R.drawable.images3,R.drawable.images4,R.drawable.images5,R.drawable.images6,R.drawable.images7,R.drawable.images8};
public static String [] prgmNameList={"Britania","Rin","Goodrej","ITC","Nestle","Wheel","Ariel","Tide"};
public static String [] loyal_pts={"10","5","15","25","20","18","30","32"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
ttv= (TextView) findViewById(R.id.tot_point);
lv=(ListView) findViewById(R.id.listView);
lv.setAdapter(new CustomAdapter(this, prgmNameList,prgmImages,loyal_pts));
}
}
CustomAdapter.java
public class CustomAdapter extends BaseAdapter{
String [] result;
String [] Pts;
Context context;
int [] imageId;
int total_points;
String con_pts;
int lyl_pts;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages, String[] loyal_pts) {
// TODO Auto-generated constructor stub
result=prgmNameList;
Pts=loyal_pts;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
TextView tv1;
ImageView img;
EditText etxt;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final Holder holder=new Holder();
View rowView;
// final int total_points,con_pts,lyl_pts;
rowView = inflater.inflate(R.layout.item_list, null);
try{
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.etxt= (EditText) rowView.findViewById(R.id.edit_txt);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.tv1= (TextView) rowView.findViewById(R.id.textView2);
holder.tv.setText(result[position]);
holder.tv1.setText(" " + Pts[position] + "pts");
holder.img.setImageResource(imageId[position]);
System.out.println("total points"+total_points);
holder.etxt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {
lyl_pts = Integer.parseInt(String.valueOf(Pts[position]));
con_pts = holder.etxt.getText().toString().trim();
total_points = lyl_pts * Integer.parseInt(con_pts);
Toast.makeText(context,total_points,LENGTH_LONG).show();
System.out.println("heyy"+total_points);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
catch(Exception e){
e.printStackTrace();
}
rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
Toast.makeText(context, "You Clicked " + result[position], LENGTH_LONG).show();
}
catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
});
return rowView;
}
}
getView()
is called whenever a new item is displayed on screen. at the initial phase nothing will be in your EditText
so that you are not getting expected result from this line in getView()
method.
con_pts=holder.etxt.getText().toString().trim();
I suggest either putting some initial value at etxt
Or moving the logic in afterTextChanged
like here
@Override
public void afterTextChanged(Editable s) {
lyl_pts=Integer.parseInt(String.valueOf(Pts[position]));
con_pts=holder.etxt.getText().toString().trim();
total_points = lyl_pts * Integer.parseInt(con_pts);
System.out.println("total points"+total_points);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.