in what order of onClick events executed for Button - android

I have Activity with 20 buttons and GridView.
One Button clear GridView and other add to GridView different values.
Some time I push 1-Add, 2-Clear, 3-Add but see that button clear becomes pushed(Orange color) and executed 1-Add, 2-Add, 3-Clear.
Some time button clear becomes pushed long time, and don't answer at all, but other buttons work.
How is it possible?
My activity:
public class Smena extends Activity
{
public Nomenklatura Asortiment;
GridView gvMain;
GridView gvMain1;
GridView gvMain2;
ArrayAdapter<String> adapter;
ArrayAdapter<String> adapter1;
ArrayAdapter<String> adapter2;
double Sum=0;
String Sdacha;
String filename="shops345";
Boolean PushX=false;
TextView tv1;
void Save()
{
LocalPersistence.witeObjectToFile(this, Asortiment, filename);
}
public void myClickHandler(View target){
Intent intent1;
switch (target.getId()){
case R.id.button1:
ButtonOn (Asortiment.SmenaButName[0],Asortiment.SmenaButPrice[0]);
break;
case R.id.button10:
ButtonOn (Asortiment.SmenaButName[9],Asortiment.SmenaButPrice[9]);
break;
case R.id.button11:
ButtonOn (Asortiment.SmenaButName[10],Asortiment.SmenaButPrice[10]);
break;
case R.id.button12:
ButtonOn (Asortiment.SmenaButName[11],Asortiment.SmenaButPrice[11]);
break;
case R.id.button13:
ButtonOn (Asortiment.SmenaButName[12],Asortiment.SmenaButPrice[12]);
break;
case R.id.button14:
ButtonOn (Asortiment.SmenaButName[13],Asortiment.SmenaButPrice[13]);
break;
case R.id.button15:
ButtonOn (Asortiment.SmenaButName[14],Asortiment.SmenaButPrice[14]);
break;
case R.id.button16:
intent1=new Intent(Smena.this,Napitki.class);
Save();
startActivity(intent1);
break;
case R.id.button17:
intent1=new Intent(Smena.this,Zhevacki.class);
Save();
startActivity(intent1);
break;
case R.id.button18:
intent1=new Intent(Smena.this,Dobavki.class);
Save();
startActivity(intent1);
break;
case R.id.button19:
intent1=new Intent(Smena.this,Presmena.class);
Save();
startActivity(intent1);
break;
case R.id.button2:
ButtonOn (Asortiment.SmenaButName[1],Asortiment.SmenaButPrice[1]);
break;
case R.id.button20:
intent1=new Intent(Smena.this,Orp.class);
Asortiment.data.clear();
Asortiment.data1.clear();
Save();
startActivity(intent1);
break;
case R.id.button21:
ButtonDel();
break;
case R.id.button22:
clear();
break;
case R.id.button23:
intent1=new Intent(Smena.this,Sdacha.class);
Save();
startActivity(intent1);
break;
case R.id.button25:
clear();
// checkTv();
break;
case R.id.button3:
ButtonOn (Asortiment.SmenaButName[2],Asortiment.SmenaButPrice[2]);
break;
case R.id.button4:
ButtonOn (Asortiment.SmenaButName[3],Asortiment.SmenaButPrice[3]);
break;
case R.id.button41:
if(PushX)
buttonx(3);
else buttonx(2);PushX=true;
break;
case R.id.button42:
if(PushX)
buttonx(4);
else buttonx(3);PushX=true;
break;
case R.id.button43:
if(PushX)
buttonx(5);
else buttonx(4);PushX=true;
break;
case R.id.button44:
if(PushX)
buttonx(6);
else buttonx(5);PushX=true;
break;
case R.id.button45:
if(PushX)
buttonx(7);
else buttonx(6);PushX=true;
break;
case R.id.button46:
if(PushX)
buttonx(8);
else buttonx(7);PushX=true;
break;
case R.id.button5:
ButtonOn (Asortiment.SmenaButName[4],Asortiment.SmenaButPrice[4]);
break;
case R.id.button6:
ButtonOn (Asortiment.SmenaButName[5],Asortiment.SmenaButPrice[5]);
break;
case R.id.button7:
ButtonOn (Asortiment.SmenaButName[6],Asortiment.SmenaButPrice[6]);
break;
case R.id.button8:
ButtonOn (Asortiment.SmenaButName[7],Asortiment.SmenaButPrice[7]);
break;
case R.id.button9:
ButtonOn (Asortiment.SmenaButName[8],Asortiment.SmenaButPrice[8]);
break;
}
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.smena);
Intent i=getIntent();
Asortiment=(Nomenklatura) LocalPersistence.readObjectFromFile(this, filename);
adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.tvText, Asortiment.data);
adapter1 = new ArrayAdapter<String>(this, R.layout.item, R.id.tvText, Asortiment.data1);
tv1=(TextView) findViewById(R.id.textView1);
gvMain = (GridView) findViewById(R.id.gridView1);
gvMain.setAdapter(adapter);
gvMain1 = (GridView) findViewById(R.id.gridView11);
gvMain1.setAdapter(adapter1);
gvMain2 = (GridView) findViewById(R.id.gridView2);
if(Asortiment.data2.size()<=0) data2clear();
if(i.getExtras().containsKey("Dobavki"))
{
ArrayList<String> DobavkiList=new ArrayList<String>();
DobavkiList=i.getExtras().getStringArrayList("Dobavki") ;
for(String item : DobavkiList)
{
String Art=Asortiment.GetArtByName(item);
String [] np=Asortiment.GetNamePriceByArt(Art);
ButtonOn (np[0],np[1]);
}
}
if(i.getExtras().containsKey("Napitki"))
{
ArrayList<String> NapitkiList=new ArrayList<String>();
NapitkiList=i.getExtras().getStringArrayList("Napitki") ;
for(String item : NapitkiList)
{
String Art=Asortiment.GetArtByName(item);
String [] np=Asortiment.GetNamePriceByArt(Art);
ButtonOn (np[0],np[1]);
}
}
if(i.getExtras().containsKey("Zhevacki"))
{
ArrayList<String> ZhevackiList=new ArrayList<String>();
ZhevackiList=i.getExtras().getStringArrayList("Zhevacki") ;
for(String item : ZhevackiList)
{
String Art=Asortiment.GetArtByName(item);
String [] np=Asortiment.GetNamePriceByArt(Art);
ButtonOn (np[0],np[1]);
}
}
Double sd;
if(i.getExtras().containsKey("Sdacha"))
{
sd=Double.parseDouble(Asortiment.data2.get(1));
Sdacha=i.getExtras().getString("Sdacha");
sd=Double.parseDouble(Sdacha)-sd;
}
else
{
if(Asortiment.data2.size()>0)
{
Sdacha=Asortiment.data2.get(3);
sd=Double.parseDouble(Asortiment.data2.get(1));
if(Double.parseDouble(Asortiment.data2.get(3))>0)
sd=Double.parseDouble(Asortiment.data2.get(3))-sd;
else sd=(double) 0;
}else {sd=(double) 0;Sdacha="0";}
}
Asortiment.data2.set(3, Sdacha);
Asortiment.data2.set(5, sd.toString());
adapter2 = new ArrayAdapter<String>(this, R.layout.item2, R.id.tvText, Asortiment.data2);
gvMain2.setAdapter(adapter2);
}
void buttonx(int k){
if(!Asortiment.LastButton.get(0).equals("0")){
for (int i=0;i<k;i++){
ButtonOn (Asortiment.LastButton.get(0),Asortiment.LastButton.get(1));
}}
}
void checkTv()
{
Date dt1=new Date();
if(dt1.getHours()>19 && Asortiment.LastRequest.getDay()<dt1.getDay() && (Calendar.DAY_OF_WEEK==2 ||Calendar.DAY_OF_WEEK==4 || Calendar.DAY_OF_WEEK==6))
tv1.setText("ОТПРАВЬТЕ ЗАЯВКУ");
else tv1.setText("");
}
void clear()
{
Asortiment.data.clear();
adapter.clear();
Asortiment.data1.clear();
adapter1.clear();
data2clear();
}
void data2clear()
{
Asortiment.data2.set(1, "0");
Asortiment.data2.set(3, "0");
Asortiment.data2.set(5, "0");
gvMain2.setAdapter(adapter2);
}
private void ButtonDel()
{
int cnt= Asortiment.data.size();
if(cnt>0){
Double d=Double.parseDouble(Asortiment.data2.get(1));//1
d=d-Double.parseDouble(Asortiment.data1.get(cnt-1));
Asortiment.data2.set(1, d.toString());
//adapter2 = new ArrayAdapter<String>(this, R.layout.item2, R.id.tvText, Asortiment.data2);//1
gvMain2.setAdapter(adapter2);
Asortiment.data.remove(cnt-1);
Asortiment.data1.remove(cnt-1);
//adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.tvText, Asortiment.data);
//adapter1 = new ArrayAdapter<String>(this, R.layout.item, R.id.tvText, Asortiment.data1);
gvMain.setAdapter(adapter);
gvMain1.setAdapter(adapter1);
}
}
private void ButtonOn (String name, String price)
{
PushX=false;
if(name.length()>15)name=name.substring(0, 15);
Asortiment.LastButton.set(0, name);
Asortiment.LastButton.set(1, price);
Asortiment.data.add(name);
Asortiment.data1.add(price);
gvMain.setAdapter(adapter);
gvMain1.setAdapter(adapter1);
Double d=Double.parseDouble(Asortiment.data2.get(1));
d=Double.parseDouble(price)+d;
Asortiment.data2.set(1, d.toString());
Double sd=Double.parseDouble(Asortiment.data2.get(3));
if (sd>0) {
sd=sd-d;
Asortiment.data2.set(5, sd.toString());
}
gvMain2.setAdapter(adapter2);
}
}

To solve the issue, or even to narrow down to the issue, you need to refactor the code. On each click there are a bunch of things that are happening, you might be over flooding the clicks. Are you giving enough time between clicks?
And also, do consider making methods generic, for example
if(i.getExtras().containsKey("Napitki"))
{
ArrayList<String> NapitkiList=new ArrayList<String>();
NapitkiList=i.getExtras().getStringArrayList("Napitki") ;
for(String item : NapitkiList)
{
String Art=Asortiment.GetArtByName(item);
String [] np=Asortiment.GetNamePriceByArt(Art);
ButtonOn (np[0],np[1]);
}
}
if(i.getExtras().containsKey("Zhevacki"))
{
ArrayList<String> ZhevackiList=new ArrayList<String>();
ZhevackiList=i.getExtras().getStringArrayList("Zhevacki") ;
for(String item : ZhevackiList)
{
String Art=Asortiment.GetArtByName(item);
String [] np=Asortiment.GetNamePriceByArt(Art);
ButtonOn (np[0],np[1]);
}
}
blocks like these can be made as generic methods and may be you would like to follow java conventions.
Just a suggestion :)

Related

How to get Value of String OnClick of a ViewList in Android Studio

I am new to android studio.
I am making an app that lists names of teachers and the subjects thought and then every person has a different class. The teacher names and subjects though are stored in a TreeMap.
I am having difficulty as I want my app to feature sorts, so when outputted the positions of teachers are changed according to the sort. At the moment the items are sorted via hard code alphabetically and a switch case is used and the onclick integer position is obtained. However, I want to make the switch case make a decision according to the string of the itemClicked so whenever a sort is executed when the user clicks the correct class is displayed. Kindly see my code below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView resultsListView = (ListView) findViewById(R.id.results_listview);
TreeMap<String, String> nameSubject = new TreeMap<>();
nameSubject.put("Richard Bonnici", "Maths"); //1
nameSubject.put("Natasha Mifsud", "English"); //2
nameSubject.put("Victor Valdes", "Computing"); //3
nameSubject.put("Jonathan Galea", "Computing"); //4
nameSubject.put("Jerome Micallef", "History"); //5
nameSubject.put("Tarah Galea", "Marketing"); //6
nameSubject.put("Micheal Gonzalez", "Maths"); //7
nameSubject.put("Jason Bartolo", "Chemistry"); //8
nameSubject.put("Jack Grech", "Chemistry"); //9
nameSubject.put("Anna Carbonaro", "Maltese"); //10
nameSubject.put("Kay Azzopardi", "Biology"); //11
nameSubject.put("George Micallef", "Physics"); //12
nameSubject.put("Gabriella Bonnici", "Maths"); //13
Map sortedMap = valueSort(nameSubject);
// Get a set of the entries on the sorted map
Set set = sortedMap.entrySet();
// Get an iterator
Iterator i = set.iterator();
List<HashMap<String, String>> listItems = new ArrayList<>();
SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.list_item,
new String[]{"First Line", "Second Line"},
new int[]{R.id.text1, R.id.text2});
Iterator it = nameSubject.entrySet().iterator();
while (it.hasNext()) {
HashMap<String, String> resultsMap = new HashMap<>();
Map.Entry pair = (Map.Entry) it.next();
resultsMap.put("First Line", pair.getKey().toString());
resultsMap.put("Second Line", pair.getValue().toString());
listItems.add(resultsMap);
}
resultsListView.setAdapter(adapter);
resultsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(android.widget.AdapterView<?> adapterView, View view, int i, long l) {
//
switch (i) {
case 0:
startActivity(new Intent(TeacherList.this, AnnaCarbonaro.class));
break;
case 1:
startActivity(new Intent(TeacherList.this, GabriellaBonnici.class));
break;
case 2:
startActivity(new Intent(TeacherList.this, GeorgeMicallef.class));
break;
case 3:
startActivity(new Intent(TeacherList.this, JackGrech.class));
break;
case 4:
startActivity(new Intent(TeacherList.this, JasonBartolo.class));
break;
case 5:
startActivity(new Intent(TeacherList.this, JeromeMicallef.class));
break;
case 6:
startActivity(new Intent(TeacherList.this, JonathanGalea.class));
break;
case 7:
startActivity(new Intent(TeacherList.this, KayAzzopardi.class));
break;
case 8:
startActivity(new Intent(TeacherList.this, MichealGonzalez.class));
break;
case 9:
startActivity(new Intent(TeacherList.this, NatashaMifsud.class));
break;
case 10:
startActivity(new Intent(TeacherList.this, RichardBonnici.class));
break;
case 11:
startActivity(new Intent(TeacherList.this, TarahGalea.class));
break;
case 12:
startActivity(new Intent(TeacherList.this, VictorValdes.class));
break;
}
}
});
}
}

How to get the Download Manager ,download cancel callback? or else how to hide or remove this cancel from notification?

When a video is being downloaded there is a cancel option to cancel the download.but when i cancel the download i don't get any callback from DownloadManager to update the app UI.
This cancle option is only available in Android 7 .
Or else is there any option to hide or remove the cancel option from there.
After registering your broadcast receiver for DownloadManager.ACTION_DOWNLOAD_COMPLETE, you can check for DownloadManager status in the onReceive() method of the DownloadManager broadcast as follows -
private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
checkDownloadStatus();
}
};
private void checkDownloadStatus(){
// TODO Auto-generated method stub
DownloadManager.Query query = new DownloadManager.Query();
long id = preferenceManager.getLong(strPref_Download_ID, 0);
query.setFilterById(id);
Cursor cursor = downloadManager.query(query);
if(cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = cursor.getInt(columnIndex);
int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
int reason = cursor.getInt(columnReason);
switch(status){
case DownloadManager.STATUS_FAILED:
String failedReason = "";
switch(reason){
case DownloadManager.ERROR_CANNOT_RESUME:
failedReason = "ERROR_CANNOT_RESUME";
break;
case DownloadManager.ERROR_DEVICE_NOT_FOUND:
failedReason = "ERROR_DEVICE_NOT_FOUND";
break;
case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
failedReason = "ERROR_FILE_ALREADY_EXISTS";
break;
case DownloadManager.ERROR_FILE_ERROR:
failedReason = "ERROR_FILE_ERROR";
break;
case DownloadManager.ERROR_HTTP_DATA_ERROR:
failedReason = "ERROR_HTTP_DATA_ERROR";
break;
case DownloadManager.ERROR_INSUFFICIENT_SPACE:
failedReason = "ERROR_INSUFFICIENT_SPACE";
break;
case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
failedReason = "ERROR_TOO_MANY_REDIRECTS";
break;
case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
failedReason = "ERROR_UNHANDLED_HTTP_CODE";
break;
case DownloadManager.ERROR_UNKNOWN:
failedReason = "ERROR_UNKNOWN";
break;
}
Toast.makeText(AndroidDownloadManagerActivity.this,
"FAILED: " + failedReason,
Toast.LENGTH_LONG).show();
break;
case DownloadManager.STATUS_PAUSED:
String pausedReason = "";
switch(reason){
case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
pausedReason = "PAUSED_QUEUED_FOR_WIFI";
break;
case DownloadManager.PAUSED_UNKNOWN:
pausedReason = "PAUSED_UNKNOWN";
break;
case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
pausedReason = "PAUSED_WAITING_FOR_NETWORK";
break;
case DownloadManager.PAUSED_WAITING_TO_RETRY:
pausedReason = "PAUSED_WAITING_TO_RETRY";
break;
}
Toast.makeText(AndroidDownloadManagerActivity.this,
"PAUSED: " + pausedReason,
Toast.LENGTH_LONG).show();
break;
case DownloadManager.STATUS_PENDING:
Toast.makeText(AndroidDownloadManagerActivity.this,
"PENDING",
Toast.LENGTH_LONG).show();
break;
case DownloadManager.STATUS_RUNNING:
Toast.makeText(AndroidDownloadManagerActivity.this,
"RUNNING",
Toast.LENGTH_LONG).show();
break;
case DownloadManager.STATUS_SUCCESSFUL:
Toast.makeText(AndroidDownloadManagerActivity.this,
"SUCCESSFUL",
Toast.LENGTH_LONG).show();
downloadManager.remove(id);
break;
}
}
}

setting specific intent for the filtered items in list view in android

i have had this problem for the last two to three days....i am trying to set specific intents to each item clicked in the list view but the issue is that iam using the position parameter to get the position but is redirecting me according to the current filtered position yet i want the original position on the list view... i tried to use the getItem method within the switch case but but am confused which values i have to pass in the case: to match the position.any help is highly appreciated
below is the sample of the codes
customList
public class CustomList extends BaseAdapter implements Filterable {
Context context;
ArrayList<Countries> countrylist;
ArrayList<Countries> stringFilterList;
ValueFilter valueFilter;
CustomList(Context context, ArrayList<Countries> countrylist){
this.context = context;
this.countrylist = countrylist;
stringFilterList = countrylist;
}
#Override
public int getCount() {
return countrylist.size();
}
#Override
public Object getItem(int position) {
return countrylist.get(position);
}
#Override
public long getItemId(int position) {
return countrylist.indexOf(getItem(position));
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = null;
if(convertView == null){
convertView = inflater.inflate(R.layout.menu_eng_content, null);
TextView countryName = (TextView) convertView.findViewById(R.id.country_names);
ImageView countryImages = (ImageView) convertView.findViewById(R.id.country_imgs);
Countries countries = countrylist.get(position);
countryName.setText(countries.getNames());
countryImages.setImageResource(countries.getFlags());
}
return convertView;
}
#Override
public Filter getFilter() {
if(valueFilter == null){
valueFilter = new ValueFilter();
}
return valueFilter;
}
private class ValueFilter extends Filter{
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length()>0){
ArrayList<Countries> filterList = new ArrayList<Countries>();
for (int i = 0; i<stringFilterList.size(); i++){
if ((stringFilterList.get(i).getNames().toUpperCase()).contains(constraint.toString()
.toUpperCase())){
Countries countries = new Countries(stringFilterList.get(i)
.getNames(), stringFilterList.get(i).getFlags());
filterList.add(countries);
}
}
results.count = filterList.size();
results.values =filterList;
}
else {
results.count = stringFilterList.size();
results.values = stringFilterList;
}
return results;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
countrylist = (ArrayList<Countries>)results.values;
notifyDataSetChanged();
}
}
FrenchMenu class
public class FrenchMenu extends ActionBarActivity implements SearchView.OnQueryTextListener{
ListView listView;
private SearchView searchView;
ArrayList<Countries> countrylist;
CustomList adapter;
String[] countryNames = {"Algerie", "Botswana", "Burkina Faso", "Burundi", "Cameroon", "Cape Verde",
"Congo Brazavile", "Côte d'Ivoire", "Djibouti", "RDC", "Egypte", "Eritrea", "Ethiopie", "Gabon","Ghana","Guinea Bissau"
, "Guinea Conakry", "Kenya", "Lesotho", "Madagascar", "Malawi", "Mali", "Mauritania", "Maroque", "Namibie",
"Niger", "Nigeria", "Rwanda", "Senegal", "Seychelles", "Afrique du Sud", "Sudan", "Tanzanie", "Tchad", "Togo"
, "Tunisie", "Uganda", "Zambie", "Zimbabwe"};
Integer imgs[] = {R.drawable.algeria_flag,
R.drawable.botswana,
R.drawable.burkina_faso,
R.drawable.burundi_flag,
R.drawable.cameroon_flag,
R.drawable.cape_verde,
R.drawable.congo_braza,
R.drawable.cote_divoire,
R.drawable.djibouti,
R.drawable.drc_flag,
R.drawable.egypt,
R.drawable.eritrea_flag,
R.drawable.ethiopia,
R.drawable.gabon_flag,
R.drawable.ghana_flag,
R.drawable.guinea_bissau,
R.drawable.guinea_conakry,
R.drawable.kenya,
R.drawable.lesotho,
R.drawable.madagascar,
R.drawable.malawi, R.drawable.mali, R.drawable.mauritania, R.drawable.morocco,
R.drawable.namibia,R.drawable.niger,R.drawable.nigeria,R.drawable.rwandan_flag,
R.drawable.senegal_flag,R.drawable.seychelles,R.drawable.south_african_flag,
R.drawable.sudan,R.drawable.tanzania,R.drawable.tchad,R.drawable.togo,
R.drawable.tunisia,R.drawable.uganda_flag,R.drawable.zambia,R.drawable.zimbabwe_flag
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_eng_countries);
listView = (ListView) findViewById(R.id.listView_eng);
searchView = (SearchView) findViewById(R.id.search_view);
/*
CustomList adapter = new CustomList(FrenchMenu.this, names, imgs);
listView = (ListView) findViewById(R.id.listView_eng);
listView.setAdapter(adapter);*/
countrylist = new ArrayList<Countries>();
for (int i= 0;i<countryNames.length;i++){
Countries countries = new Countries(countryNames[i], imgs[i]);
countrylist.add(countries);
}
adapter = new CustomList(getApplicationContext(),countrylist);
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
searchView.setOnQueryTextListener(this);
//when the items in the list view are clicked
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//String itemPosition = adapter.getItem(position).toString();
switch (position){
case 0:
Intent algeria = new Intent(FrenchMenu.this, WebDirection.class);
algeria.putExtra("url","http://fr.napteker.com/products/article/algeria");
algeria.putExtra("title", "Medicament en Algerie");
startActivity(algeria);
break;
case 1:
Intent botswana = new Intent(FrenchMenu.this, WebDirection.class);
botswana.putExtra("url", "http://fr.napteker.com/products/article/botswana");
botswana.putExtra("title", "Medicament au Botswana");
startActivity(botswana);
break;
case 2:
Intent burkina_faso = new Intent(FrenchMenu.this, WebDirection.class);
burkina_faso.putExtra("url","http://fr.napteker.com/products/article/burkina-faso");
burkina_faso.putExtra("title","Medicament au Burkina Faso");
startActivity(burkina_faso);
break;
case 3:
Intent burundi = new Intent(FrenchMenu.this, WebDirection.class);
burundi.putExtra("url","http://fr.napteker.com/products/article/burundi");
burundi.putExtra("title","Medicament au Burundi");
startActivity(burundi);
break;
case 4:
Intent cameroon = new Intent(FrenchMenu.this, WebDirection.class);
cameroon.putExtra("url", "http://fr.napteker.com/products/article/cameroon");
cameroon.putExtra("title", "Medicament en Cameroun");
startActivity(cameroon);
break;
case 5:
Intent cape_verde = new Intent(FrenchMenu.this, WebDirection.class);
cape_verde.putExtra("url", "http://fr.napteker.com/products/article/cape-verde");
cape_verde.putExtra("title", "Medicament au Cape Vert");
startActivity(cape_verde);
break;
case 6:
Intent congo_braza = new Intent(FrenchMenu.this, WebDirection.class);
congo_braza.putExtra("url", "http://fr.napteker.com/products/article/congo-braza");
congo_braza.putExtra("title","Medicament au Congo ");
startActivity(congo_braza);
break;
case 7:
Intent ivory_coast = new Intent(FrenchMenu.this, WebDirection.class);
ivory_coast.putExtra("url", "http://fr.napteker.com/products/article/ivory-coast");
ivory_coast.putExtra("title", "Medicament au Côte d'Ivoire");
startActivity(ivory_coast);
break;
case 8:
Intent djibouti = new Intent(FrenchMenu.this, WebDirection.class);
djibouti.putExtra("url", "http://fr.napteker.com/products/article/djibouti");
djibouti.putExtra("title", "Medicament au Djibouti");
startActivity(djibouti);
break;
case 9:
Intent drc = new Intent(FrenchMenu.this, WebDirection.class);
drc.putExtra("url", "http://fr.napteker.com/products/article/drc");
drc.putExtra("title", "Medicament au RDC");
startActivity(drc);
break;
case 10:
Intent egypt = new Intent(FrenchMenu.this, WebDirection.class);
egypt.putExtra("url", "http://fr.napteker.com/products/article/egypt");
egypt.putExtra("title", "Medicament en Egypte");
startActivity(egypt);
break;
case 11:
Intent eritrea = new Intent(FrenchMenu.this, WebDirection.class);
eritrea.putExtra("url", "http://fr.napteker.com/products/article/eritrea");
eritrea.putExtra("title", "Medicament en Eritrea");
startActivity(eritrea);
break;
case 12:
Intent ethiopia = new Intent(FrenchMenu.this, WebDirection.class);
ethiopia.putExtra("url", "http://fr.napteker.com/products/article/burkina-faso");
ethiopia.putExtra("title", "Medicament en Ethiopie");
startActivity(ethiopia);
break;
case 13:
Intent gabon = new Intent(FrenchMenu.this, WebDirection.class);
gabon.putExtra("url", "http://fr.napteker.com/products/article/gabon");
gabon.putExtra("title", "Medicament au Gabon");
startActivity(gabon);
break;
case 14:
Intent ghana = new Intent(FrenchMenu.this, WebDirection.class);
ghana.putExtra("url", "http://fr.napteker.com/products/article/ghana");
ghana.putExtra("title", "Medicament au Ghana");
startActivity(ghana);
break;
case 15:
Intent guinea_bissau = new Intent(FrenchMenu.this, WebDirection.class);
guinea_bissau.putExtra("url", "http://fr.napteker.com/products/article/guinea-bissau");
guinea_bissau.putExtra("title", "Medicament au Guinea Bissau");
startActivity(guinea_bissau);
break;
case 16:
Intent guinea_conakry = new Intent(FrenchMenu.this, WebDirection.class);
guinea_conakry.putExtra("url", "http://fr.napteker.com/products/article/guinea-conakry");
guinea_conakry.putExtra("title", "Medicament au Guinea Conakry");
startActivity(guinea_conakry);
break;
case 17:
Intent kenya = new Intent(FrenchMenu.this, WebDirection.class);
kenya.putExtra("url", "http://fr.napteker.com/products/article/kenya");
kenya.putExtra("title", "Drugs in Kenya");
startActivity(kenya);
break;
case 18:
Intent lesotho = new Intent(FrenchMenu.this, WebDirection.class);
lesotho.putExtra("url", "http://fr.napteker.com/products/article/lesotho");
lesotho.putExtra("title", "Medicament au Lesotho");
startActivity(lesotho);
break;
case 19:
Intent madagascar = new Intent(FrenchMenu.this, WebDirection.class);
madagascar.putExtra("url", "http://fr.napteker.com/products/article/madagascar");
madagascar.putExtra("title", "Medicament au Madagascar");
startActivity(madagascar);
break;
case 20:
Intent malawi = new Intent(FrenchMenu.this, WebDirection.class);
malawi.putExtra("url", "http://fr.napteker.com/products/article/malawi");
malawi.putExtra("title", "Medicament au Malawi");
startActivity(malawi);
break;
case 21:
Intent mali = new Intent(FrenchMenu.this, WebDirection.class);
mali.putExtra("url", "http://fr.napteker.com/products/article/mali");
mali.putExtra("title", "Medicament au Mali");
startActivity(mali);
break;
case 22:
Intent mauritania = new Intent(FrenchMenu.this, WebDirection.class);
mauritania.putExtra("url", "http://fr.napteker.com/products/article/mauritania");
mauritania.putExtra("title", "Medicament au Mauritanie");
startActivity(mauritania);
break;
case 23:
Intent morocco = new Intent(FrenchMenu.this, WebDirection.class);
morocco.putExtra("url", "http://fr.napteker.com/products/article/morocco");
morocco.putExtra("title", "Medicament au Maroc");
startActivity(morocco);
break;
case 24:
Intent namibia = new Intent(FrenchMenu.this, WebDirection.class);
namibia.putExtra("url", "http://fr.napteker.com/products/article/namibia");
namibia.putExtra("title", "Drugs in Namibia");
startActivity(namibia);
break;
case 25:
Intent niger = new Intent(FrenchMenu.this, WebDirection.class);
niger.putExtra("url", "http://fr.napteker.com/products/article/niger");
niger.putExtra("title", "Medicament au Niger");
startActivity(niger);
break;
case 26:
Intent nigeria = new Intent(FrenchMenu.this, WebDirection.class);
nigeria.putExtra("url", "http://fr.napteker.com/products/article/nigeria");
nigeria.putExtra("title", "Medicament au Nigeria");
startActivity(nigeria);
break;
case 27:
Intent rwanda = new Intent(FrenchMenu.this, WebDirection.class);
rwanda.putExtra("url", "http://fr.napteker.com/products/article/rwanda");
rwanda.putExtra("title", "Medicament au Rwanda");
startActivity(rwanda);
break;
case 28:
Intent senegal = new Intent(FrenchMenu.this, WebDirection.class);
senegal.putExtra("url", "http://fr.napteker.com/products/article/senegal");
senegal.putExtra("title", "Medicament au Senegal");
startActivity(senegal);
break;
case 29:
Intent seychelles = new Intent(FrenchMenu.this, WebDirection.class);
seychelles.putExtra("url", "http://fr.napteker.com/products/article/seychelles");
seychelles.putExtra("title", "Medicament au Seychelles");
startActivity(seychelles);
break;
case 30:
Intent south_africa = new Intent(FrenchMenu.this, WebDirection.class);
south_africa.putExtra("url", "http://fr.napteker.com/products/article/south-africa");
south_africa.putExtra("title", "Medicament en Afrique du Sud");
startActivity(south_africa);
break;
case 31:
Intent sudan = new Intent(FrenchMenu.this, WebDirection.class);
sudan.putExtra("url", "http://fr.napteker.com/products/article/sudan");
sudan.putExtra("title", "Medicament au Sudan");
startActivity(sudan);
break;
case 32:
Intent tanzania = new Intent(FrenchMenu.this, WebDirection.class);
tanzania.putExtra("url", "http://fr.napteker.com/products/article/tanzania");
tanzania.putExtra("title", "Medicament au Tanzanie");
startActivity(tanzania);
break;
case 33:
Intent tchad = new Intent(FrenchMenu.this, WebDirection.class);
tchad.putExtra("url", "http://fr.napteker.com/products/article/Tchad");
tchad.putExtra("title", "Medicament au Tchad");
startActivity(tchad);
break;
case 34:
Intent togo = new Intent(FrenchMenu.this, WebDirection.class);
togo.putExtra("url", "http://fr.napteker.com/products/article/togo");
togo.putExtra("title", "Medicament au Togo");
startActivity(togo);
break;
case 35:
Intent tunisia = new Intent(FrenchMenu.this, WebDirection.class);
tunisia.putExtra("url", "http://fr.napteker.com/products/article/tunisia");
tunisia.putExtra("title", "Drugs in Tunisia");
startActivity(tunisia);
break;
case 36:
Intent uganda = new Intent(FrenchMenu.this, WebDirection.class);
uganda.putExtra("url", "http://fr.napteker.com/products/article/uganda");
uganda.putExtra("title", "Medicament en Uganda");
startActivity(uganda);
break;
case 37:
Intent zambia = new Intent(FrenchMenu.this, WebDirection.class);
zambia.putExtra("url", "http://fr.napteker.com/products/article/burkina-faso");
zambia.putExtra("title", "Medicament au Zambie");
startActivity(zambia);
break;
case 38:
Intent zimbabwe = new Intent(FrenchMenu.this, WebDirection.class);
zimbabwe.putExtra("url", "http://fr.napteker.com/products/article/zimbabwe");
zimbabwe.putExtra("title", "Medicament au Zimbabwe");
startActivity(zimbabwe);
break;
}
}
});
//instantiating the action bar
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#436487")));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
public boolean onQueryTextChange(String newText){
adapter.getFilter().filter(newText);
return false;
}
public boolean onQueryTextSubmit(String query){
return false;
}
}
If you have access the countries class, try adding the link & title to the country itself. It will allow the listview to just retreive the country and get the link & title from the object.
Like so:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Countries country= (Countries) adapter.getItemAtPosition(position);
Intent country_intent = new Intent(FrenchMenu.this, WebDirection.class);
country_intent ("url", country.getURL());
country_intent .putExtra("title", country.getTitle());
startActivity(country_intent);
}
}
});
Hope this helps
Edit:
So, in Countries : Create 2 variables for the link and title (String link, title). Edit the constructor and create getters and setters for those variables.
In the FrenchMenu class:
Add an array String[] links = { "The","bunch","Of","Links" }
And an array String[] titles = { "the","Same","here"}
Change your loop
for (int i= 0;i<countryNames.length;i++){
Countries countries = new Countries(countryNames[i], imgs[i]);
countrylist.add(countries);
}
To
for (int i= 0;i<countryNames.length;i++){
Countries countries = new Countries(countryNames[i], imgs[i],links[i],titles[i]);
countrylist.add(countries);
}
Then you can use the function in your onItemClickListener as I showed you above.

Detect when system starts a download

Is there a way to detect when the system starts a download and get information of it and force to download it to specific location? I googled it and results where not much helpful
Start this service class and then start your download it will print the result for status of download.
public class MyService extends Service {
private Timer timer;
private TimerTask timerTask;
private static final int SAMPLING_RATE = 1000;
public MyService() {
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
#Deprecated
public void onStart(Intent intent, int startId) {
Log.e("Download", "onstart");
}
#Override
public void onCreate() {
timer = new Timer();
timerTask = new TimerTask() {
#Override
public void run() {
// Log.d(MyService.class.toString(),
// "tic ... "+System.currentTimeMillis());
getDownloadData();
}
};
if (timer != null && timerTask != null) {
timer.schedule(timerTask, 0, SAMPLING_RATE);
}
}
#TargetApi(Build.VERSION_CODES.GINGERBREAD)
#SuppressLint("NewApi")
public void getDownloadData() {
DownloadManager downloadMgr = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterByStatus(DownloadManager.STATUS_FAILED
| DownloadManager.STATUS_PENDING
| DownloadManager.STATUS_RUNNING
| DownloadManager.STATUS_SUCCESSFUL);
Cursor c = downloadMgr.query(query);
if (c == null) {
System.out.println("--------------------------");
} else {
if (c.moveToFirst()) {
System.out.println("------------End--------------");
while (c.isAfterLast() == false) {
getStatus(c);
c.moveToNext();
}
}
}
}
public void getStatus(Cursor c) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
int columnReason = c.getColumnIndex(DownloadManager.COLUMN_REASON);
int reason = c.getInt(columnReason);
int status = c.getInt(columnIndex);
String statusText = null;
String reasonText = null;
switch (status) {
case DownloadManager.STATUS_FAILED:
statusText = "STATUS_FAILED";
switch (reason) {
case DownloadManager.ERROR_CANNOT_RESUME:
reasonText = "ERROR_CANNOT_RESUME";
break;
case DownloadManager.ERROR_DEVICE_NOT_FOUND:
reasonText = "ERROR_DEVICE_NOT_FOUND";
break;
case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
reasonText = "ERROR_FILE_ALREADY_EXISTS";
break;
case DownloadManager.ERROR_FILE_ERROR:
reasonText = "ERROR_FILE_ERROR";
break;
case DownloadManager.ERROR_HTTP_DATA_ERROR:
reasonText = "ERROR_HTTP_DATA_ERROR";
break;
case DownloadManager.ERROR_INSUFFICIENT_SPACE:
reasonText = "ERROR_INSUFFICIENT_SPACE";
break;
case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
reasonText = "ERROR_TOO_MANY_REDIRECTS";
break;
case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
reasonText = "ERROR_UNHANDLED_HTTP_CODE";
break;
case DownloadManager.ERROR_UNKNOWN:
reasonText = "ERROR_UNKNOWN";
break;
}
break;
case DownloadManager.STATUS_PAUSED:
statusText = "STATUS_PAUSED";
switch (reason) {
case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
reasonText = "PAUSED_QUEUED_FOR_WIFI";
break;
case DownloadManager.PAUSED_UNKNOWN:
reasonText = "PAUSED_UNKNOWN";
break;
case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
reasonText = "PAUSED_WAITING_FOR_NETWORK";
break;
case DownloadManager.PAUSED_WAITING_TO_RETRY:
reasonText = "PAUSED_WAITING_TO_RETRY";
break;
}
break;
case DownloadManager.STATUS_PENDING:
statusText = "STATUS_PENDING";
break;
case DownloadManager.STATUS_RUNNING:
statusText = "STATUS_RUNNING";
break;
case DownloadManager.STATUS_SUCCESSFUL:
statusText = "STATUS_SUCCESSFUL";
break;
}
Log.d("status", statusText + " " + reasonText);
}
}

EditText set selector position doesn't works

When I delete text inside EditText with clrFunc() I lost cursor (I see it flashing, but nothing happens if I type). I can't type back in. I have to click back onEditText and then I can type in.
/* WORKS PERFECT */
private void delFunc(){
String str = display.getText().toString();
if(str.length() > 0){
String strStart = str.substring(0, SELECTOR_POSITION-1);
String strEnd = str.substring(SELECTOR_POSITION);
display.setText(strStart + strEnd);
display.requestFocus();
display.setSelection(--SELECTOR_POSITION);
}
}
/* NOT FULLY WORKING */
private void clrFunc(){
display.setText(""); //text is set to ""
display.requestFocus(); //not working
display.setSelection(display.getText().length()); //not working
}
EDIT: Added more code, I'm building simple calculator.
private EditText display;
private Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
private Button bDec, bEquals, bAdd, bSub, bMultiply, bDivide, bClear, bBracket, bBackBracket, bDel, bClrH;
private Button bSin, bAsin, bCos, bAcos, bTan, bAtan, bLn, bLog, bPow, bPow2, bSqrt, bPi, bE, bToRad, bToDeg;
private TextView history;
private int SELECTOR_POSITION;
private void implementGUI(){
/* EditText */
display = (EditText) findViewById(R.id.etDisplay);
display.setOnTouchListener(this);
/* ... */
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.bZero: insert("0"); break;
case R.id.bOne: insert("1"); break;
case R.id.bTwo: insert("2"); break;
case R.id.bThree: insert("3"); break;
case R.id.bFour: insert("4"); break;
case R.id.bFive: insert("5"); break;
case R.id.bSix: insert("6"); break;
case R.id.bSeven: insert("7"); break;
case R.id.bEight: insert("8"); break;
case R.id.bNine: insert("9"); break;
case R.id.bDecPoint: insert("."); break;
case R.id.bAdd: insert("+"); break;
case R.id.bSub: insert("-"); break;
case R.id.bMultiply: insert("*"); break;
case R.id.bDivide: insert("/"); break;
case R.id.bBracket: insert("("); break;
case R.id.bBackBracket: insert(")"); break;
case R.id.bDel: delFunc(); break;
case R.id.bC: clrFunc(); break;
case R.id.bEquals: calcFunc(); break;
case R.id.bClrH: clrHistory(); break;
case R.id.bSin: insert("sin("); break;
case R.id.bAsin: insert("asin("); break;
case R.id.bCos: insert("cos("); break;
case R.id.bAcos: insert("acos("); break;
case R.id.bTan: insert("tan("); break;
case R.id.bAtan: insert("atan("); break;
case R.id.bLn: insert("ln("); break;
case R.id.bLog: insert("log("); break;
case R.id.bPow: insert("^"); break;
case R.id.bPow2: insert("^2"); break;
case R.id.bSqrt: insert("sqrt("); break;
case R.id.bPi: insert("(PI)"); break;
case R.id.bE: insert("(E)"); break;
case R.id.bToRad: insert("toRadians("); break;
case R.id.bToDeg: insert("toDegrees("); break;
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
v.onTouchEvent(event);
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
SELECTOR_POSITION = display.getSelectionStart();
return true;
}
private void delFunc(){
String str = display.getText().toString();
if(str.length() > 0){
String strStart = str.substring(0, SELECTOR_POSITION-1);
String strEnd = str.substring(SELECTOR_POSITION);
display.setText(strStart + strEnd);
display.requestFocus();
display.setSelection(--SELECTOR_POSITION);
}
}
/* NOT FULLY WORKING */
private void clrFunc(){
display.setText("");
display.requestFocus();
display.setSelection(display.getText().length());
}
private void clrHistory(){
history.setText("");
}
private void calcFunc(){
try{
MathEval math = new MathEval();
String input = display.getText().toString();
history.setText(String.format("%s = %s%n%s", input, math.evaluate(input), history.getText()));
}catch(Exception e){
e.printStackTrace();
history.setText(String.format("%s%n%s", "ERROR", history.getText()));
}
clrFunc();
}
private void insert(String midStr){
try{
String input = display.getText().toString();
String startStr = input.substring(0,SELECTOR_POSITION);
String endStr = input.substring(SELECTOR_POSITION);
String retStr = startStr + midStr + endStr;
SELECTOR_POSITION += midStr.length();
display.setText(retStr);
display.setSelection(SELECTOR_POSITION);
}catch(Exception e){
e.printStackTrace();
}
}
P.S.: Sorry for my bad language.
If you want to directly type into the EditText you are missing the following in your method:
display.requestFocus();
EDIT
This is the method I am using to show the keyboard with focus, the different focus call could do the trick.
protected void showKeyboard(EditText edit) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edit, 0);
edit.requestFocusFromTouch();
}

Categories

Resources