我正在开发一个项目,我需要将一些纯文本放到剪贴板中,但问题是当我尝试将其粘贴到某个地方时。我试着解决但是徒劳无功。它显示为同一列表的下一个项目。我正在使用customArrayadapter。我的适配器代码如下:
package com.gippy.status99;
import java.util.List;
import java.util.Objects;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class StatusAdapter extends ArrayAdapter<StatusEntity> {
Context context;String copytext;int id;
public StatusAdapter(Context context, int resource,
int textViewResourceId, List<StatusEntity> objects) {
super(context, resource, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=LayoutInflater.from(getContext());
convertView=inflater.inflate(R.layout.status_list_item,parent,false);
TextView txtCategory=(TextView)convertView.findViewById(R.id.txt_status);
Button btn_copy=(Button)convertView.findViewById(R.id.img_btn_copy);
Button btn_share=(Button)convertView.findViewById(R.id.img_btn_share);
StatusEntity obj=getItem(position);
txtCategory.setText(obj.getStatus());
id=obj.getStatusid();
copytext=txtCategory.getText().toString();
btn_copy.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ClipboardManager clipmanager=(ClipboardManager)getContext().getSystemService(getContext().CLIPBOARD_SERVICE);
ClipData clip=ClipData.newPlainText("data",copytext);
Toast.makeText(getContext(), "Copied to clipboard::::"+position+"---"+id, 1000).show();
clipmanager.setPrimaryClip(clip);
}
});
return convertView;
}
}
现在,如果我将尝试复制状态,即我复制了第一项,但是当我粘贴第二项时。如何解决这个问题?
答案 0 :(得分:1)
getView()中的位置变量包含控件请求显示内容的位置信息。当用户与行上的按钮交互时,不会调用getView()。因此,请将位置值保存在btn_copy对象的标记字段中。像这样更改getView():
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Only create view if convertView is null
if(convertView == null) {
LayoutInflater inflater=LayoutInflater.from(getContext());
convertView=inflater.inflate(R.layout.status_list_item,parent,false);
Button btn_copy=(Button)convertView.findViewById(R.id.img_btn_copy);
btn_copy.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Get the saved position
int pos = (int)v.getTag();
StatusEntity obj=getItem(pos);
ClipboardManager clipmanager= (ClipboardManager)getContext().getSystemService(getContext().CLIPBOARD_SERVICE);
ClipData clip=ClipData.newPlainText("data",obj.getStatus());
Toast.makeText(getContext(), "Copied to clipboard::::"+pos+"---"+obj.getStatusid(), 1000).show();
clipmanager.setPrimaryClip(clip);
}
});
}
TextView txtCategory=(TextView)convertView.findViewById(R.id.txt_status);
Button btn_copy=(Button)convertView.findViewById(R.id.img_btn_copy);
Button btn_share=(Button)convertView.findViewById(R.id.img_btn_share);
// Save the position for use in the copy button
btn_copy.setTag(position);
StatusEntity obj=getItem(position);
txtCategory.setText(obj.getStatus());
return convertView;
}