View Image in One activity using SqlLite Database - android

i'm using SqlLite Database to store this image, then retrieve it.
If i'm click Next button the new image will be display, but the activity never change. (Sorry my English). image : http://www.pixentral.com/show.php?picture=101dALzioHZuNTHvJ3Vz9UBqk1NrlK0

--- EDITED ---
public class MainActivity extends Activity {
int imgNumber = 1;
...
ImageView imgView = (ImageView)findViewById(R.id.imageview);
imgView.setImageResource(R.drawable.image1);
...
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (imgNumber==0) {
imgView.setImageResource(R.drawable.image1);
imgNumber++;
} else if(imgNumber==1) {
imgView.setImageResource(R.drawable.image2);
imgNumber++;
} else if(imgNumber==2) {
imgView.setImageResource(R.drawable.image3);
imgNumber++;
} else if(imgNumber==3) {
imgView.setImageResource(R.drawable.image4);
imgNumber++;
} else if(imgNumber==4) {
imgView.setImageResource(R.drawable.image5);
imgNumber = 0;
}
}
});
...
}

Related

Interface callback after startactivity

I have fragment that contains recyclerview and products inside of it. When click row opens a new activity (SuggestionActivity). This activity also contains recyclerview inside of suggested products. When I click this products selecting or deselecting. Under the recyclerview I have button and when clicked this button it should be listen first recyclerview adapter and notifying data.
Ex:
ProductsFragment -> RecyclerAdapter -> Click Row -> Open Activity -> Click one or multiple rows and select -> Click Button -> Close activity and notify fragment adapter
My first recyclerview inside fragment
public class MyView extends RecyclerView.ViewHolder {
public MyView(View view) {
super(view);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SuggestionActivity x = new SuggestionActivity();
x.setTab2AdapterListener(new SuggestionActivity.Tab2AdapterListener() {
#Override
public void interfaceClicked() {
Log.d("interfaceClicked", "interfaceClicked"); // This line doesn't work!
}
});
Intent i = new Intent(mContext, SuggestionActivity.class);
i.putExtra("first", String.valueOf(tag));
i.putExtra("second", String.valueOf(getAdapterPosition()));
mContext.startActivity(i);
}
});
}
}
My Interface defined on SuggestionActivity
private Tab2AdapterListener tab2AdapterListener;
public interface Tab2AdapterListener {
void interfaceClicked();
}
public void setTab2AdapterListener(Tab2AdapterListener tab2AdapterListener) {
this.tab2AdapterListener = tab2AdapterListener;
}
And Button inside SuggestionActivity
btnSendSuggestions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String suggested = "";
for (int i = 0; i < suggestedProductsList.size(); i++) {
if (suggestedProductsList.get(i).getIsSelected())
suggested += suggestedProductsList.get(i).getProductId() + ",";
}
if ( suggested.length() > 0 ) {
suggested = suggested.substring(0, suggested.length() - 1);
}
if (tab2AdapterListener != null)
tab2AdapterListener.interfaceClicked();
// tab2AdapterListener comes null
}
});

How can I have scrolling to last page working properly in a paginated scroll in android ListView

Attention! This question is not about dynamic loading of items into a very long ListView.
This is about adding PageUP and PageDown buttons to a ListView so that user can touch the button and scroll ListView page by page. Page means all fully and partially visible items on the screen.
I have partially implemented this in the following code, but my problem is that when I have lets say 10 items of approximately same height in the listview and 7 of them fit into the first page, then when I press PgDown button, user expects that item 8 to be on top of the screen (next page), but because there are only 10 items, ListView scrolls to the bottom of the list and because there is no extra scroll space I have item number 4 on top.
What is the best solution in this situation?
Should I add one item to the end of the list which will make the last page the height of the screen or there are any better options?
Here is my code:
public class cPaginatedListViewHelper {
Activity m_parentActivity;
private ListView mList;
//controls
private LinearLayout m_PagingLL;
//buttons
private ImageButton m_btnPrevPage;
private ImageButton m_btnNextPage;
private ImageButton m_btnExitPaginatedMode;
public cPaginatedListViewHelper(ListActivity mParent) {
this.m_parentActivity = mParent;
m_btnPrevPage=(ImageButton) mParent.findViewById(R.id.btnPrevPage);
m_btnNextPage=(ImageButton) mParent.findViewById(R.id.btnNextPage);
m_btnExitPaginatedMode =(ImageButton) mParent.findViewById(R.id.btnClosePage);
if(m_btnPrevPage!=null) {
m_btnPrevPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showSiblingPage(-1);
}
});
m_btnPrevPage.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mList.smoothScrollToPosition(0);
return true;
}
}
);
}
if(m_btnNextPage!=null) {
m_btnNextPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showSiblingPage(1);
}
});
m_btnNextPage.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mList.smoothScrollToPosition(mList.getCount());
return true;
}
}
);
}
m_btnExitPaginatedMode.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setEnabled(false);
m_PagingLL.setVisibility(View.GONE);
}
});
mList=mParent.getListView();
m_PagingLL = (LinearLayout) mParent.findViewById(R.id.pageControls);
}
public void updateControlsVisibility()
{
ViewTreeObserver observer = mList.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (willMyListScroll()) {
boolean psm = isEnabled();
//enable or disable
m_PagingLL.setVisibility( psm ? View.VISIBLE : View.GONE);
((View)mList).setVerticalScrollbarPosition(psm ? View.SCROLLBAR_POSITION_LEFT: View.SCROLLBAR_POSITION_RIGHT);
}
else
{
m_PagingLL.setVisibility(View.GONE);
((View)mList).setVerticalScrollbarPosition(View.SCROLLBAR_POSITION_RIGHT);
}
}
});
}
private boolean willMyListScroll() {
try {
int pos = mList.getLastVisiblePosition();
if (mList.getChildAt(pos).getBottom() > mList.getHeight()) {
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
private void showSiblingPage(int shift)
{
if(mList!=null) {
int iScrollPageHeight = mList.getHeight();
mList.scrollListBy(iScrollPageHeight * shift);
}
}
public void setEnabled(boolean psm) {
MyApp.Pref.edit().putBoolean("PSModeEnabled", psm).commit();
}
public boolean isEnabled(){
return MyApp.Pref.getBoolean("PSModeEnabled", false);
}
public void pagedScrollEnableDisable() {
boolean pagingEnabled = isEnabled();
pagingEnabled=!pagingEnabled;
setEnabled(pagingEnabled);
m_PagingLL.setVisibility( pagingEnabled ? View.VISIBLE : View.GONE);
updateControlsVisibility();
}
}
I finished up with using ListView's footer with variable height as shown in the following code:
LayoutInflater inflater = m_parentActivity.getLayoutInflater();
m_footerView = inflater.inflate(R.layout.listview_paged_overscroll, mList, false );
ViewGroup.LayoutParams lp =m_footerView.getLayoutParams();
if(m_tvPageNum!=null) recalcPagination();
if(lp!=null) lp.height = m_extraScrollFooterHeight;
int iFooters = mList.getFooterViewsCount();
if(iFooters==0) mList.addFooterView(m_footerView);

Reduce memory setting image to imageview

I am showing a series of questions to the user. In each question I set drawables to 6 imageviews. The only problem is the ram usage goes up about 60MB ever time I go to the next screen.
I dont understand why this is happening because I am just replacing the picture with a different one? After about 5 screens the app crashing because of failed memory allocation. How would I release this memory of the screen before it so It doesnt build up.
Thanks in advance.
public class Test extends ActionBarActivity {
ImageButton a1;
ImageButton a2;
ImageButton a3;
ImageButton a4;
Intent result;
String correctOrWrong = "";
int question;
int amountOfC = 0;
int numCorrect;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
getSupportActionBar().hide();
ImageView qMain = (ImageView) findViewById(R.id.qMain);
a1 = (ImageButton) findViewById(R.id.a1);
a2 = (ImageButton) findViewById(R.id.a2);
a3 = (ImageButton) findViewById(R.id.a3);
a4 = (ImageButton) findViewById(R.id.a4);
ImageView qTitle = (ImageView) findViewById(R.id.qTitle);
a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
nextQ(1);
}
});
a2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
nextQ(2);
}
});
a3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
nextQ(3);
}
});
a4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
nextQ(4);
}
});
Intent qGet = getIntent();
question = qGet.getIntExtra("QNUMBER", 1);
numCorrect = qGet.getIntExtra("NUMCORRECT", 1);
Log.d("lel", String.valueOf(question));
if (question == 1) {
qMain.setBackgroundResource(R.drawable.q1);
a1.setBackgroundResource(R.drawable.a13);
a2.setBackgroundResource(R.drawable.a12);
a3.setBackgroundResource(R.drawable.a11);
a4.setBackgroundResource(R.drawable.ca1);
qTitle.setBackgroundResource(R.drawable.q1t);
} else if (question == 2) {
qMain.setBackgroundResource(R.drawable.q2);
a1.setBackgroundResource(R.drawable.q23);
a2.setBackgroundResource(R.drawable.q2a);
a3.setBackgroundResource(R.drawable.q22);
a4.setBackgroundResource(R.drawable.q21);
qTitle.setBackgroundResource(R.drawable.q2t);
} else if (question == 3) {
qMain.setBackgroundResource(R.drawable.q3);
a1.setBackgroundResource(R.drawable.q31);
a2.setBackgroundResource(R.drawable.q32);
a3.setBackgroundResource(R.drawable.q33);
a4.setBackgroundResource(R.drawable.q3a);
qTitle.setBackgroundResource(R.drawable.q3t);
} else if (question == 4) {
qMain.setBackgroundResource(R.drawable.q4);
a1.setBackgroundResource(R.drawable.q4a);
a2.setBackgroundResource(R.drawable.q43);
a3.setBackgroundResource(R.drawable.q42);
a4.setBackgroundResource(R.drawable.q41);
qTitle.setBackgroundResource(R.drawable.q4t);
} else if (question == 5) {
qMain.setBackgroundResource(R.drawable.q5);
a1.setBackgroundResource(R.drawable.q53);
a2.setBackgroundResource(R.drawable.q5a);
a3.setBackgroundResource(R.drawable.q52);
a4.setBackgroundResource(R.drawable.q51);
qTitle.setBackgroundResource(R.drawable.q5t);
don't handle image memory/caching yourself, instead use a library such as glide - https://github.com/bumptech/glide
or
try clearing the imageview onDestroy() of the acitvity
a1.setImageDrawable(null);
a2.setImageDrawable(null);
a3.setImageDrawable(null);
a4.setImageDrawable(null);
you need to call recycle() for every bitmap after you used them like below :
a1.setImageDrawable(null);
a2.setImageDrawable(null);
a3.setImageDrawable(null);
a4.setImageDrawable(null);
a1.recycle();
a2.recycle();
a3.recycle();
a4.recycle();

Selected Item has to be added to cart in android

Already I have some static data in expanded list view and from that list if any customer click on that multiple items they has to be selected and it has to be added into cart.So for that please give me some suggestions and if u have any implemented code please post here.
Thanks in advance.
Assume there is two button for add and remove item on cart screen so both have click event on adapter class below is just sample example
holder.imgAddItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCartDetail mCartDetail;
if (Utility.mCartList.containsKey(mcategoryProductDetail.productdetails.get(0).psid)) {
mCartDetail = Utility.mCartList.get(mcategoryProductDetail.productdetails.get(0).psid);
int finalMmaxBuy = 0;
if (!mCartDetail.categoryProductDetail.max_buy_qty.equalsIgnoreCase(" ")) {
finalMmaxBuy = Integer.parseInt(mCartDetail.categoryProductDetail.max_buy_qty);
}
if (mCartDetail.addQuantity < finalMmaxBuy) {
mCartDetail.addQuantity++;
}
} else {
mCartDetail = new mCartDetail();
mCartDetail.categoryProductDetail = mcategoryProductDetail.productdetails.get(0);
mCartDetail.addQuantity = 1;
Utility.mCartList.put(mcategoryProductDetail.productdetails.get(0).psid, mCartDetail);
}
mCartDetail.totalprice = Float.parseFloat(mCartDetail.categoryProductDetail.our_price) * mCartDetail.addQuantity;
holder1.tvProductCounter.setText(String.valueOf(mCartDetail.addQuantity));
}
});
holder.imgRemoveItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (Utility.mCartList.containsKey(mcategoryProductDetail.productdetails.get(0).psid)) {
mCartDetail mCartDetail = Utility.mCartList.get(mcategoryProductDetail.productdetails.get(0).psid);
mCartDetail.addQuantity--;
mCartDetail.totalprice = Float.parseFloat(mCartDetail.categoryProductDetail.our_price) * mCartDetail.addQuantity;
holder1.tvProductCounter.setText(String.valueOf(mCartDetail.addQuantity));
if (mCartDetail.addQuantity == 0) {
Utility.mCartList.remove(mCartDetail.categoryProductDetail.psid);
notifyDataSetChanged();
}
}
});
and below is my model class and hashmap for data storing and send to server
public static HashMap<String, CartDetail> mCartList;
public CartDetail mCartDetail;
Hope this concept will help you to implement in your scenario

Android test if it having data else return to first

I have android apps:
Send data to Textview (working)
check if data is there getdata from text view(working)
Else if data not in Textview check again ??
Thanks.
button1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
//enter text to TextView (tv1) need some time
EntertextToTextView();
//label1
if (tv1.getText().toString() != "")
{
tv2.setText(tv1.getText().toString());
}
else {
// goto label1;
}
}
}
});

Categories

Resources