使用索引位置发送图像

时间:2013-12-16 05:54:42

标签: java android bluetooth

我通过蓝牙将图像发送到其他设备点击列表项,但每次我的设备发送最后一个索引图像,如:我的ListView中有3个图像,它总是发送第3张图像,即使我点击第1张图像列表中的发送按钮....

String fileName;
String strPath;
int position ;
List <String> ImageList;
File f;
File file;


    public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    LayoutInflater inflater = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);      

    if (convertView == null) {
    convertView = inflater.inflate(R.layout.list_upload, null);
    }       

    // ColImgName
    TextView txtName = (TextView) convertView.findViewById(R.id.ColImgName);
    strPath = ImageList.get(position).toString();

    // Get File Name
    fileName = strPath.substring( strPath.lastIndexOf('/')+1, strPath.length() );
    file = new File(strPath);
    @SuppressWarnings("unused")
    long length = file.length();
    txtName.setText(fileName);

    // Image Resource
    ImageView imageView = (ImageView) convertView.findViewById(R.id.ColImgPath);
    final BitmapFactory.Options options = new BitmapFactory.Options();

    options.inSampleSize = 8;

    Bitmap bm = BitmapFactory.decodeFile(strPath,options);
    imageView.setImageBitmap(bm);


    // ColStatus
    final ImageView txtStatus = (ImageView) convertView.findViewById(R.id.ColStatus);
    txtStatus.setImageResource(R.drawable.bullet_button);

    // progressBar
    final ProgressBar progress = (ProgressBar) convertView.findViewById(R.id.progressBar);
    progress.setVisibility(View.GONE);

    //btnUpload
    final ImageButton btnUpload = (ImageButton) convertView.findViewById(R.id.btnUpload);
    btnUpload.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
    // Upload
    btnUpload.setEnabled(false);

    startUpload(position);
    }
    });

    // Data
    final ImageButton btnData = (ImageButton) convertView.findViewById(R.id.btnData);
    btnData.setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        public void onClick(View v) {

            fileName=ImageList.get(position).toString().substring(strPath.lastIndexOf('/')+1, strPath.length());
            showDialog(DIALOG_LOGIN);
        }
    });     


    final ImageButton btnSend = (ImageButton) convertView.findViewById(R.id.btnSend);
    btnSend.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setComponent(new ComponentName(
            "com.android.bluetooth",
            "com.android.bluetooth.opp.BluetoothOppLauncherActivity"));
        intent.setType("image/jpeg");
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        startActivity(intent);

        }
    });

    return convertView;

    }   
} 

2 个答案:

答案 0 :(得分:1)

您应该使用setTag()方法并将File对象分配给该按钮。然后,当单击该按钮时,getTag() File对象并使用它..

更新您要附加onClickListener的代码部分,如下所示:

final ImageButton btnSend = (ImageButton) convertView.findViewById(R.id.btnSend);
btnSend.setTag(new File(strPath)); // btn knows which file is related to it
btnSend.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        File newFile = (File) btnSend.getTag(); // get the related file on click

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setComponent(new ComponentName(
            "com.android.bluetooth",
            "com.android.bluetooth.opp.BluetoothOppLauncherActivity"));
        intent.setType("image/jpeg");
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(newFile)); // use it
        startActivity(intent);

    }
});

答案 1 :(得分:0)

使用此

查找列表视图位置
final int position = mylistview.getPositionForView(((Layout) view.getParent()));