自定义列表视图,用于显示 sd 卡中的文件

Customized listview for dispaly files from sdcard

我正在使用自定义列表视图列出 SD 卡中的文件,每行有两个按钮。下面是我的代码。

public class SDcardFiles extends BaseAdapter{

    private ArrayList<File> mSdFilesList;
    private LayoutInflater mInflator;
    File myfilename;

    public SDcardFiles(File filename){

      this.myfilename = filename;
      mSdFilesList = new ArrayList<File>();
      mInflator = FileList.this.getLayoutInflater();

      File list[] = myfilename.listFiles();

    for( int i=0; i< list.length; i++)
      {
      File f = list[i].getAbsoluteFile();
      mSdFilesList.add(f);      
     }      
    }    

    @Override
    public int getCount() { 
        return mSdFilesList.size();     

        }

   @Override
   public Object getItem(int position) {    
      return mSdFilesList.get(position);
       }

  @Override
  public long getItemId(int position) {     
    return position;
       }

      @Override
     public View getView(int position, View view, ViewGroup parent) {
    ViewHolder viewHolder;
     if (view == null) {
       view = mInflator.inflate(R.layout.listitem_sdfiles, null);
       viewHolder = new ViewHolder();
       viewHolder.fileName = (TextView)view.findViewById(R.id.file_name);
       viewHolder.bt_encrypt = (Button)view.findViewById(R.id.encrypt);
       viewHolder.bt_decrypt = (Button)view.findViewById(R.id.decrypt);
       view.setTag(viewHolder);

          } else {
                 viewHolder = (ViewHolder) view.getTag();
          }

        mFileName = mSdFilesList.get(position);
        mFN = mFileName.getName();
        System.out.println("filename in position====>"+mFN);
        if(mFN != null && mFN.length() > 0){
         viewHolder.fileName.setText(mFN);
         }

    viewHolder.bt_encrypt.setOnClickListener(new OnClickListener() {
    @Override
     public void onClick(View v) {
        System.out.println("inside encryption====>"+mFN);
         }
                });     

在这里,我可以从特定文件夹的 SD 卡中获取文件列表,每行有两个按钮。我也收到 name of the file from position of the list(i.e Line number : 35)but problem is when i clicked a button of particular row it is always printing last row file name but not that row which i pressed button。谁能帮帮我

谢谢

您不应在 getView() 方法中设置 OnClickListener,而是使用方法 ListView.setOnItemClickListener()

在接口实现中可以通过position获取数据项,并显示点击项对应的数据。

在 getView() 方法中使用:

viewHolder.bt_encrypt = (Button)view.findViewById(R.id.encrypt);
viewHolder.bt_encript.setTag(position);

并在 onClick 中使用:

@Override
    public void onClick(View view) {
        String mStringData = (String) view.getTag();

在你的按钮点击监听器中,应该是:

viewHolder.bt_encrypt.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
        ViweHolder holder = (ViewHolder)v.getTag();
        System.out.println("inside encryption====>"+ holder.fileName.getText());
   }

}); 
 @Override
    public View getView(int position, View view, ViewGroup parent) {
    ViewHolder viewHolder;
   if (view == null) 
  {
      view = mInflator.inflate(R.layout.listitem_sdfiles, null);
      viewHolder = new ViewHolder();
      viewHolder.fileName = (TextView)view.findViewById(R.id.file_name);
      viewHolder.bt_encrypt = (Button)view.findViewById(R.id.encrypt);
      viewHolder.bt_decrypt = (Button)view.findViewById(R.id.decrypt);
      view.setTag(viewHolder);

      } else {
              viewHolder = (ViewHolder) view.getTag();
            }

      mFileName = mSdFilesList.get(position);
      mFN = mFileName.getName();
      System.out.println("filename in position====>"+mFN);
      if(mFN != null && mFN.length() > 0)
      {
        viewHolder.fileName.setText(mFN);

        viewHolder.bt_encript.setTag(mFN);
   }

 viewHolder.bt_encrypt.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {

         if(view.getTag()!= null)
         {
            String fileName =  (String) view.getTag();
            System.out.println("inside encryption====>"+fileName);
         }
     }


  });