2014-10-08 5 views
0

일부 이미지 (.jpg) 및 pdf (.pdf)를 sdcard의 내 app 폴더에 저장합니다. 여러번이나이목록보기의 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; 
    } 

} 

// 주요 활동에 파악를 탈에서 도움을 필요로 목록보기이 채워 얻을하지만 난 내 목록보기에서 PDF 항목을 클릭하고 때, JPG 년대와 PDF의 항목을 모두리스트 뷰에 표시되고있다

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를 지웠다. 그것이 미래에 누군가를 돕기를 희망한다.

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

시도해도 여전히 목록보기에 반복되는 데이터가 채워집니다. – artist

0

난 당신이 위의 게시 된 코드에서 어떤 문제를 찾을 수 없습니다.

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

"dataclass"를 정적으로 선언 했으므로 코드의 다른 부분에서 수정하지 않도록하십시오.

+0

나는 수업을 수정하지 않을 것이다. 내가 이해하지 못하는 것은 왜 listview의 데이터가 반복 되는가하는 것입니다. – artist

+0

listview의 항목 수 = "dataclass"arraylist의 항목 수입니다. 내가 말하고자하는 것은 클릭 이벤트가 listview에서 수행 될 때 정적 arraylist 변수 dataclass가 수정되지 않았는지 확인하는 것입니다. –

+0

내가 방금 주목 한 또 다른 것은 목록보기에서 스크롤 할 때 항목의 위치가 변경됩니다. – artist

관련 문제