我有一個ListView適配器如下:Android的列表視圖適配器,帶滾動
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
private Context mContext;
private LayoutInflater inflater;
private ArrayList<ApplicationPojo> applicationList = null;
private Activity activity;
private Bitmap logoImg;
private ViewHolder holder;
private String logoName;
//private String fileName;
private PackageManager pm;
private boolean isAppInstalled;
public ListViewAdapter(Context context, ArrayList<ApplicationPojo> applicationList) {
this.mContext = context;
this.activity = (Activity)mContext;
this.applicationList = applicationList;
this.inflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
return applicationList.size();
}
@Override
public ApplicationPojo getItem(int position) {
return applicationList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent)
{
if (view == null)
{
holder = new ViewHolder();
view = inflater.inflate(R.layout.singleapp, null);
holder.appName = (TextView) view.findViewById(R.id.appName);
holder.developer = (TextView) view.findViewById(R.id.developer);
holder.rate = (TextView) view.findViewById(R.id.rateApp);
holder.updateBtn = (ImageView) view.findViewById(R.id.updateApp);
holder.installBtn = (ImageView) view.findViewById(R.id.install);
holder.openInstalledApp = (ImageView) view.findViewById(R.id.openInstalledApp);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
}
try
{
final View finalView = view;
holder.logo = (ImageView) finalView.findViewById(R.id.appLogo);
logoName = applicationList.get(position).getLogo();
Picasso.with(activity)
.load(IPClass.SERVERIP + logoName)
.into(holder.logo);
if(!appInstalled(applicationList.get(position).getFileName().substring(0, applicationList.get(position).getFileName().length() - 4)))
{
if(new File(Environment.getExternalStorageDirectory() + "/download/appsaraai/" + applicationList.get(position).getFileName()).exists())
{
holder.installBtn.setVisibility(View.VISIBLE);
holder.installBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/appsaraai/" + applicationList.get(position).getFileName())), "application/vnd.android.package-archive");
activity.startActivity(intent);
for (int i = 0; i < DownloadLists.list.size(); i++) {
if (DownloadLists.list.get(i).getName().equals(applicationList.get(position).getName()));
{
DownloadLists.list.remove(i);
}
}
}
});
}
else
{
holder.updateBtn.setVisibility(view.VISIBLE);
holder.updateBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
String id = applicationList.get(position).getId();
int category = applicationList.get(position).getCategory();
Intent rec1Intent = new Intent(activity,AppView.class);
activity.startActivity(rec1Intent);
AppView appView = new AppView();
appView.setParameters(id,category);
}
});
}
}
else
{
holder.updateBtn.setVisibility(View.GONE);
holder.openInstalledApp.setVisibility(View.VISIBLE);
holder.openInstalledApp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context ctx = mContext;
Intent mIntent = ctx.getPackageManager().getLaunchIntentForPackage(applicationList.get(position).getFileName());
String mainActivity = mIntent.getComponent().getClassName();
Intent intent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName(applicationList.get(position).getFileName(), mainActivity);
activity.startActivity(intent);
}
});
}
holder.appName.setText(applicationList.get(position).getName());
holder.developer.setText(applicationList.get(position).getDeveloper());
}
catch (Exception ex)
{
Log.d("Adapter Exception", ex.toString());
}
return view;
}
public boolean appInstalled(String checkApp)
{
pm = activity.getPackageManager();
try
{
pm.getPackageInfo(checkApp, PackageManager.GET_ACTIVITIES);
isAppInstalled = true;
}
catch (PackageManager.NameNotFoundException e)
{
isAppInstalled = false;
}
return isAppInstalled;
}
public class ViewHolder
{
ImageView logo;
TextView appName;
TextView developer;
TextView rate;
ImageView updateBtn;
ImageView installBtn;
ImageView openInstalledApp;
}
}
列表中包含的應用程序列表。在這裏,我檢查設備中是否安裝了應用程序,以便在該項目上顯示打開按鈕,並且如果未安裝應用程序,則會顯示下載按鈕,以便用戶可以下載該應用程序。每件事情都很好,直到我滾動列表。當我滾動列表按鈕與任何想法相互重疊!
你可以發佈你的XML佈局?也許截圖會更好地解決問題。 –
getItemId函數似乎沒用,它用於什麼? – dasjkdj