2013-05-22 77 views
0

因此,我要創建一個列表視圖,其中每行有一個Imageview(用於預覽圖像),一個textView和一個按鈕(用於刪除圖像) 。自定義列表視圖Imageview,TextView和一個按鈕作爲項目

問題:

在按鈕的項目&的點擊點擊我生成,顯示項目的任何#敬酒。

但有時代碼正常工作,有時它只顯示錯誤的項目編號。

更新:

我忘了補充一點,在TextView中顯示的值是:「標誌:#」,其中#是項目的當前計數。對此我無法理解永遠是正確的,但不是烤麪包的消息,我得到:■

後臺工作:

有通過其用戶可以點擊照片頁面頂部的按鈕,當他返回從背部攝像頭的應用程序列表視圖得到更新

代碼:

public class PicMgr extends ListActivity { 

    public String storeID; 

    public String date; 

    TextView picMsg; 

    DBAdapter dbEngine = new DBAdapter(this); 

    ProgressDialog PDpicTasker; 
    private static final String DATASUBDIRECTORY = "pics"; 
    private static int TAKE_PICTURE = 1; 
    public String fIMGname; 
    public static String outletpicpathIO; 
    private Uri outputFileUri; 
    public String fnameIMG; 

    public static String[] imgs; 
    public static String[] comments; 

    private EfficientAdapter adap; 

    public static String[] dataLV; 

private void TakePhoto() { 


     fIMGname = storeID + "_"+ date; 

     dbEngine.open(); 
     //String fIMGname; 
         int countExistingPics = 0; 
         countExistingPics = dbEngine.getExistingPicNos(storeID); 
         countExistingPics = countExistingPics + 1; 
         fIMGname = fIMGname + "_" +countExistingPics; 
         dbEngine.close(); 

     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

     File dirORIGimg = new File(Environment.getExternalStorageDirectory(),DATASUBDIRECTORY); 
     if (!dirORIGimg.exists()) { 
      dirORIGimg.mkdirs(); 
     } 

     //newfilename = "newfilename123"; 
     File file = new File(dirORIGimg, fIMGname + ".jpg"); 

     //Toast.makeText(getApplicationContext(), "inside TakePhoto(): "+newfilename, Toast.LENGTH_SHORT).show(); 

     outputFileUri = Uri.fromFile(file); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
     startActivityForResult(intent, TAKE_PICTURE); 

    } 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    System.out.println("GetPic- onActivityResult - resultCode: "+resultCode); 

    if (requestCode == TAKE_PICTURE && resultCode == Activity.RESULT_OK) { 
     // ShowMessage(outputFileUri.toString()); 
     //if (data != null) { 
      Toast.makeText(getApplicationContext(),outputFileUri.toString(), Toast.LENGTH_SHORT).show(); 
      System.out.println("outputFileUri.toStr: "+outputFileUri.toString()); 

      outletpicpathIO = outputFileUri.toString().trim(); 

      if(!outletpicpathIO.isEmpty()){ 
       try { 
        new GetCompressedPic().execute().get(); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (ExecutionException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 


     //} 
    } 

} 

    private class GetPicPath extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      Log.i("GetPicPathTasker", "Ready to get start -GetCompressedPic-"); 

     } 

     @Override 
     protected Void doInBackground(Void... params) { 

      try { 
      runOnUiThread(new Runnable() { 
        public void run() { 
         try { 
          TakePhoto(); // get pic 
         } catch (Exception e) { 
          e.printStackTrace(); 
         } 
        } 
       }); 




      } catch (Exception e) { 
       Log.i("GetPicPathTasker", "GetCompressedPic Failed!", e); 
      } 

      finally { 

       Log.i("GetPicPathTasker", "GetCompressedPic Completed..."); 
      } 
      return null; 
     } 

     @Override 
     protected void onCancelled() { 
      Log.i("GetPicPathTasker", "GetCompressedPic Cancelled"); 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      Log.i("GetPicPathTasker", "GetCompressedPic cycle completed"); 

     } 
    } 

    private class GetCompressedPic extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      Log.i("PicBGtasker", "Ready to get start -GetCompressedPic-"); 

      PDpicTasker = ProgressDialog.show(PicMgr.this, null, null); 
      //PDpicTasker.setContentView(R.layout.loader); 
      PDpicTasker.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 

      try { 
       Options opts = new BitmapFactory.Options(); 
       opts.inSampleSize = 2; // for 1/2 the image to be loaded 
       //Bitmap finalBitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(outletpicpathIO, opts), 640, 480, false); 
       System.out.println("outletpicpathIO: "+outletpicpathIO); 

       fnameIMG = new File(outletpicpathIO).getAbsolutePath(); 
       fnameIMG = fnameIMG.replace("/file:", ""); 
       fnameIMG = fnameIMG.trim(); 
       System.out.println("fnameIMG: "+fnameIMG); 


       Bitmap finalBitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(fnameIMG), 640, 480, false); 
       //Bitmap finalBitmap = BitmapFactory.decodeFile(outletpicpathIO); 

       dbEngine.open(); 

       dbEngine.savePicPath(storeID, fnameIMG); 
       dbEngine.close(); 

       ByteArrayOutputStream streamIMG = new ByteArrayOutputStream(); 
       File dir = new File(Environment.getExternalStorageDirectory(),DATASUBDIRECTORY); 
       if (!dir.exists()) { 
        dir.mkdirs(); 
       } 


       String exportFileName = fIMGname+".jpg"; 
       File fileIMG = new File(dir, exportFileName); 
       fileIMG.createNewFile(); 

       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 50, streamIMG); 

       FileOutputStream fo = new FileOutputStream(fileIMG); 
       fo.write(streamIMG.toByteArray()); 
       fo.close(); 

      } catch (Exception e) { 
       Log.i("PicBGtasker", "GetCompressedPic Failed!", e); 
      } 

      finally { 

       Log.i("PicBGtasker", "GetCompressedPic Completed..."); 
      } 
      return null; 
     } 

     @Override 
     protected void onCancelled() { 
      Log.i("PicBGtasker", "GetCompressedPic Cancelled"); 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      Log.i("PicBGtasker", "GetCompressedPic cycle completed"); 
      PDpicTasker.dismiss(); 
     } 
    } 
    // >< 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_pic_mgr); 


     Intent StoreData = getIntent(); 

     storeID = StoreData.getStringExtra("storeID"); 

     date = StoreData.getStringExtra("date"); 

     System.out.println("storeID >> "+storeID); 

     picMsg = (TextView)findViewById(R.id.textView1_PicMsg); 


     Button outletPic = (Button) findViewById(R.id.Button01_outletpic); 
     outletPic.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v2) { 

       //TakePhoto(); // get pic 

       //async(S) 
       try { 
        new GetPicPath().execute().get(); 
       } catch (InterruptedException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
       } catch (ExecutionException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
       } 

      } 
     }); 

     Button saveNcont = (Button) findViewById(R.id.picMgr_saveNcont); 
     saveNcont.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v2) { 

       /*dbEngine.open(); 
       dbEngine.savePicPath(storeID, fnameIMG); 
       dbEngine.close();*/ 

       // save comments 

      } 
     }); 

     Button backk = (Button) findViewById(R.id.picMgr_back); 
     backk.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v2) { 

        finish(); 

       } 
      }); 


    } 

    @Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 
     super.onResume(); 

     // 

     picMsg.setText("Please Click on \"Take Photograph\" button to start taking Pictures"); 

     dbEngine.open(); 
     int maxLIMIT = dbEngine.getExistingPicNos(storeID); 

     System.out.println("maxLIMIT existing pics count: "+maxLIMIT); 

     if(maxLIMIT > 0){ 

      imgs = dbEngine.getImgsPath(storeID); 
      comments = dbEngine.getImgsComment(storeID); 


      dataLV=new String[maxLIMIT]; 
      for(int limitx = 0; limitx < maxLIMIT; limitx++){ 

       dataLV[limitx]=""+limitx; 
      } 


      System.out.println("dataLV:"+dataLV.toString()); 

      picMsg.setText("Please \"Delete\" non-required Photographs shown below \nor Click on \"Take Photograph\" button to start taking more Pictures"); 


      adap = new EfficientAdapter(this); 
      setListAdapter(adap); 

     } 


     // 
     dbEngine.close(); 
    } 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     // TODO Auto-generated method stub 
     super.onListItemClick(l, v, position, id); 
     Toast.makeText(this, "Click-" + String.valueOf(position), 
       Toast.LENGTH_SHORT).show(); 
    } 

    public static class EfficientAdapter extends BaseAdapter implements 
      Filterable { 
     private LayoutInflater mInflater; 
     private Bitmap mIcon1; 
     private Context context; 

     public EfficientAdapter(Context context) { 
      // Cache the LayoutInflate to avoid asking for a new one each time. 
      mInflater = LayoutInflater.from(context); 
      this.context = context; 
     } 

     /** 
     * Make a view to hold each row. 
     * 
     * @see android.widget.ListAdapter#getView(int, android.view.View, 
     *  android.view.ViewGroup) 
     */ 
     public View getView(final int position, View convertView, 
       ViewGroup parent) { 
      // A ViewHolder keeps references to children views to avoid 
      // unneccessary calls 
      // to findViewById() on each row. 
      ViewHolder holder; 
      // When convertView is not null, we can reuse it directly, there is 
      // no need 
      // to reinflate it. We only inflate a new View when the convertView 
      // supplied 
      // by ListView is null. 
      if (convertView == null) { 
       convertView = mInflater.inflate(R.layout.listview_layout, null); 
       // Creates a ViewHolder and store references to the two children 
       // views 
       // we want to bind data to. 
       holder = new ViewHolder(); 
       holder.textLine = (TextView) convertView 
         .findViewById(R.id.textLine); 
       holder.iconLine = (ImageView) convertView 
         .findViewById(R.id.iconLine); 

       holder.txt = (EditText) convertView.findViewById(R.id.txt); 

       holder.buttonLine = (Button) convertView 
         .findViewById(R.id.buttonLine); 

       convertView.setOnClickListener(new OnClickListener() { 
        private int pos = position; 

        @Override 
        public void onClick(View v) { 
         Toast.makeText(context, "Click-" + String.valueOf(pos), 
           Toast.LENGTH_SHORT).show(); 
        } 
       }); 
       holder.buttonLine.setOnClickListener(new OnClickListener() { 
        private int pos = position; 

        @Override 
        public void onClick(View v) { 
         Toast.makeText(context, 
           "Delete-" + String.valueOf(pos), 
           Toast.LENGTH_SHORT).show(); 
        } 
       }); 

       convertView.setTag(holder); 
      } else { 
       // Get the ViewHolder back to get fast access to the TextView 
       // and the ImageView. 
       holder = (ViewHolder) convertView.getTag(); 
      } 
      // Get flag name and id 
      String filename = "flag_" + String.valueOf(position); 
      /* 
      * int id = context.getResources().getIdentifier(filename, 
      * "drawable", context.getString(R.string.package_str)); 
      */ 

      mIcon1 = BitmapFactory.decodeFile(imgs[position]); 

      // Bind the data efficiently with the holder. 
      holder.iconLine.setImageBitmap(mIcon1); 
      holder.textLine.setText("flag " + String.valueOf(position)); 

      holder.txt.setText(comments[position]); 
      return convertView; 
     } 

     static class ViewHolder { 
      TextView textLine; 
      ImageView iconLine; 
      EditText txt; 
      Button buttonLine; 
     } 

     @Override 
     public Filter getFilter() { 
      // TODO Auto-generated method stub 
      return null; 
     } 

     @Override 
     public long getItemId(int position) { 
      // TODO Auto-generated method stub 
      return 0; 
     } 

     @Override 
     public int getCount() { 
      // TODO Auto-generated method stub 
      return dataLV.length; 
     } 

     @Override 
     public Object getItem(int position) { 
      // TODO Auto-generated method stub 
      //String sendBack = PicMgr.dataLV[position]; 
      return dataLV[position]; 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_pic_mgr, menu); 
     return true; 
    } 

} 

任何意見/建議表示讚賞...

+0

明確您的評論code..so,這將是容易的助手..! – TheFlash

+0

@Pratik allrigh..its now updated:D – beerBear

回答

1
  1. ListView有一個方法setOnItemClickListener(),你不必做convertView.setOnClickListener如果你想爲行的監聽器。
  2. 只有當轉換視圖爲空時,您纔在該按鈕上設置偵聽器。這意味着當convertView得到回收時您不會更改聽衆(這就是爲什麼它不能很好地工作)。你應該做這樣的事情:

公共查看getView(最終詮釋的立場,觀點convertView, 的ViewGroup父){ ViewHolder持有人; (convertView == null){ convertView = mInflater.inflate(R.layout.listview_layout,null); holder = new ViewHolder(); holder.textLine =(TextView)convertView .findViewById(R.id.textLine); holder.iconLine =(ImageView)convertView .findViewById(R.id.iconLine);

holder.txt = (EditText) convertView.findViewById(R.id.txt); 

    holder.buttonLine = (Button) convertView 
      .findViewById(R.id.buttonLine); 

    holder.buttonLine.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Toast.makeText(context, 
        "Delete-" + v.getTag().toString(), 
        Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    convertView.setTag(holder); 
} else { 
    // Get the ViewHolder back to get fast access to the TextView 
    // and the ImageView. 
    holder = (ViewHolder) convertView.getTag(); 
} 
holder.buttonLine.setTag(position); 
//rest of the method 
return convertView; 

}

+0

Thx用於回答詳細的解決方案:)所以,這意味着只是註釋掉我的'convertView.setOnClickListener'會做的伎倆?其餘的代碼保持不變(就像我在你的文章中看到的那樣)。我幾乎忘記了LV自己的'onItemclicklistener' thingy:D – beerBear

+0

還有一件事,如果你在按鈕中看到點擊監聽器,我不再使用該位置,我已經使用了按鈕的標籤,然後我在「else」下面設置按鈕的標籤。 – bogdan

1
holder.buttonLine.setTag(position); 
holder.buttonLine.setOnClickListener(new OnClickListener() { 
      private int pos = position; 

      @Override 
      public void onClick(View v) { 
       Toast.makeText(context,"Delete-" + String.valueOf(holder.buttonLine.getTag().toString()), 
       Toast.LENGTH_SHORT).show(); 
      } 
     }); 
+0

嗨thx發佈您的解決方案。接受bogdan的帖子,因爲他提供了錯誤的解釋:D。仍+1 – beerBear

相關問題