2014-10-08 87 views
0

我在SD卡上的應用程序文件夾中存儲了一些圖片(.jpg)和pdf(.pdf)。列表視圖被填充,但是當我在PDF文檔中的項目列表視圖我的點擊,被在ListView所示的兩個JPG文件和PDF的項目很多times.I需要幫助搞清楚out.Thanks在onClick列表視圖中出現重複的項目

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 + "/AppName/"; 

    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); 
     listview1.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
       long id) { 
      // TODO Auto-generated method stub 



     Data_Class mData_Class = (Data_Class)parent.getItemAtPosition(position); 

     String path = mData_Class.get_path(); 



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

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

       Intent intent = new Intent(); 
       intent.setAction(Intent.ACTION_VIEW); 
       intent.setDataAndType(Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath() 
         + "/AppName/" + path3), "image/*"); 
       startActivity(intent); 

       //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() + "/AppName/" + 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(); 
       } 


      } 

     } 

        }); 


      } 

    } 

// Data_Class

public class Data_Class { 

public String data; 
public String pic; 
public Data_Class() 
{ 
    super(); 
} 
public Data_Class(String d, String p) 
{ 
    super(); 
    this.data=d; 
    this.pic=p; 
} 

public String Get_Name() 
{ 
    return data; 
} 

public String get_path() 
{ 
    return pic; 
} 

void Set_Name(String s) 
{ 
    data=s; 

} 

回答

0

問題是,在下次加載listview之前,listview中的以前的數據沒有被清除。所以每次活動加載時,我都清除了下面的列表視圖。希望它可以幫助未來的人。

adapter.clear(); 
adapter.notifyDataSetChanged(); 
0

做到這一點,因爲每當你點擊ListView項目getView()方法獲取調用所以沒有必要添加它再次。

YOUT getView()應該是這樣的:

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); 

      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); 


      } 
      else 
      { 

       holder=(DataHolder)row.getTag(); 

      } 
      return row;  

    } 
+0

嘗試過,它仍然在listview中填充重複的數據。 – artist 2014-10-08 18:30:27

0

我無法找到你上面貼的代碼中的任何問題。

protected static ArrayList<Data_Class> dataclass=new ArrayList<Data_Class>(); 

正如你所宣稱的「數據類」靜態確保你不會在代碼的其他部分進行修改。

+0

我不修改這個類。我不明白的是爲什麼重複列表視圖中的數據? – artist 2014-10-08 18:49:21

+0

listview中的項目數量=「dataclass」數組列表中的項目數量。我的意思是說,確保在listview上執行單擊事件時,不會修改static arraylist變量dataclass。 – 2014-10-08 18:53:55

+0

我剛剛注意到的另一件事是,當我在列表視圖中滾動時,項目的位置會發生變化。 – artist 2014-10-08 18:56:53