0

I have a ListView with a custom layout named custom_listview.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:padding="5dp"
android:id="@+id/file_name"
/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18dp"
    android:id="@+id/file_path"/>

</LinearLayout>

And this is my CustomAdapter class:

public class CustomAdapter extends BaseAdapter {

ArrayList<File> result;
Context context;

private static LayoutInflater inflater=null;
public CustomAdapter(Activity parentActivity, ArrayList<File> fileList) {
    // TODO Auto-generated constructor stub
    result=fileList;
    context=parentActivity;
   inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return result.size();
}

@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 file_name;
    TextView file_path;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub



    if (convertView == null)
    {
        LayoutInflater mInflater =   (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.custom_listview, null);
    }

    Holder holder=new Holder();
    holder.file_name=(TextView) convertView.findViewById(R.id.file_name);
    holder.file_path=(TextView) convertView.findViewById(R.id.file_path);
    //holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
    holder.file_name.setText(result.get(position).getName());
    holder.file_path.setText(result.get(position).getPath());

    return convertView;
    }

    }

And here is my onClickListener for the ListView:

 public void onItemClick(AdapterView<?> parent, View view, int position,       long id) { 

    int color = Color.TRANSPARENT;
    Drawable background = parent.getChildAt(position).getBackground();
    if (background instanceof ColorDrawable)
        color = ((ColorDrawable) background).getColor();
  if(color == Color.CYAN)
  { parent.getChildAt(position).setBackgroundColor(Color.TRANSPARENT);}
    else {
      parent.getChildAt(position).setBackgroundColor(Color.CYAN);
      TextView t;
      t = (TextView) view.findViewById(R.id.file_path);
      files_selected.addElement(t.getText().toString());

      }
      }
      }

The app works fine when I click on any item on the top of the list. But as soon as I scroll down and click ANY item, the app crashes. Please Help. Thanks!

Pankaj
  • 7,380
  • 6
  • 39
  • 60
Tejas
  • 127
  • 9

2 Answers2

0

You forgot else. Try this. If you are not implementing else part then while scrolling your app get collapsed.

Holder holder;

    if (convertView == null)
        {
     holder=new Holder();
            LayoutInflater mInflater =   (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.custom_listview, null);
     holder.file_name=(TextView) convertView.findViewById(R.id.file_name);
        holder.file_path=(TextView) convertView.findViewById(R.id.file_path);
    convertView.setTag(holder);
        }else{
    holder = (Holder) convertView.getTag();
    }

EDIT: If you are set if condition if (convertView == null) and forgot to set else part then the time of recycling (Scrolling) it will try to recreate and the text changes between rows, crash, etc. all are happen. So whenever you add if condition inside of getView you must add if as well as else.

Amsheer
  • 6,666
  • 8
  • 39
  • 74
  • why does it collapse? – Tejas Jul 08 '15 at 15:01
  • it's saying viewHolder cannot be resolved. And also what should I return from the getView() method? – Tejas Jul 08 '15 at 15:09
  • Oh sorry you need to change holder = (ViewHolder) convertView.getTag(); this to holder = (Holder) convertView.getTag(); – Amsheer Jul 09 '15 at 05:23
  • Check my edit and try my last comment. It will work. – Amsheer Jul 09 '15 at 05:28
  • Now it is scrolling perfectly, but it crashes when I scroll down and CLICK on any item. What exactly is the issue here?thanks for the help in advance. – Tejas Jul 09 '15 at 13:49
  • Hey I was just checking my ListView, and the ListView items are repeating itself after every 4 or 5 items! Serious issue! please help!! – Tejas Jul 09 '15 at 14:20
  • yes. I solved my problem of repititive items as well.Thanks for the help! – Tejas Jul 10 '15 at 08:14
  • then accept the answer and close. If you have another issue ask another question. – Amsheer Jul 10 '15 at 08:40
0

Shouldnt it be;

if (convertView == null)
{
   LayoutInflater mInflater =   (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   convertView = mInflater.inflate(R.layout.custom_listview, null);

   Holder holder=new Holder();
   holder.file_name=(TextView) convertView.findViewById(R.id.file_name);
   holder.file_path=(TextView) convertView.findViewById(R.id.file_path);
   //holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
   holder.file_name.setText(result.get(position).getName());
   holder.file_path.setText(result.get(position).getPath());
}else{ 
   viewHolder = (ViewHolder) view.getTag(); 
}       
view.setTag(viewHolder);
matty357
  • 619
  • 4
  • 15