How to findviewbyid to an item that hasn't been created yet? - android

i am creating a Checkbox inside the java code while the program is running like this:
void openContactsList() {
// TODO Auto-generated method stub
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams butparams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// getting phone number and name of the contacts
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER };
Cursor people = getContentResolver().query(uri, projection, null, null,
null);
int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
j = 0;
people.moveToFirst();
ContactsDataBase entry = new ContactsDataBase(Contacts.this);
entry.open();
do {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(params);
for (j = 0; (j < 3) && next; j++) {
// create the person and put it in the db table
String name = people.getString(indexName);
String number = people.getString(indexNumber);
entry.createEntry(name, number);
// create every person Image button
CheckBox btnTag = new CheckBox(this);
butparams.setMargins(j, 10, 0, 0);
btnTag.setGravity(10);
btnTag.setLayoutParams(butparams);
btnTag.setText(number + "\n" + name);
btnTag.setTextSize(10);
btnTag.setTag("Button " + name);
try {
int num = Integer.parseInt(number);
btnTag.setId(num);
} catch (NumberFormatException nfe) {
// Handle parse error.
}
// btnTag.setBackgroundResource(R.drawable.have_a_nice_day);
btnTag.getLayoutParams().width = 70;
btnTag.getLayoutParams().height = 70;
next = people.moveToNext();
row.addView(btnTag);
}
layout12.addView(row);
} while (next);
entry.close();
people.close();
}
so every checkbox is a person that i saved in my phone and the Id is the phone number
now i want to check if they are checked, and than make a button to move the checked ones to other LinearLayout... but the problem is that i dont know how to check them if i cant get the Id because the computer cannot find them when they didnt have been created yet and they are not in R.java class yet!
well ,i created a Db on SQL so i got the phone numbers and i tried go one by one . something like that:
{
int num; //lets say that the num is the phonenumber=the Id
Friends = (CheckBox) findviewbyid(R.id.num) //this it the problem
if(Friends.isChecked()){ // and i got one by one on the Db and changing the number
... //all i want to do is here so i wont write it (its not the point here)
}
}
So I can't use this num because its not in the R.java
How can I check those checkboxes. maybe I can search in the program for all of the Id's in a specific LinearLayout
Thank you for helping I hope you understand what I want..

I'm not sure whether you can use 10+ digits phone number as View ID(integer). But if it works then you may use
Friends = (CheckBox) findviewbyid(phonenumber)

Related

Is it possible to give a string an empty value if it cant find the string

Is there away to create a blank string if it doesn't exist in the xml on the fly.
for example the following code:-
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
String SManuf = bundle.getString("SManuf");
String manuf = bundle.getString("manuf");
String panel = bundle.getString("panel");
setTitle("Instructions for "+manuf+" "+panel);
String Ins = (SManuf + panel+"Noof");
String InsI = (SManuf + panel);
// Toast.makeText(getApplicationContext(), Ins, Toast.LENGTH_SHORT).show();
int resId = getResources().getIdentifier(Ins, "string", getPackageName());
String InsNo = getResources().getString(resId);
scrollviewTerms = new ScrollView(this);
LinearLayout linearlayout = new LinearLayout(this);
linearlayout.setOrientation(LinearLayout.VERTICAL);
scrollviewTerms.addView(linearlayout);
//for (int i = 0; i < 13; i++)
int foo = Integer.parseInt(InsNo);
for (int i = 0; i < foo; i++)
{
LinearLayout linearManufTB = new LinearLayout(this);
linearManufTB.setOrientation(LinearLayout.HORIZONTAL);
linearlayout.addView(linearManufTB);
Termsb = new Button(this);
int id = getResources().getIdentifier("PanMansTitle" + i, "string", getPackageName());
String Terms = getResources().getString(id);
// Toast.makeText(getApplicationContext(), Terms, Toast.LENGTH_SHORT).show();
Termsb.setText(Terms);
Termsb.setId(i);
Termsb.setTextSize(30);
Termsb.setPadding(0, 0, 0, 0);
LinearLayout linearManufTV = new LinearLayout(this);
linearManufTV.setOrientation(LinearLayout.HORIZONTAL);
linearlayout.addView(linearManufTV);
String TermsS = Terms.replaceAll("&","").replaceAll(" ","").replaceAll("/","").replaceAll(" / ","").replaceAll("/ ","").replaceAll(" /","".replaceAll("&",""));
TermstINS = new TextView(this);
int ids = getResources().getIdentifier(InsI + TermsS, "string", getPackageName());
String TermsTV = getResources().getString(ids);
TermstINS.setText(TermsTV);
TermstINS.setId(i);
TermstINS.setTextSize(20);
TermstINS.setBackgroundColor(Color.parseColor("#999999"));
TermstINS.setPadding(0, 0, 0, 0);
LinearLayout linearManufTV2 = new LinearLayout(this);
linearManufTV2.setOrientation(LinearLayout.HORIZONTAL);
linearlayout.addView(linearManufTV2);
TermstNOTS = new TextView(this);
int idn = getResources().getIdentifier(InsI + TermsS +"N", "string", getPackageName());
String TermsTVN = getResources().getString(idn);
TermstNOTS.setText(TermsTVN);
TermstNOTS.setId(i);
TermstNOTS.setTextSize(20);
TermstNOTS.setBackgroundColor(Color.parseColor("#515151"));
TermstNOTS.setPadding(0, 0, 0, 0);
// b.setTypeface(Typeface.SERIF,Typeface.ITALIC);
Termsb.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
TermstINS.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
TermstNOTS.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
linearManufTB.addView(Termsb);
linearManufTV.addView(TermstINS);
linearManufTV2.addView(TermstNOTS);
if(TermstINS.length()== 0){ linearManufTB.setVisibility(View.GONE);linearManufTV.setVisibility(View.GONE);linearManufTV2.setVisibility(View.GONE);}else{
linearManufTB.setVisibility(View.VISIBLE); linearManufTV.setVisibility(View.GONE); linearManufTV2.setVisibility(View.GONE);}
this lists the titles and the instructions underneath, and notes below this.
however the list of Titles is fixed title0 to title 200+ but not all systems would have the same titles as they don't have the same features, but would share some of them.
rather than type out 200+ instructions and notes that are blank in the xml file, is it possible to say that the item isn't currently available, generate a blank on the fly.
Thus making it behave smarter and not requiring lots of dead data to be entered.
At the moment anything that has no instructions wont show the header(which isn't empty), or instructions and notes but, that requires me to make a data entry for the xml file instructions and notes when there isn't any.
I am sure this is basic, but I cant find anything to cross reference. Any help of suitable examples or ideas on how to tackle it would be appreciated.
getResources().getIdentifier(String name, String defType, String defPackage) returns id of the resource. If the resource doesn't exist it will return 0.
You can change you code like this:
String defaultString = "";
String Terms = id != 0 ? getResources().getString(id) : defaultString;
String your_string = "";
This give an "empty" value to your string.

When is the ANDROID Contact Group PHYSICALLY removed instead of being marked as deleted?

I am trying to permanently remove a Android Contact Group and have used the Sync parameter and it always appears the record is simply marked as deleted and not physically removed. Can anyone explain how/when, if ever, the Contract group row is deleted permanently or show a snippet of code demonstrating how to do this? The records I am trying to remove are ones that I added, so they are not Read-Only.
Linked back to https://stackoverflow.com/a/21376905/5398898
My Delete Code:
private void RemoveGroup()
{
TextView tv = (TextView) this.findViewById(R.id.helloworld);
int[] startId = {10};//{6, 7, 8, 9, 10, 11};
String groupName = "My New Contacts";
Uri mUri = ContactsContract.Groups.CONTENT_URI;
mUri.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build();
for (int n = 0; n < startId.length; n++) {
groupCount = startId[n];
ContentValues values = new ContentValues();
values.put(ContactsContract.Groups._ID, groupCount);
try {
getContentResolver().delete(mUri, values.toString(),null);
} catch (Exception ex) {
tv.setText(ex.getMessage());
}
}
}
Result when reading the groups:
Image can be found here http://i.stack.imgur.com/5OOfc.png
You are building the correct Uri but not using it, try like this
Uri mUri = ContactsContract.Groups.CONTENT_URI;
mUri = mUri.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build();

how can I get the value from programmatically radio button/check box?

I have a form that the question comes from web service (maybe have check boxes or text field or radio button-made programmatic-ally ) and send the value to the local database.
The first part (call the web service and showing them) is correct but the second part (getting the values) is not!
how can I get the value from programmatic-ally radio button/check box ?
(the important part is i have two "FOR" for radio button or check boxes)
I do not know how can i get the value!??!??!
this is my code, made question!
PLZ help me.
for (int i = 0; i < output2.length; i++) {
if (output2[i].contains("#")) {
part1 = output2[i].split("#");
int i2 = 0;
do {
// Question Id ->part1[0]
// FK_MASTERNAZAR ->partinfo_id[0]
// Tilte ->part1[1]
TextView tv2 = new TextView(page2.this);
tv2.setId(i2);
tv2.setText(part1[1]);
tv2.setGravity(Gravity.RIGHT);
lmain2.addView(tv2);
// Answers ->part1[2]
// Type ->part1[3]
switch (Integer.valueOf(part1[3])) {
case 1:
// single selection
part2 = part1[2].split(",");
radioGroup1 = new RadioGroup(page2.this);
radioGroup1.setGravity(Gravity.RIGHT);
radioGroup1.setOrientation(RadioGroup.HORIZONTAL);
for (int i3 = 0; i3 < part2.length; i3++) {
LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
rb6 = new RadioButton(page2.this);
rb6.setId(i3);
rb6.setText(part2[i3]);
rb6.setGravity(Gravity.RIGHT);
radioGroup1.addView(rb6, 0, layoutParams);
}
lmain2.addView(radioGroup1);
break;
case 2:
et_Num = new EditText(page2.this);
et_Num.setId(i2);
et_Num.setHint("insert your answer…");
et_Num.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED);
lmain2.addView(et_Num);
break;
case 3:
// multi selection
part2 = part1[2].split(",");
LinearLayout lcheck = new LinearLayout(page2.this);
lcheck.setGravity(Gravity.RIGHT);
lcheck.setOrientation(LinearLayout.HORIZONTAL);
for (int i3 = 0; i3 < part2.length; i3++) {
ch6 = new CheckBox(page2.this);
ch6.setId(i3);
ch6.setText(part2[i3]);
ch6.setGravity(Gravity.RIGHT);
lcheck.addView(ch6);
lmain2.addView(lcheck);
break;
}
// T ->part2[4]
// insert line
TextView tv6 = new TextView(page2.this);
tv6.setId(i2);
tv6.setText("**********");
tv6.setGravity(Gravity.CENTER);
tv6.setTextColor(Color.parseColor("#33B5E5"));
lmain2.addView(tv6);
Question_Id = Integer.valueOf(part1[0]);
Integer qtype = Integer.valueOf(part1[4]);
// copy FormQuestion web service to the database
database = new MySQLiteHelper(getApplicationContext());
FormQuestionClass formquestionclass = new FormQuestionClass(
Question_Id, Fk_masternazar, part1[2], part1[3], qtype);
database.InsertQuestion(formquestionclass);
database.close();
// end insertting to db.
} while (i2 == part1.length);
}
}
Use the added controls as fields instead of local variables.
That way you can check later on for null, and if they're not null, use
RadioGroup.getCheckedRadioButtonId()
EditText.getText()

How to dynamically add rows and detect and delete clicked?

I'm dynamically adding rows to the table. I have few textViews[] that I add to the row[] and then I add row[] to the table. That's ok, but then I want to detect click on textView[] and delete that row, also I want the numbers of the textViews below deleted ones to decrease.
example:
1 a a a //(I delete this row)
2 b b b
3 c c c
after deleting, I want to have this:
1 b b b
2 c c c
I have tried adding onClickListener to one textView[], but sometimes it deletes row[] and decreases number, sometimes don't.
Can someone write me some example to try out?
EDIT: Here's my code (I think it's all that's needed for this)
This is the code on my button for adding rows:
public TextView textViewKoeficijent[] = new TextView[50];
public TextView textViewBr[] = new TextView[50];
public TextView textViewObrisi[] = new TextView[50];
public TextView textViewTip[] = new TextView[50];
public TextView textViewPar[] = new TextView[50];
fkoeficijent = Double.valueOf(koeficijent);
koefIzracun = koefIzracun * fkoeficijent;
TextView textViewKoeficijentIzracun = (TextView) findViewById(R.id.textViewUkupniKoeficijentIzracun);
koefIzracun = Math.round(koefIzracun*100)/100.0d;
koefIzracunString = String.valueOf(koefIzracun);
textViewKoeficijentIzracun.setText(koefIzracunString);
final TableLayout PopisParova = (TableLayout) findViewById(R.id.TableLayout);
final TableRow noviPar[] = new TableRow[50];
LayoutParams paramsBroj = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1);
paramsBroj.setMargins(4, 0, 2, 4);
LayoutParams paramsPar = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 2);
paramsPar.setMargins(2, 0, 2, 4);
LayoutParams paramsKoef = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 2);
paramsKoef.setMargins(2, 0, 4, 4);
//onclicklistener:
OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
id = view.getId();
brpara --;
TextView textViewKoeficijentIzracun = (TextView) findViewById(R.id.textViewUkupniKoeficijentIzracun);
if(brpara==1){
textViewKoeficijentIzracun.setText("0");
koefIzracun = 1;
}
else{
koeficijent = textViewKoeficijent[id].getText().toString();
fkoeficijent = Double.valueOf(koeficijent);
koefIzracun = koefIzracun / fkoeficijent;
koefIzracun = Math.round(koefIzracun*100)/100.0d;
koefIzracunString = String.valueOf(koefIzracun);
textViewKoeficijentIzracun.setText(koefIzracunString);}
PopisParova.removeViewAt(id);
//PopisParova.removeView(noviPar[id]);
for(i=1; i<=brpara; i++){
if(i>id){
String bri = String.valueOf(i-1);
textViewBr[i].setText(bri);
textViewObrisi[i].setDrawingCacheBackgroundColor(i-1);
}
}
}};
{
textViewBr[brpara] = new TextView(MainActivity.this);
textViewBr[brpara].setLayoutParams(paramsBroj);
textViewBr[brpara].setGravity(Gravity.CENTER_HORIZONTAL);
textViewBr[brpara].setBackgroundColor(0xFFFFFFFF);
brojPara = String.valueOf(brpara);
textViewBr[brpara].setText(brojPara);
textViewBr[brpara].setId(brpara);
}
{
textViewPar[brpara] = new TextView(MainActivity.this);
textViewPar[brpara].setLayoutParams(paramsPar);
textViewPar[brpara].setGravity(Gravity.CENTER_HORIZONTAL);
textViewPar[brpara].setBackgroundColor(0xFFFFFFFF);
textViewPar[brpara].setText(par);
textViewPar[brpara].setId(brpara);
}
{
textViewTip[brpara] = new TextView(MainActivity.this);
textViewTip[brpara].setLayoutParams(paramsPar);
textViewTip[brpara].setGravity(Gravity.CENTER_HORIZONTAL);
textViewTip[brpara].setBackgroundColor(0xFFFFFFFF);
textViewTip[brpara].setText(tip);
textViewTip[brpara].setId(brpara);
}
{
textViewKoeficijent[brpara] = new TextView(MainActivity.this);
textViewKoeficijent[brpara].setLayoutParams(paramsPar);
textViewKoeficijent[brpara].setGravity(Gravity.CENTER_HORIZONTAL);
textViewKoeficijent[brpara].setBackgroundColor(0xFFFFFFFF);
textViewKoeficijent[brpara].setText(koeficijent);
textViewKoeficijent[brpara].setId(brpara);
}
{
textViewObrisi[brpara] = new TextView(MainActivity.this);
textViewObrisi[brpara].setLayoutParams(paramsKoef);
textViewObrisi[brpara].setGravity(Gravity.CENTER_HORIZONTAL);
textViewObrisi[brpara].setBackgroundColor(0xFFFFFFFF);
textViewObrisi[brpara].setText("X");
textViewObrisi[brpara].setId(brpara);
textViewObrisi[brpara].setClickable(true);
textViewObrisi[brpara].setOnClickListener(onClickListener);
}
noviPar[brpara] = new TableRow(MainActivity.this);
noviPar[brpara].addView(textViewBr[brpara]);
noviPar[brpara].addView(textViewPar[brpara]);
noviPar[brpara].addView(textViewTip[brpara]);
noviPar[brpara].addView(textViewKoeficijent[brpara]);
noviPar[brpara].addView(textViewObrisi[brpara]);
PopisParova.addView(noviPar[brpara]);
brpara++;
editTextPar.setText("");
editTextTip.setText("");
editTextKoeficijent.setText("");
EDIT [2]:
brpara is my counter so I know which is number of row which is being added. I have a max of 20 rows.
Also, my loop is working perfectly for deleting one row, but when I delete multiple rows at once, it only changes my row numbers for the first time and after that only deletes rows.
What are you doing is this:
Let's say that brpara = 3.
You click the view with id = 2.
You execute brbara --; So brbara is now 2.
You check if id != brpara they are NOT so no renumbering. Logical Problem!
Furthermore you have this loop:
for(i=id+1; i<=brpara; i++){
i=id+1;
String bri = String.valueOf(id);
textViewBr[i].setText(bri);
textViewObrisi[i].setId(id);}
You say that i must be id+1 (in our example 3) to < brpara (in our example 2) so 3 <= 2 it never executes, if however id and brpara allows loop execution you resetting the i inside the loop
i=id+1;
If for example the loop starting with i = id+1 let's say id=2 so it is now 3 and go inside the loop the i will be always 3 because you are resetting it inside the loop.
It is not clear from your code how are you using brpara but the loop for renumbering is wrong. So let's assume that brpara is ALWAYS the last index in the array then I suggest the following code:
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
id = view.getId();
TextView textViewKoeficijentIzracun = (TextView) findViewById(R.id.textViewUkupniKoeficijentIzracun);
if(brpara==1){
textViewKoeficijentIzracun.setText("0");
koefIzracun = 1;
}
else{
koeficijent = textViewKoeficijent[id].getText().toString();
fkoeficijent = Double.valueOf(koeficijent);
koefIzracun = koefIzracun / fkoeficijent;
koefIzracun = Math.round(koefIzracun*100)/100.0d;
koefIzracunString = String.valueOf(koefIzracun);
textViewKoeficijentIzracun.setText(koefIzracunString);}
//FIRST RENUMBER ALL VIEWS FROM id+1 to brpara
if(id!=brpara){
for(i=id+1; i<=brpara; i++){
String bri = String.valueOf(i - 1); //<--HERE MAYBE MUST BE i-1 NOT id...
textViewBr[i].setText(bri);
textViewObrisi[i].setId(id);} //THIS DOESN'T FEEL RIGHT MAYBE i INSTEAD OF id? OR i - 1?
}
//AND FINALY REMOVE VIEW AND DECREASE brpara...
PopisParova.removeViewAt(id);
brpara --;
}
My suggestion assumes that brpara is always the last index and you are renumbering from the clicked view + 1 to the end. However there is a more elegant solution which is to create a function that renumbers the whole array from top to bottom regardless the view clicked and call it whenever a view gets added or removed. That way you will not have to manage brpara at all but this depends on what exactly you are trying to achieve.
Based on your Edits
Try this:
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
id = view.getId();
TextView textViewKoeficijentIzracun = (TextView) findViewById(R.id.textViewUkupniKoeficijentIzracun);
if((brpara - 1)==1){
textViewKoeficijentIzracun.setText("0");
koefIzracun = 1;
} else {
koeficijent = textViewKoeficijent[id].getText().toString();
fkoeficijent = Double.valueOf(koeficijent);
koefIzracun = koefIzracun / fkoeficijent;
koefIzracun = Math.round(koefIzracun*100)/100.0d;
koefIzracunString = String.valueOf(koefIzracun);
textViewKoeficijentIzracun.setText(koefIzracunString);
PopisParova.removeViewAt(id);
brpara --;
for(i=1; i<=brpara; i++){
String bri = String.valueOf(i);
textViewBr[i].setText(bri);
textViewObrisi[i].setDrawingCacheBackgroundColor(i);
}
}
}
Since brpara is a counter you should decrease it only when you are actually remove the view. You may have to change the
if((brpara - 1)==1){
with
if(brpara==1){
based on your needs. But now the loop seems to be OK.
Hope this helps...
The only possible solution is to replace deleted rows, and their data, with rows following, and their data and delete last row. So it looks like the deleted row is really deleted.
row[x] = row[x+1] // want to delete this
row[x+1] = row[x+2]
//... and like that until the last row
row[last] // delete this row

Insert TextView at Certain Spots in ListView

I have a List View that Displays songs in alphebetical order being populated by this method
public void updatelist(){
Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,null);
int j =0;
if(cursor.moveToFirst()){
do{
int ALBUM_ID = cursor.getInt((cursor.getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM_ID)));
int pathcolumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
String path1 = cursor.getString(pathcolumn);
String album_url = null;
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, ALBUM_ID);
album_url = uri.toString();
ContentResolver res = this.getContentResolver();
// Album
String album_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM));
String year = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.YEAR));
// String year = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AlbumColumns.NUMBER_OF_SONGS));
// artist
String artist_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.ArtistColumns.ARTIST));
// display name
String DisplayName = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME));
//title
String Title = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.TITLE));
songtitle.add(Title);
Collections.sort(songtitle);
artistname.add(songtitle.indexOf(Title), artist_name);
albumname.add(songtitle.indexOf(Title), album_name);
path.add(songtitle.indexOf(Title),path1);
albumartwork.add(songtitle.indexOf(Title),album_url);
j++;
}while(cursor.moveToNext());
}
Collections.sort(songtitle);
adapter = new ArrayAdapter<String>(this,R.layout.song,songtitle);
setListAdapter(adapter);
}
My Question is i want to insert Dividers whenever the first letter of the SongName Changes.
I have this method to get the first letter of the songname if it is different than the previous..
private void alphebetdividers(ArrayList<String> songtitle2) {
String newString = null;
// TODO Auto-generated method stub
ArrayList<Character> letters = new ArrayList<Character>();
int i = 0;
int j = 0;
while( j < songtitle.size()-1){
if(songtitle2.get(i).charAt(0) == songtitle2.get(i+1).charAt(0)){
Toast.makeText(getApplication(), songtitle2.get(i).charAt(0) + "== " + songtitle2.get(i+1).charAt(0), Toast.LENGTH_LONG).show();
}else{
// Display Char with TextView
String songName = songtitle2.get(i);
newString = songName.substring(0, 1);
}
j++;
i++;
}
How would i display this in the list view at the appropriate spots. Thank you and i will give u a good rating if u know the answer.
You probably want an Adapter that implements SectionIndexer, specifically AlphabetIndexer. See this or this.
You really should be using an Adapter... there are libraries that do all of this work for you, you shouldn't have to do it yourself in a massive loop!
Here is some sample code to help you get started... note that it is a little out-dated in that it makes use of some deprecated methods (such as managedQuery, etc.). If you wanted to be entirely 100% correct you would want to make use of the LoaderManager.LoaderCallbacks<Cursor interface introduced in the Honeycomb release, but it's a good start.
To sort the displayed views, you can make use of the sortOrder argument in the ContentProvider's query method.

Categories

Resources