Listview点击事件无效

时间:2014-10-08 17:04:26

标签: android

我在列表视图中显示图像和文件名。使用下面的代码填充图像和文件名,但我无法单击列表视图中的项目。活动只是在列表视图中单击项目时崩溃。请帮忙搞清楚。谢谢

public class ListviewActivity extends Activity
{

public class Data_Adapter extends ArrayAdapter<Data_Class>
{


    Context context;
    int ResourceLayoutId;
    ArrayList<Data_Class> data = null;

    public Data_Adapter(Context c, int r, ArrayList<Data_Class> dc)
    {
        super(c, r, dc);
        this.ResourceLayoutId = r;
        this.context = c;
        this.data = dc;

    }


    public View getView(int position, View convertView, ViewGroup parent)
    {

        View row = convertView;
        DataHolder holder = null;

        if (row == null)
        {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(ResourceLayoutId, parent, false);
            holder = new DataHolder();
            holder.image = (ImageView) row.findViewById(R.id.image1);
            holder.txt = (TextView) row.findViewById(R.id.textlist);
            row.setTag(holder);
        } else
        {

            holder = (DataHolder) row.getTag();

        }

        Data_Class dc = data.get(position);
        holder.txt.setText(dc.data);

        Bitmap bm = decodeSampledBitmapFromUri(data.get(position).get_path(), 100, 100);
        holder.image.setImageBitmap(bm);


        return row;


    }

    public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight)
    {

        Bitmap bm = null;
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        bm = BitmapFactory.decodeFile(path, options);

        return bm;
    }


    public int calculateInSampleSize(

            BitmapFactory.Options options, int reqWidth, int reqHeight)
    {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth)
        {
            if (width > height)
            {
                inSampleSize = Math.round((float) height / (float) reqHeight);
            } else
            {
                inSampleSize = Math.round((float) width / (float) reqWidth);
            }
        }

        return inSampleSize;
    }


    public class DataHolder
    {

        ImageView image;
        TextView txt;
    }

}

private ListView listview1;
private int check_view;
private File targetDirectory;
private File[] files;
protected static ArrayList<Data_Class> dataclass = new ArrayList<Data_Class>();

@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO Auto-generated method stub
    check_view = 0;

    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview_layout);
    listview1 = (ListView) findViewById(R.id.List1);

    String path = Environment.getExternalStorageDirectory().getAbsolutePath();
    String targetPath = path + "/MyApp/";

    targetDirectory = new File(targetPath);
    files = targetDirectory.listFiles();

    for (int i = 0; i < files.length; i++)
    {
        dataclass.add(new Data_Class(files[i].getName(), files[i].getAbsolutePath()));
    }

    Data_Adapter adapter = new Data_Adapter(this, R.layout.img, dataclass);
    listview1.setAdapter(adapter);

    listview1.setClickable(true);

//  activity crashes when trying clicking item in listview
    listview1.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id)
        {
            // TODO Auto-generated method stub
            String path = (String) parent.getItemAtPosition(position);
            Toast.makeText(ListviewActivity.this, path, Toast.LENGTH_LONG).show();

 if(path.contains(".pdf")){

               String path2 = path.substring(path.lastIndexOf("/") + 1);


               File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/" +  path2);
               Intent intent = new Intent(Intent.ACTION_VIEW);
               intent.setDataAndType(Uri.fromFile(file), "application/pdf");
               intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
               Intent intent2 = Intent.createChooser(intent, "Open File");
               try {
                   startActivity(intent2);

               } catch (ActivityNotFoundException e) {

                   Toast.makeText(ListviewActivity.this, "No pdf viewer found. Install one. ", Toast.LENGTH_LONG).show();
               }   


           }

        }








        }
    });
   }
}

// image.xml包含imageview和textview

 <ImageView
    android:id="@+id/image1"
    android:layout_width="50dip"
    android:layout_height="50dip"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"

    />

<TextView
    android:id="@+id/textlist"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:textSize="15dp"
    android:textStyle="bold" />

//和main activity.xml

  <ListView
    android:id="@+id/List1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

</ListView>

1 个答案:

答案 0 :(得分:1)

您的列表视图包含Data_Class而不是String的列表,这会导致崩溃,因此更改

String path = (String) parent.getItemAtPosition(position);

Data_Class  mData_Class = (Data_Class) parent.getItemAtPosition(position);
String path = mData_Class.Name;

使用Name文件名变量更改Data_Class变量。