Refresh list view data with viewHolder - android-listview

I'm working on a project that has a list view should update automatically with database changes, I'm working on it for a long time and find a solution that using ViewHolder pattern, and I tried this way and it's work really! but now I try manyyyy solution to do it again, it's not working!! pls help me or tell me if there is a tip in using this pattern.
Thanks really!
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) {
}
}
}
}
}

if you want to set adapter then follow this code.
adapter=new AuctionListAdapter(getActivity(), listAuctionBeans,progressBar,1);
setListAdapter(adapter);
if you wan to refresh list with new data change follow this
adapter.addData(getActivity(), listAuctionBeans);
adapter.notifyDataSetChanged();
and adapter is you your d1Adapter.

Related

camera preview showing blank screen

public class cameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private Activity act;
private int currentCameraId;
private boolean cameraFront;
private boolean InPreview;
//private SurfaceView SV;
public cameraPreview(Context context, Activity A, SurfaceView SFV) {
super(context);
mCamera = null;
mHolder = null;
InPreview = false;
act = A;
mHolder = SFV.getHolder();
mHolder.addCallback(this);
initialzeCamera();
}
private void initialzeCamera()
{
initializeCameraBool();
OpenCamera();
}
public void OpenCamera()
{
if(mCamera==null)
{
Log.e("open", "sesami");
if(cameraFront)
mCamera.open(cameraPreview.findFrontFacingCamera());
else
mCamera.open(cameraPreview.findBackFacingCamera());
}
//StartPreview();
}
private void StartPreview()
{
if (mHolder.getSurface() == null || mCamera == null){
Log.e("Stop", "in the name of the law");
return;
}
Log.e("Started", "from the bottom");
if(InPreview)
{
mCamera.stopPreview();
InPreview = false;
}
setCameraDisplayOrientation();
if(!InPreview)
{
try {
mCamera.setPreviewDisplay(mHolder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mCamera.startPreview();
InPreview = true;
}
}
public void releaseCamera()
{
if (mCamera != null) {
if(InPreview)
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
public void surfaceCreated(SurfaceHolder holder) {
StartPreview();
}
public void surfaceDestroyed(SurfaceHolder holder) {
releaseCamera();
}
public void setCameraDisplayOrientation() {
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(currentCameraId, info);
int rotation = act.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
if(mCamera!=null)
mCamera.setDisplayOrientation(result);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
StartPreview();
}
public static int findBackFacingCamera() {
int cameraId = -1;
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
cameraId = i;
break;
}
}
return cameraId;
}
public static int findFrontFacingCamera() {
int cameraId = -1;
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
cameraId = i;
break;
}
}
return cameraId;
}
private void initializeCameraBool()
{
if(cameraPreview.findBackFacingCamera()>=0)
cameraFront = false;
else
cameraFront = true;
}
}
So the surfaceview that I pass in comes from the main activity and it's getting it from the layout. The layout is just a surfaceview. When I launch it though it only shows a black screen. I don't understand why this is happening. It should be launching the preview display but for some reason it doesn't show it.

Hide the Title bar while scrolling and Show the TItle bar after Scrolling?

I want to hide the title bar when i scrolling the items in the ListView and i want to show the title bar after scrolling. Suggest any ideas to solve this issue.
First add the Xml View into ActionBar like this:
LayoutInflater inflater = (LayoutInflater) getActionBar()
.getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View customActionBarView = inflater.inflate(R.layout.main, null);
final ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(
ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME);
actionBar.setCustomView(customActionBarView,
new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
setContentView(R.layout.main);
Then do the changes in onScrollStateChanged() method:
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case SCROLL_STATE_IDLE:
actionBar.show();
break;
case SCROLL_STATE_TOUCH_SCROLL:
actionBar.hide();
break;
}
}
//declare this two globally
public static int ch = 0, cht = 1;
int myLastVisiblePos;
//Then add onScrollListener to your ListView
list.setOnScrollListener(new OnScrollListener() {
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int currentFirstVisPos = view.getFirstVisiblePosition();
if (currentFirstVisPos > myLastVisiblePos) {
if (ch == 1) {
ch++;
cht = 1;
getActionBar().hide();
} else if (ch == 0) {
getActionBar().show();
ch++;
}
}
if (currentFirstVisPos < myLastVisiblePos)
if (cht == 1)
getActionBar().show();
myLastVisiblePos = currentFirstVisPos;
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
});
This solution worked for me very good:
// mLastFirstVisibleItem defined globally
quranTextList.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
/**
* Hide actionbar when scroll down
*/
if (mLastFirstVisibleItem < firstVisibleItem)
if (getSupportActionBar().isShowing())
getSupportActionBar().hide();
if (mLastFirstVisibleItem > firstVisibleItem)
if (!getSupportActionBar().isShowing())
getSupportActionBar().show();
mLastFirstVisibleItem = firstVisibleItem;
}
});
Source: Android ActionBar hide/show when scrolling list view

How to implement OnClickListener of ListView items for good performance (avoiding slow scrolling)

I've been reading about ViewHolder Pattern and it's effects on ListView scrolling performance lately.
For a smooth scrolling, fast ListView should i avoid using OnClickListener registerations inside adapter getView() method such as:
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
{
holder = new ViewHolder();
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(mResourceId, null);
holder.btn1 = (TextView) convertView.findViewById(R.id.btn1);
holder.img1 = (TextView) convertView.findViewById(R.id.img1);
convertView.setTag(holder);
} else { holder = (ViewHolder) convertView.getTag(); }
final Items item = getItem(position);
holder.btn1.setText(item.btnText);
holder.img1.setBackgroundResource(item.imgSource);
holder.img1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) { /* .. my code block USING POSITION ARG .. */ }
}
holder.btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) { /* .. My Code Block USING POSITION ARG .. */ }
}
return convertView;
}
If so, is registering a OnItemClickListener to ListView instance as following sample does a good practice:
myListView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
if (view.getId() == R.id.btn1)
{
// my code block USING POSITION ARG
}
else if (view.getId() == R.id.img1)
{
// my code block USING POSITION ARG
}
}
});
It's better to use a Wrapper to access to your View and to define your OnClickListener earlier (and outside the adapter for a better usability).
The following sample show how to handle 2 clickable View on one single item of the ListView with good performance:
public class ItemAdapter extends BaseAdapter {
private List<Item> items;
private ItemWrapper wrapper = null;
private OnClickListener onMyItemClickListener1;
private OnClickListener onMyItemClickListener2;
public ItemAdapter(Context context, List<Item> items, OnClickListener onMyItemClickListener1, OnClickListener onMyItemClickListener2) {
this.inflater = LayoutInflater.from(context);
this.items = items;
this.onMyItemClickListener1 = onMyItemClickListener1;
this.onMyItemClickListener2 = onMyItemClickListener2;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public synchronized View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
row = inflater.inflate( R.layout.item, null);
wrapper = new ItemWrapper(row);
row.setTag(wrapper);
} else {
wrapper = (ItemWrapper)row.getTag();
}
Item item = getItem(position);
wrapper.getClickView1().setOnClickListener(onMyItemClickListener1);
wrapper.getClickView2().setOnClickListener(onMyItemClickListener2);
return(row);
}
}
public class ItemWrapper {
private View baseView;
private View clickView1;
private View clickView2;
public ItemWrapper(View baseView) {
this.baseView = baseView;
}
public View getClickView1() {
if ( clickView1 == null) {
clickView1 = (View)baseView.findViewById(R.id.clickView1);
}
return(clickView1);
}
public View getClickView2() {
if ( clickView2 == null) {
clickView2 = (View)baseView.findViewById(R.id.clickView2);
}
return(clickView2);
}
}

setId() and getId() on custom listView item giving unexpected result

I have created custom listview in which i have
textView,imageView.
I want to set id to the each row's imageView so that when i click on that image,can get the id(for the purpose of uniqueness).I have also a click listener in getView.So I want to get that id on click of that particular image but when i click on that image everytime i am getting 3,I don't no why this is happening.Any help would be appreciable.
public View getView(int position, View convertView, ViewGroup parent) {
final CruunNotificationModel cruun = userDetails.get(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.imagewithlist, parent,
false);
holder = new ViewHolder();
holder.userName = (TextView) convertView
.findViewById(R.id.userNameTextView);
holder.userTask = (TextView) convertView
.findViewById(R.id.userTaskTextView);
holder.dateTime = (TextView) convertView
.findViewById(R.id.dateTimeTextView);
holder.userComments = (TextView) convertView
.findViewById(R.id.userCommentsTextView);
holder.userImage = (ImageView) convertView
.findViewById(R.id.userImageImageView);
holder.expandImage = (ImageView) convertView
.findViewById(R.id.expandTextImageView);
holder.commentPart = (LinearLayout) convertView
.findViewById(R.id.commentlayout);
holder.view = (View) convertView.findViewById(R.id.view);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.userComments.setText(cruun.getComments());
holder.userName.setText(cruun.getUserName());
holder.userTask.setText(cruun.getUserTask());
holder.dateTime.setText(cruun.getDateAndTime());
holder.userImage.setImageResource(cruun.getImageId());
holder.expandImage.setImageResource(R.drawable.down_arrow);
holder.expandImage.setId(position);
holder.expandImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.userComments.setEllipsize(null);
RelativeLayout.LayoutParams lParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lParams.setMargins(2, 10, 2, 0);
holder.userComments.setMaxLines(Integer.MAX_VALUE);
lParams.addRule(RelativeLayout.BELOW, R.id.userImageLayout);
holder.userComments.setLayoutParams(lParams);
holder.expandImage.setImageResource(R.drawable.up_arrow);
notifyDataSetChanged();
if (v.getId() == holder.expandImage.getId()) {
holder.view.setVisibility(View.VISIBLE);
holder.commentPart.setVisibility(View.VISIBLE);
}
}
});
return convertView;
}

Load a Listview In a Fragment using Tabs (Actionbarsherlock)

I have a main activity (FragmentDemoActivity) which creates 2 tabs. Fragment A and Fragment B.
I have another class called CategoryList which loads a customlist from an sqlite database.
I am using this example which works great for the TAB parts.
I want to load the CategoryList into the Fragment A.
My CategoryList class is as follows
// All the imports
public class CategoryList extends BaseActivity{
ImageView imgFeedback;
ImageView imgAbout;
ListView listCategories;
ProgressBar prgLoading;
TextView txtAlert;
AdView ads;
static String[] CategoryName ;
ListAdapter la;
static int[] id;
List<String> catList;
static DBHelper dbhelper;
class ListAdapter extends BaseAdapter {
private LayoutInflater inflater;
#SuppressWarnings("unused")
private Context ctx;
public ListAdapter(Context context) {
inflater = LayoutInflater.from(context);
ctx = context;
}
#Override
public int getCount() {
return CategoryName.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = inflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.imgPreview = (ImageView) convertView.findViewById(R.id.imgPreview);
holder.txtRecipeName = (TextView) convertView.findViewById(R.id.txtRecipeName);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.imgPreview.setImageResource(getResources().
getIdentifier(CategoryName[position], "drawable", getPackageName()));
String categoryName = CategoryName[position];
categoryName = categoryName.toLowerCase();
categoryName = Character.toString(categoryName.charAt(0)).toUpperCase()+categoryName.substring(1);
holder.txtRecipeName.setText(categoryName);
return convertView;
}
class ViewHolder {
ImageView imgPreview;
TextView txtRecipeName;
}
}
final Context context = this ;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.categories_list);
AppRater.displayAsk2Rate(this, 1, 1, false);
getSupportActionBar().setHomeButtonEnabled(true);
dbhelper = new DBHelper(this);
try {
dbhelper.createDataBase();
}catch(IOException ioe){
throw new Error("Unable to create database");
}
/** then, the database will be open to use */
try{
dbhelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
CategoryName = dbhelper.getAllCategories();
dbhelper.close();
catList = Arrays.asList(CategoryName);
/* imgAbout = (ImageView) findViewById(R.id.imgAbout);
imgAbout.setOnClickListener(new ButtonListner(CategoryList.this));
imgFeedback = (ImageView) findViewById(R.id.imgFeedback);
imgFeedback.setOnClickListener(new ButtonListner(CategoryList.this));*/
listCategories = (ListView) findViewById(R.id.listCategories);
la = new ListAdapter(CategoryList.this);
listCategories.setAdapter(la);
txtAlert = (TextView) findViewById(R.id.txtAlert);
ads = (AdView) findViewById(R.id.ads);
Ads.loadAds(ads);
listCategories.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
String category = catList.get(position);
Intent i = new Intent(CategoryList.this, RecipesList.class);
i.putExtra("Category_Name", category);
startActivity(i);
}
});
}
}
My FragmentA is as follows
public class FragmentA extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved)
{
View view = inflater.inflate(R.layout.categories_list, container, false);
ListView listCategories = (ListView) getActivity().findViewById(R.id.listCategories);
ListAdapter lav = new ListAdapter(CategoryList.this);
listCategories.setAdapter(lav);
return view;
}
#Override
public void onActivityCreated (Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
}
}
Any pointers ?