I'm making a simple scoring system for a minesweeper clone and I keep getting the Resources$NotFoundException when it crashes. I'm pretty sure it's the getIdentifier function that's causing it and I've had nothing but trouble with this function in the past. What am I doing wrong?
Thanks in advance.
private void doScoring()
{
SharedPreferences sp = this.getSharedPreferences("scores", Context.MODE_PRIVATE);
int score = (int) Math.ceil(1000/this.getIntent().getExtras().getDouble("time"));
int[] scores = new int[10];
String[] names = new String[10];
int newOffset = 0;
for(int i = 0; i < 10; i++)
{
if(sp.getInt("s"+i,999)>score&&newOffset==0)
{
newOffset++;
names[i] = "Prompted Name";
scores[i] = score;
}
scores[i] = sp.getInt("s"+(i-newOffset), 999);
names[i] = sp.getString("n"+(i-newOffset), "No Score");
((TextView) this.findViewById(getResources().getIdentifier("score_name_"+(i+1), "id", "edu.laura.omc_prototype"))).setText(scores[i]);
((TextView) this.findViewById(getResources().getIdentifier("score_"+(i+1), "id", "edu.laura.omc_prototype"))).setText(names[i]);
}
}
Related
I'm tried to sort the database from a web service by using the bubble sort.
itemLists[i] = new ItemList(
a.getString(id),
a.getString(nama),
a.getString(latitude),
a.getString(longitude),
a.getString(alamat));
double terendah = Double.valueOf(a.getString("TAG_TERENDAH")).doubleValue();
//double terendah = a.getDouble(terenda);
harga[i] = terendah;
//bubble sort
double tHarga;
ItemList tItemList;
for (int k = 0; k < tempatrental.length(); k++) {
for (int l = 0; l < tempatrental.length() - (k + 1); l++) {
if (harga[l] > harga[l + 1]) {
tHarga = harga[l];
tItemList = itemLists[l];
harga[l] = harga[l + 1];
itemLists[l] = itemLists[l + 1];
harga[l + 1] = tHarga;
itemLists[l + 1] = tItemList;
}
}
Anyways, database from the web service is array type, so I store it in the array from another class.
public class ItemList{
public String label1, label2, label3, label4, label5;
public ItemList(String label1, String label2, String label3, String label4, String label5) {
super();
// TODO Auto-generated constructor stub
//this.icon = icon;
this.label1 = label1;
this.label2 = label2;
this.label3 = label3;
this.label3 = label4;
this.label3 = label5;
}
The problem came when I tried to show the results of sorting in the list view.
this.setListAdapter (new ArrayAdapter<String>(TermurahActivity.this, android.R.layout.simple_list_item_1, itemLists));
Eclipse says : The Constructor ArrayAdapter(TermurahActivity, int, ItemList[]) is undefined. Please help fix to fix this...
Once you defined ArrayAdapter<String> the adapter expects a list of String.
ItemLists is a list of ItemList so you should use:
new ArrayAdapter<ItemList>(TermurahActivity.this,
android.R.layout.simple_list_item_1, itemLists);`
hello i want my app find prime between two number with sieve algorithm i am use this code but its not working well what can i do for improve this code to my goal please help me
public int[] GetPrimes() {
int[] primesData = new int[maxValue+1];
int[] results = new int[maxValue];
int i;
int result_count = 0;
for (i=MIN_VALUE; i<=maxValue; i++) {
primesData[i] = PRIME_UNKNOWN;
}
for (i=MIN_VALUE; i<=maxValue; i++) {
if (primesData[i] == PRIME_UNKNOWN) {
primesData[i] = PRIME_YES;
results[result_count++] = i;
int j;
for (j=i; j<=maxValue; j = j + i) {
primesData[j] = PRIME_NO;
}
}
}
int[] retval = new int[result_count];
for (i=0; i<result_count; i++) {
retval[i] = results[i];
}
tvresult.setText(retval.toString());
return retval;
}
}
The algorithm is correct. Likely you are launching it with wrong values as input.
Ensure to have MIN_VALUE set to value 2 and prepopulate retval with 1 i.e.:
int[] retval = new int[result_count+1];
retval[0] = 1;
for (i=0; i<result_count; i++) {
retval[i+1] = results[i];
For the printing issue:
tvresult.setText(Arrays.toString(retval));
Because retval.toString() just returns the object reference not the content. This is true for any Java array. Instead use Arrays.toString().
this is my code
LinearLayout ll[] = new LinearLayout [9];
ll[0]=(LinearLayout)findViewById(R.id.rlay1);
ll[1]=(LinearLayout)findViewById(R.id.rlay2);
ll[2]=(LinearLayout)findViewById(R.id.rlay3);
ll[3]=(LinearLayout)findViewById(R.id.rlay4);
ll[4]=(LinearLayout)findViewById(R.id.rlay5);
ll[5]=(LinearLayout)findViewById(R.id.rlay6);
ll[6]=(LinearLayout)findViewById(R.id.rlay7);
ll[7]=(LinearLayout)findViewById(R.id.rlay8);
ll[8]=(LinearLayout)findViewById(R.id.rlay9);
ll[9]=(LinearLayout)findViewById(R.id.rlay10);
You could put the ids into an array:
int[] ids = new int[] {R.id.rlay1, R.id.rlay2, R.id.rlay3, R.id.rlay4, R.id.rlay5,
R.id.rlay6, R.id.rlay7, R.id.rlay8, R.id.rlay9, R.id.rlay10};
LinearLayout ll[] = new LinearLayout [10];
for (int i = 0; i < 10; i++) {
ll[i] = (LinearLayout)findViewById(ids[i]);
}
I prefer this solution over the use of Resources.getIdentifier since it does not cause resource id lookup costs at runtime.
LinearLayout ll[] = new LinearLayout [10];
int counter = 0;
for (int i = 1; i < 11; i++) {
int id = getResources().getIdentifier("rlay"+i, "id", getPackageName());
ll[counter++] = (LinearLayout)findViewById(id);
}
Add this method to your code:
protected final static int getResourceID
(final String resName, final String resType, final Context ctx)
{
final int ResourceID =
ctx.getResources().getIdentifier(resName, resType,
ctx.getApplicationInfo().packageName);
if (ResourceID == 0)
{
throw new IllegalArgumentException
(
"No resource string found with name " + resName
);
}
else
{
return ResourceID;
}
}
Then change your code so:
LinearLayout ll[] = new LinearLayout [10];
Context ctx = getApplicationContext();
for (int i = 0; i < 10; i++)
{
ll[i] = (LinearLayout) findViewById(getResourceID("rlay" + (i + 1), "id", ctx));
}
For my android app I need to make an array of View ID's.
The array will hold 81 values so it's quite lengthy to add them one by one.
This is how it looks now:
cells[0] = R.id.Square00;
cells[1] = R.id.Square01;
cells[2] = R.id.Square02;
cells[3] = R.id.Square03;
cells[4] = R.id.Square04;
cells[5] = R.id.Square05;
//All the way to 80.
Is there a shorter/more efficient way of doing this?
Thankfully, there is. Use getIdentifier():
Resources r = getResources();
String name = getPackageName();
int[] cells = new int[81];
for(int i = 0; i < 81; i++) {
if(i < 10)
cells[i] = r.getIdentifier("Squares0" + i, "id", name);
else
cells[i] = r.getIdentifier("Squares" + i, "id", name);
}
Sam's answer is better but i think i should share an alternative
int [] ids = new int [] {R.id.btn1, R.id.btn2, ...};
Button [] arrayButton = new Button[ids.length];
for(int i=0 ; i < arrayButton.length ; i++)
{
arrayButton[i] = (Button) findViewById(ids[i]);
}
Modified form of Sam Answer
No need of if else use Integer String Formating
Resources r = getResources();
String name = getPackageName();
int[] resIDs = new int[81];
for(int i = 0; i < 81; i++)
{
resIDs[i] = r.getIdentifier("Squares0" + String.format("%03d", i), "id", name);
}
Hi can some one suggest me a sample example of how i can sort the textviews based on the numbers in textviews. I am able to get the text from the TextViews need to sort and place the lowest number first.
Thank you.
public void sortNumbers(View v) {
String[] numbers = new String[7];
numbers[0] = textView23.getText().toString();
numbers[1] = textView33.getText().toString();
numbers[2] = textView43.getText().toString();
numbers[3] = textView53.getText().toString();
numbers[4] = textView63.getText().toString();
numbers[5] = textView73.getText().toString();
numbers[6] = textView83.getText().toString();
Integer[] intValues = new Integer[numbers.length];
for (int i = 0; i < numbers.length; i++) {
intValues[i] = Integer.parseInt(numbers[i].trim());
}
Collections.sort(Arrays.asList(intValues));
for (int i = 0; i < intValues.length; i++) {
Integer intValue = intValues[i];
//here I want to assign sorted numberes to the TextViews
}
}
So I have followed Jeffrey's advice. Here is the code which still doesn't work properly. What could be wrong?
Created an array of TextViews:
TextView[] tvs = new TextView[7];
tvs[0] = textView23;
tvs[1] = textView33;
tvs[2] = textView43;
tvs[3] = textView53;
tvs[4] = textView63;
tvs[5] = textView73;
tvs[6] = textView83;
Sorted the array and assinged new values to the TextViews:
Arrays.sort(tvs, new TVTextComparator());
textView23.setText(tvs[0].getText().toString());
textView33.setText(tvs[1].getText().toString());
textView43.setText(tvs[2].getText().toString());
textView53.setText(tvs[3].getText().toString());
textView63.setText(tvs[4].getText().toString());
textView73.setText(tvs[5].getText().toString());
textView83.setText(tvs[6].getText().toString());
And here is the Comporator class:
public class TVTextComparator implements Comparator<TextView> {
public int compare(TextView lhs, TextView rhs) {
Integer oneInt = Integer.parseInt(lhs.getText().toString());
Integer twoInt = Integer.parseInt(rhs.getText().toString());
return oneInt.compareTo(twoInt);
}
}
to sort your textViews, first put them in an array,
TextView[] tvs = new TextView[7];
tvs[0] = textView23;
tvs[1] = textView33;
// and so on
note that if you have handle to the parent container, you could easily build the array by using ViewGroup.getChildCount() and getChildAt().
now write a comparator for a text view,
class TVTextComparator implements Comparator<TextView> {
#Override
public int compare(TextView lhs, TextView rhs) {
return lhs.getText().toString().compareTo(rhs.getText().toString());
// should check for nulls here, this is NOT a robust impl of compare()
}
}
now use the comparator to sort the array,
Arrays.sort(tvs, 0, tvs.length, new TVTextComparator());
public void sortNumbers(View v) {
String[] numbers = new String[7];
numbers[0] = textView23.getText().toString();
numbers[1] = textView33.getText().toString();
numbers[2] = textView43.getText().toString();
numbers[3] = textView53.getText().toString();
numbers[4] = textView63.getText().toString();
numbers[5] = textView73.getText().toString();
numbers[6] = textView83.getText().toString();
Integer[] intValues = new Integer[numbers.length];
for (int i = 0; i < numbers.length; i++) {
intValues[i] = Integer.parseInt(numbers[i].trim());
}
Collections.sort(Arrays.asList(intValues));
textView23.setText(intValues[0]);
textView33.setText(intValues[1]);
textView43.setText(intValues[2]);
textView53.setText(intValues[3]);
textView63.setText(intValues[4]);
textView73.setText(intValues[5]);
textView83.setText(intValues[6]);
}