我正在开发一个项目,列表视图应该自动更新数据库更改,我正在研究它很长一段时间并找到一个使用ViewHolder模式的解决方案,我尝试了这种方式,它真的很有效!但现在我尝试了许多解决方案再做一次,它不起作用!!请帮助我或告诉我是否有使用此模式的提示。 真谢谢!
public class DownloadItem_ViewHolder {
private boolean hasInited = false;
public DownloadItem_ViewHolder() {
}
public DownloadItem_ViewHolder(View convertView) {
if (convertView != null) {
App_icon = (ImageView) convertView
.findViewById(R.id.Download_app_icon);
App_name = (TextView) convertView
.findViewById(R.id.Downlaod_app_name);
Downloaded_percent = (TextView) convertView
.findViewById(R.id.Downloaded_app_percent);
Downloaded_progress = (ProgressBar) convertView
.findViewById(R.id.Downloaded_app_progress);
Pause_btn = (ImageView) convertView
.findViewById(R.id.Download_pause_btn);
Resume_btn = (ImageView) convertView
.findViewById(R.id.Download_Resume_btn);
hasInited = true;
}
}
public void setPorgress(int progress) {
Downloaded_progress.setProgress(progress);
// Downloaded_percent.setText(String.valueOf(progress) + "%");
}
public void setData(DownloadManagerItem_Cls dlItem) {
if (hasInited) {
// this.App_icon.setBackground(newDownloadManager.getDownloadItemApp(
// dlItem.getItemID()).getAppIcon());
// App_name.setText(newDownloadManager.getDownloadItemApp(
// dlItem.getItemID()).getAppName()
// + "-->" + dlItem.getItemID());
this.Downloaded_progress
.setProgress(dlItem.getDownloadPercentage());
// this.Downloaded_percent.setText(String.valueOf(dlItem
// .getDownloadPercentage()) + "%");
// DownloadStatus downloadItemStatus = dlItem.getDownloadStatus();
// // PAUSE
// if (downloadItemStatus.equals(DownloadStatus.Pause)) {
// // Resume
// Resume_btn.setVisibility(View.VISIBLE);
// // Pause
// Pause_btn.setVisibility(View.GONE);
// } else if (downloadItemStatus.equals(DownloadStatus.Downloading))
// {// Resume
// Resume_btn.setVisibility(View.GONE);
// // Pause
// Pause_btn.setVisibility(View.VISIBLE);
// } else if (downloadItemStatus.equals(DownloadStatus.Complete)) {
// // Resume
// Resume_btn.setVisibility(View.GONE);
// // Pause
// Pause_btn.setVisibility(View.GONE);
// // Install or Run
// }
}
}
ImageView App_icon;
public TextView App_name;
ProgressBar Downloaded_progress;
TextView Downloaded_percent;
public ImageView Resume_btn;
public ImageView Pause_btn;
}
public class DownloadItemAdpater extends ArrayAdapter<DownloadManagerItem_Cls> {
int _rowResourceID;
public DownloadItemAdpater(Context context, int rowResourceID,
List<DownloadManagerItem_Cls> DownloadedItems) {
super(context, rowResourceID, DownloadedItems);
_rowResourceID = rowResourceID;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// final int AppID = DownloadManager.DownloadedList.get(position).ApkID;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.download_history_item, null);
// store the holder with the view.
// convertView.setTag(1, viewHolder);
}
convertView.setTag(getItem(position).getItemID());
DownloadItem_ViewHolder viewHolder = new DownloadItem_ViewHolder(
convertView);
viewHolder.setData(getItem(position));
viewHolder.App_name.setText(String.valueOf(getItem(position)
.getItemID()));
// set data
// viewHolder.Pause_btn.setOnClickListener(new DownloadBtnListener(
// position, viewHolder));
// viewHolder.Resume_btn.setOnClickListener(new DownloadBtnListener(
// position, viewHolder));
return convertView;
}
public class DownloadManager_Act extends ListActivity {
DownloadItemAdpater dlAdapter;
ArrayList<DownloadManagerItem_Cls> listHistory;
// @Override
// protected void onDestroy() {
// unregisterReceiver(mReceiver);
// super.onDestroy();
// }
MyReceiver mReceiver;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dl_manager);
listHistory = CP_Connector.GDM_QueueTable_Connector.getDownloadList();
dlAdapter = new DownloadItemAdpater(getApplicationContext(),
R.layout.download_history_item, listHistory);
setListAdapter(dlAdapter);
mReceiver = new MyReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("com.glx.appscenter.dlmanager");
registerReceiver(mReceiver, filter);
}
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null
&& intent.getAction()
.equals("com.glx.appscenter.dlmanager")) {
try {
int dlItemID = Integer.valueOf(intent
.getStringExtra("itemID"));
if (dlItemID >= 0) {
DownloadManagerItem_Cls dlTempItem = CP_Connector.GDM_QueueTable_Connector
.getDownloadItemByID(dlItemID);
View v = getListView().findViewWithTag(
dlTempItem.getItemID());
TextView tv = (TextView) v
.findViewById(R.id.Downlaod_app_name);
Utility.appUtility.ShowToast(String.valueOf(tv
.getText() + ":" + dlItemID));
DownloadItem_ViewHolder viewHolder = new DownloadItem_ViewHolder(
v);
viewHolder.setPorgress(dlTempItem
.getDownloadPercentage());
dlAdapter.notifyDataSetChanged();
}
} catch (Exception e) {
}
}
}
}
}
答案 0 :(得分:0)
如果要设置适配器,请遵循此代码。
adapter=new AuctionListAdapter(getActivity(), listAuctionBeans,progressBar,1);
setListAdapter(adapter);
如果你想用新的数据更改刷新列表,请按照
进行操作adapter.addData(getActivity(), listAuctionBeans);
adapter.notifyDataSetChanged();
和适配器就是你的d1Adapter。