简体   繁体   English

Listview点击事件不起作用

[英]Listview click event not working

I am displaying images and file names in my list view. 我在列表视图中显示图像和文件名。 With the below code images and file names are populated but I am unable to click items in the listview.The activity just crashes on the click of item in the listview. 使用下面的代码填充图像和文件名,但是我无法单击列表视图中的项目。单击列表视图中的项目只会导致活动崩溃。 Please help in figuring out.Thanks 请帮忙弄清楚。谢谢

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 which contains imageview and textview // 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" />

// and main activity.xml //和main activity.xml

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

</ListView>

Your listview contains list of Data_Class not String ,which causes crash,So Change 您的列表视图包含Data_Class而不是String列表,这会导致崩溃,因此请更改

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

to

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

Change Name variable with your Data_Class file name variable. 用您的Data_Class文件名变量更改Name变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM