Java ViewHolder的使用提供了不寻常的输出
我使用的是Java ViewHolder的使用提供了不寻常的输出,java,android,listview,android-view,baseadapter,Java,Android,Listview,Android View,Baseadapter,我使用的是自定义列表视图,其中每行包含两个图像视图和一个文本视图。我在getView()自定义适配器的方法中使用view holder。我已经在getview()方法中的一个图像视图上设置了Onclicklistener(),也在listview上的活动中设置了onSelectItemListener()。现在,当我点击imageview时,它会影响到特定的行,但当我向下滚动时,我会看到随机的行受到点击的影响。当我单击imageview时会发生这种情况,但当我不使用viewholder时不会发生
自定义列表视图
,其中每行包含两个图像视图和一个文本视图。我在getView()
自定义适配器的方法中使用view holder
。我已经在getview()
方法中的一个图像视图上设置了Onclicklistener()
,也在listview
上的活动中设置了onSelectItemListener()
。现在,当我点击imageview
时,它会影响到特定的行,但当我向下滚动时,我会看到随机的行受到点击的影响。当我单击imageview
时会发生这种情况,但当我不使用viewholder
时不会发生这种情况。我真的不明白为什么会这样。请帮帮我
下面是我的getView()
方法代码:
public View getView(int position, View convertView, ViewGroup parent) {
final int myposition = position;
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.fileviewrowfrnview_layout, null);
ViewHolder holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.icon2);
holder.text = (TextView) convertView.findViewById(R.id.file_name2);
holder.download = (ImageView) convertView.findViewById(R.id.play_buttonOropen2);
convertView.setTag(holder);
}
final ViewHolder holder = (ViewHolder) convertView.getTag();
holder.download.setOnClickListener(new View.OnClickListener() {
@
Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (frnshare_list.get(myposition).getFileType().trim()
.equalsIgnoreCase("file")) {
// it is file
if (frnshare_list.get(myposition).getDownloadStatus().trim()
.equalsIgnoreCase("0")) {
// is is not a downloaded file do download it
holder.icon.setVisibility(View.GONE);
holder.download.setVisibility(View.GONE);
holder.text.setTextSize(14);
Log.e("file to be download", "" + frnshare_list.get(myposition).getFileName());
if (frnshare_list.get(myposition).getFileName().length() > 15)
holder.text.setText(frnshare_list.get(myposition).getFileName().subSequence(0, 15) + " will be downloaded soon.");
else
holder.text.setText(frnshare_list.get(myposition).getFileName() + " will be downloaded soon.");
UserService us = new UserService(context);
Frnshared fdto = frnshare_list.get(myposition);
fdto.setDownloadStatus("2"); // in queue
fdto.setMessageId("0");
us.updateDowloadStatusById(fdto);
us.updatemsgIdbyId(fdto);
if (CommonUtility.isNetworkAvailable(context)) {
AsyncTask < Void, Void, Void > httpconnection = new AsyncTask < Void, Void, Void > () {@
Override
protected Void doInBackground(Void...params) {
// TODO Auto-generated method stub
NetworkCommunication nc = new NetworkCommunication(
context);
try {
UserService us = new UserService(
context);
Frnshared fdto = frnshare_list
.get(myposition);
Log.i("file to be download", "" + fdto.getFileName());
fdto.setDownloadStatus("2"); // in queue
fdto.setMessageId("0");
us.updateDowloadStatusById(fdto);
us.updatemsgIdbyId(fdto);
String response = nc
.MyHttpPostDownload(frnshare_list
.get(myposition));
ParsersAndDataInsertion.DownloadRequestResponseParser(
response, context,
frnshare_list.get(myposition));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// http call for download
return null;
}
}.execute(null, null, null);
}
Apitable(adto);
}
} else if (frnshare_list.get(myposition)
.getDownloadStatus().equalsIgnoreCase("1")) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(frnshare_list.get(myposition)
.getFileLocation());
intent.setDataAndType(Uri.fromFile(file), "audio/*");
context.startActivity(intent);
} else if (frnshare_list.get(myposition)
.getDownloadStatus().equalsIgnoreCase("2")) {
Toast.makeText(context, "already request snet", 2000)
.show();
}
} else if (frnshare_list.get(myposition).getFileType()
.equalsIgnoreCase("folder")) {}
}
});
// view purpose
String file_name = frnshare_list.get(position).getFileName();
if (file_name.length() > 15)
holder.text.setText(file_name.substring(0, 15) + "");
else
holder.text.setText(file_name); // frn name
try {
if (frnshare_list.get(myposition).getFileType()
.equalsIgnoreCase("file")) { // it is file
holder.icon.setImageResource(R.drawable.mp3_icon);
if (frnshare_list.get(myposition).getDownloadStatus()
.equalsIgnoreCase("0")) {
Log.i("file is ", "" + frnshare_list.get(myposition).getFileName());
holder.download.setImageResource(R.drawable.download_png);
} else if (frnshare_list.get(myposition).getDownloadStatus()
.equalsIgnoreCase("1")) {
} else if (frnshare_list.get(myposition).getDownloadStatus()
.equalsIgnoreCase("2")) {
Log.i("file is in queue", "" + frnshare_list.get(myposition).getFileName());
holder.icon.setVisibility(View.GONE);
holder.download.setVisibility(View.GONE);
holder.text.setTextSize(14);
if (frnshare_list.get(myposition).getFileName().length() > 15)
holder.text.setText(frnshare_list.get(myposition).getFileName().substring(0, 1);
else
holder.text.setText(frnshare_list.get(myposition).getFileName() );
}
} else if (frnshare_list.get(myposition).getFileType()
.equalsIgnoreCase("folder")) {
holder.icon.setImageResource(R.drawable.folder);
}
} catch (Exception e) {
// TODO: handle exception
Intent i = new Intent(context, TabViewActivity.class);
i.putExtra("show", "frntab");
context.startActivity(i);
BugSenseHandler.sendException(e);
e.printStackTrace();
}
return convertView;
}
public View getView(int位置、视图转换视图、视图组父视图){
最终int myposition=位置;
//TODO自动生成的方法存根
if(convertView==null){
LayoutFlater充气机=LayoutFlater.from(上下文);
convertView=充气机。充气(R.layout.fileviewrowfrnview\u布局,null);
ViewHolder=新的ViewHolder();
holder.icon=(ImageView)convertView.findViewById(R.id.icon2);
holder.text=(TextView)convertView.findViewById(R.id.file_name2);
holder.download=(ImageView)convertView.findViewById(R.id.play_按钮打开2);
convertView.setTag(支架);
}
final ViewHolder=(ViewHolder)convertView.getTag();
holder.download.setOnClickListener(新视图.OnClickListener(){
@
推翻
公共void onClick(视图v){
//TODO自动生成的方法存根
if(frnshare_list.get(myposition).getFileType().trim()
.equalsIgnoreCase(“文件”)){
//这是文件
if(frnshare_list.get(myposition).getDownloadStatus().trim()
.equalsIgnoreCase(“0”)){
//不是下载的文件,请下载它
holder.icon.setVisibility(View.GONE);
holder.download.setVisibility(View.GONE);
holder.text.setTextSize(14);
Log.e(“要下载的文件”,“”+frnshare_list.get(myposition.getFileName());
if(frnshare_list.get(myposition).getFileName().length()>15)
holder.text.setText(frnshare_list.get(myposition).getFileName()。子序列(0,15)+“将很快下载”);
其他的
holder.text.setText(frnshare_list.get(myposition).getFileName()+“将很快下载”);
UserService us=新的UserService(上下文);
Frnshared fdto=frnshare_list.get(我的位置);
fdto.setDownloadStatus(“2”);//在队列中
fdto.setMessageId(“0”);
美国更新的WloadStatusById(fdto);
美国最新数据(fdto);
if(CommonUtility.isNetworkAvailable(上下文)){
AsyncTaskhttpconnection=新建AsyncTask(){@
推翻
受保护的Void doInBackground(Void…参数){
//TODO自动生成的方法存根
网络通信nc=新的网络通信(
上下文);
试一试{
UserService us=新的UserService(
上下文);
FRN共享fdto=FRN共享列表
.获得(我的职位);
Log.i(“要下载的文件”,“”+fdto.getFileName());
fdto.setDownloadStatus(“2”);//在队列中
fdto.setMessageId(“0”);
美国更新的WloadStatusById(fdto);
美国最新数据(fdto);
字符串响应=nc
.MyHttpPostDownload(frnshare_列表
.get(我的位置));
ParsersAndDataInsertion.DownloadRequestResponseParser(
回应、背景、,
frnshare_list.get(我的位置));
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//http请求下载
返回null;
}
}.执行(空,空,空);
}
Apitable(adto);
}
}else if(frnshare_list.get)(我的位置)
.getDownloadStatus().equalsIgnoreCase(“1”)){
意图=新意图();
setAction(android.content.intent.ACTION\u视图);
File File=新文件(frnshare\u list.get(myposition)
.getFileLocation());
intent.setDataAndType(Uri.fromFile(file),“audio/*”;
背景。开始触觉(意图);
}else if(frnshare_list.get)(我的位置)
.getDownloadStatus().equalsIgnoreCase(“2”)){
Toast.makeText(上下文,“已请求snet”,2000年)
.show();
}
}else if(frnshare_list.get(myposition).getFileType()
.equalsIgnoreCase(“文件夹”){}
}
});
//查看目的
String file_name=frnshare_list.get(position).getFileName();
如果(文件名.length()>15)
holder.text.setText(文件名.substring(0,
if(position==1){
//here change color or hide view
holder.someView.setVisibility(View.Invisible);
holder.someView.setColor ==== green; //forgive me abt syntx
}else{
//Here in else part it is important that you must reverse above things
//so that when you scroll list, GREEN color do not show up in random rows
holder.someView.setVisibility(View.Visible);
holder.someView.setColor ==== red; //forgive me abt syntx
}
//same is for on click listener etc