我有一个名为custom _ ListView
的自定义布局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>
这是我的CustomAdapter
课程:
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;
}
}
以下是onClickListener
的{{1}}:
ListView
当我点击列表顶部的任何项目时,该应用程序正常工作。但是只要我向下滚动并单击任何项目,应用程序就会崩溃。请帮忙。谢谢!
答案 0 :(得分:0)
你忘了别的。试试这个。如果您没有实现其他部分,那么在滚动您的应用时会崩溃。
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();
}
编辑:如果设置了条件if (convertView == null)
并忘记设置其他部分然后recycling
(滚动)的时间,它将尝试重新创建并且文本更改所有行都发生在行之间,崩溃等。因此,无论何时在getView中添加if条件,都必须添加if。否则。
答案 1 :(得分:0)
不应该;
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);