Amit Ranjan
Amit Ranjan

Reputation: 567

Scrolling issue in listview

//Filling the ArrayList From the Fragment

gridmodel = new TypeTruckPogo("Truck 14 wheel",-1,false);
        list.add(gridmodel);
        gridmodel = new TypeTruckPogo("Truck 16 wheel",-1,false);
        list.add(gridmodel);

        final TypeOfTruckAdapter truckAdapter=new TypeOfTruckAdapter(context, list);
        truckListView.setAdapter(truckAdapter);

//My POGO CLASS

public class TypeTruckPogo{
    String typeOfTruckName;
    int nmbrOfTruck;
    boolean isEditTextVisiable;

    public TypeTruckPogo(String typeOfTruckName,int nmbrOfTruck,boolean isEditTextVisiable){
        this.typeOfTruckName=typeOfTruckName;
        this.nmbrOfTruck=nmbrOfTruck;
        this.isEditTextVisiable=isEditTextVisiable;
    }

    public String getTypeOfTruckName() {
        return typeOfTruckName;
    }

    public String setTypeOfTruckName(String typeOfTruckName) {
        this.typeOfTruckName = typeOfTruckName;
        return typeOfTruckName;
    }

    public int getNmbrOfTruck() {
        return nmbrOfTruck;
    }

    public Integer setNmbrOfTruck(int nmbrOfTruck) {
        this.nmbrOfTruck = nmbrOfTruck;
        return nmbrOfTruck;
    }

    public boolean isEditTextVisiable() {
        return isEditTextVisiable;
    }

    public Boolean setIsEditTextVisiable(boolean isEditTextVisiable) {
        this.isEditTextVisiable = isEditTextVisiable;
        return isEditTextVisiable;
    }
}

// My Adapter Class,this class have 20 item in the list,at first time it's showing 8 item , when user press on single row then editText become visible and if user press the same position twice the editText become inviable,But the problem is that when user tap on any position(let say he tapped on position 0) then editText is visible but when i scroll down then other editText is also becoming visible sometime position 8 sometime 15.ANY HELP APPRECIATED !!!! Single row contain textView and editText

public class TypeOfTruckAdapter extends BaseAdapter{
List<TypeTruckPogo> list;
Context context;
LayoutInflater inflater;
public String[] Current;
TypeTruckPogo typeTruckPogo;
public static HashMap<Integer,String> truckHashMap=new HashMap<Integer,String>();
public TypeOfTruckAdapter( Context context,List<TypeTruckPogo> list) {
    this.list = list;
    this.context = context;
    for(int i=0;i<list.size();i++)
    {
        truckHashMap.put(i,"");
    }
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}



class Viewholder{
    TextView name;
    EditText nmbrOfTruck;
    int ref;

    Viewholder(View view) {

        name = (TextView) view.findViewById(R.id.textView1);
        nmbrOfTruck = (EditText) view.findViewById(R.id.et_nmbr_of_truck_id);
    }

}
@Override
public View getView(final int position, View convertView,  ViewGroup parent) {
    final Viewholder holder;
    typeTruckPogo = list.get(position);
    if(convertView==null){
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = inflater.inflate(R.layout.single_row_for_type_of_truck, parent, false);
        holder = new Viewholder(convertView);
        convertView.setTag(holder);
    }else{
        holder = (Viewholder)convertView.getTag();
    }
    // For position zero i have to set the text color to blue color,else dark gray color
    if(position==0){
        holder.name.setTextColor(context.getResources().getColor(R.color.blue_color));
    }else{
        holder.name.setTextColor(context.getResources().getColor(R.color.darkgray));
    }
    // setting the name on the textview
    holder.name.setText(list.get(position).getTypeOfTruckName());
    //setting the tag on edittext
    holder.nmbrOfTruck.setTag(position);
    //setting the viewholder position
    holder.ref = position;

   // clicking on listview making edittext to appear (initially edittext is invisiable)
    // if edittext is visiable make it invisiable and if it is invisiable make it visiable
    convertView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if (v.findViewById(R.id.et_nmbr_of_truck_id).getVisibility() == View.VISIBLE) {
                v.findViewById(R.id.et_nmbr_of_truck_id).setVisibility(View.INVISIBLE);
            } else {
                v.findViewById(R.id.et_nmbr_of_truck_id).setVisibility(View.VISIBLE);
            }

        }
    });
    // if user write on editText save the input by the user in specified position
    //truckHashMap is hashmap where I saving the position as a key and value as the user Input

    holder.nmbrOfTruck.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence s, int start,
                                  int before, int count) {

        }

        public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after) {
            // TODO Auto-generated method stub

        }

        public void afterTextChanged(Editable s) {
            Current = new String[holder.ref];

            truckHashMap.put(position, s.toString().trim());
        }
    });
    // Setting the User Input at specified Position
    holder.nmbrOfTruck.setText(truckHashMap.get(position));

    Config.colorFont(context, null, holder.name, null);
    return convertView;
}

Upvotes: 0

Views: 57

Answers (2)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75798

Where your else condition ?

    if(position==0)
    {
              holder.name.setTextColor(context.getResources().getColor(R.color.blue_color));
    } else {

    // Calling when if Condition not Satisfied .
             holder.name.setTextColor(context.getResources().getColor("Your_Color"));
            }

Upvotes: 0

Ivan Ivanov
Ivan Ivanov

Reputation: 912

You have to use else statement which set your default text color in your adapter:

...
if(position==0){
            holder.name.setTextColor(context.getResources().getColor(R.color.blue_color));
        } else {
          // here set your default text color black for example
          holder.name.setTextColor(context.getResources().getColor(R.color.black_color));
}
...

Hope it helps!

Upvotes: 2

Related Questions