[Q] How to get a Reference ID to accept a variable input? - Android Q&A, Help & Troubleshooting

Hi, I'm trying to create an application, and i require to use a variable input for a reference ID to display different arrays here is what i am using at the moment
Code:
public void verbTenses() {
Spinner verbs = (Spinner) findViewById(R.id.verbs);
Spinner tenses = (Spinner) findViewById(R.id.verb_tenses);
[COLOR="RoyalBlue"]ListAdapter adapter = R.array.(verbs.getSelectedItem() + "_" + tenses.getSelectedItem());[/COLOR]
ListView list = (ListView) findViewById(R.id.ListView01);
list.setAdapter(adapter);
}
the error area is highlighted
Thank you

Questions or Problems Should Not Be Posted in the Development Forum
Please Post in the Correct Forums
Moving to Q&A

Related

[Q] Filter Contacts by Group with ContactsContract

Hello!
I'm developing a big application and a part of it is about contacts. I want to, first, get all the groups of contacts. And when the users selects one, show all the contacts of the group.
I've seen other applications and widgets (like LauncherPro widget or HTC Sense Widget) than can do it, but when I try, I don't get same results.
My code is:
1.- To get an example group (this is just for testing, in real application user selects):
Code:
long idGroup = 0;
String[] GROUP_PROJECTION = new String[] {
ContactsContract.Groups._ID, ContactsContract.Groups.TITLE };
Cursor c = mcp.getContext().getContentResolver().query(ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, null, null, ContactsContract.Groups.TITLE + " ASC");
while(c.moveToNext())
{
if (c.getString(1).contains("Family")){
idGroup = c.getLong(0);
}
}
c.close();
2.- To filter Users by group??
Thank you!
2.- To filter Users by group??
Same question... Cannot find the answer... Anybody knows ?
well maybe i should not revive this thread, but i had a lot of issues finding this,, my code working and you can filter and avoid repeated groups and only with phone numbers
Code:
String[] GROUP_PROJECTION = new String[] {
ContactsContract.Groups._ID, ContactsContract.Groups.TITLE,ContactsContract.Groups.SUMMARY_COUNT ,ContactsContract.Groups.SUMMARY_WITH_PHONES };
Cursor c = context.getApplicationContext().getContentResolver().query(
ContactsContract.Groups.CONTENT_SUMMARY_URI,
GROUP_PROJECTION,
ContactsContract.Groups.SUMMARY_WITH_PHONES +"> 0"
, null, ContactsContract.Groups.TITLE + " ASC");

[Q] Dynamically edit Uri for playing audio-files

I'm trying to play an mp3-file located in res/raw.
Problem is that I have a lot of audio that needs to be played dynamically.
This code does work for "mySound.mp3".
Code:
mPlayer = MediaPlayer.create(main.this, R.raw.mySound);
mPlayer.start();
Whereas this code does NOT work:
Code:
String soundName = "mySound";
Uri uri_soundName = Uri.parse("R.raw." + soundName);
mPlayer = MediaPlayer.create(main.this, uri_soundName);
mPlayer.start();
I've tried various other syntaxes to get the uri right, for instance:
Code:
uri_soundName = Uri.parse("android.resource://com.myProject/myApp/raw/" + soundName);
...but to no avail.
Does anybody have an idea what I'm doing wrong?
Thanks,
Dan
Android Software Development
Questions or Problems Should Not Be Posted in the Development Forum
Please Post in the Correct Forums
Moving to Q&A

Ping app

hey guys,
Building an app that runs a ping command at the moment and I can't quite get it to work. If I modify the command to something that isn't a terminal command then it'll output my error statement but i can't get it to display the ping output. any help would be awesome. I know my outputs for my error are bad but it's an easy way to determine what path it's outputting.
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import java.io.*;
import java.lang.Process;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView Text = new TextView(this);
Runtime runtime = Runtime.getRuntime();
try
{
Process proc = runtime.getRuntime().exec("system/bin/ping 192.168.1.1");
BufferedReader in = new BufferedReader(
new InputStreamReader(proc.getInputStream()));
String line = "null";
while ((line = in.readLine()) != null) {
Text.setText(in.readLine());
}
}
catch (Exception e)
{
String line="55";
Text.setText(line);
}
setContentView(Text);
}
}
Thanks for any help you guys can give me,
Adam
Hey,
Try using "system/bin/ping -c 1 192.168.1.1" instead.
And then add this line just after that:
Code:
proc.waitFor();
and while reading the output of the ping you might want to do it like this maybe?
Code:
String line = "";
String result = "";
while (line != null)
{
result = result + "\n" + line;
line = in.readLine();
}
Text.setText(result);
If you want to ping more than 1 packet I think it would be better to make a new thread and do proc.waitFor() in that thread. Then send a message using a handler to set the output of the ping to the TextView
k i have done that and that does make more sense but Im still getting a black screen on the output. i am testing on a Sony tab s and using AIDE (on the device) cause my eclipse is broken. this is how my code looks now,
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import java.io.*;
import java.lang.Process;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView Text = new TextView(this);
Runtime runtime = Runtime.getRuntime();
try
{
Process proc = runtime.getRuntime().exec("system/bin/ping 192.168.1.1");
BufferedReader in = new BufferedReader(
new InputStreamReader(proc.getInputStream()));
String line = "null";
while ((line = in.readLine()) != null) {
Text.setText(in.readLine());
}
}
catch (Exception e)
{
String line="55";
Text.setText(line);
}
setContentView(Text);
}
}
Is there anything else I could be missing??
Thanks,
Adam
Hey, I think you posted the same code again.
I am not sure if you can read the process stream before it is complete and ping does take a long time to complete. So your main thread is blocking on it and it doesn't get to executing the setContentView.
Thats why I think going for a seperate thread is a better option.
so it would be better to put the ping into a new class and call on it when i need it??
Not a seperate class, a seperate thread to be more specific.
Even if you do put the ping code in a seperate class' method and call that method in onCreate it will still run on the your applications main thread.
What I was trying to say is something along the lines of the following code:
Code:
private TextView textView;
private Process process;
private Handler handler;
private Thread pingThread;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
textView = new TextView(this);
textView.setText("Pinging...");
setContentView(textView);
try
{
process = Runtime.getRuntime().exec("system/bin/ping -c 5 192.168.1.1");
handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
String result = "";
while(line != null)
{
result = result + "\n" + line;
line = reader.readLine();
}
reader.close();
textView.setText(result);
}
catch(Exception e)
{
e.printStackTrace();
textView.setText("Error");
}
}
};
pingThread = new Thread()
{
@Override
public void run()
{
try
{
process.waitFor();
}
catch(Exception e)
{
e.printStackTrace();
}
handler.sendEmptyMessage(0);
}
};
pingThread.start();
} catch (Exception e)
{
e.printStackTrace();
}
}
I am not sure if this is exactly how you want your app to behave. There might be a better way of doing what you want than what I have suggested but I tested the above code and it works for me.
thats wicked, basically what want my program to do is to run a ping command through a usb to rj45 adapter. at the moment I just need it to do the ping command through wifi and once that was working I was going to set it up to go through usb.
I wonder if there's something I'm doing wrong like I'm build the apk in AIDE on the tablet or something, cause I'm still getting a black screen when booting the app.
If I error the code up a bit, Like put "system/bin/pi ....." instead I do get pinging on the app but no error output and if the code is fine then nothing displays
That is very strange :S.
If you use the incorrect command it works fine but when you use the correct command it doesn't?
Can you post your code? or provide more details if possible?
the code is just the code you posted cause I thought if a I could get it working with that code then modify it to what it needs to do, when the command is incorrect it displays "ping....." but doesn't display an error, I'm going to install eclipse and build the package with that and see how it goes. did you test on a tablet and what did you use to deploy the package?
Adz117 said:
the code is just the code you posted cause I thought if a I could get it working with that code then modify it to what it needs to do, when the command is incorrect it displays "ping....." but doesn't display an error, I'm going to install eclipse and build the package with that and see how it goes. did you test on a tablet and what did you use to deploy the package?
Click to expand...
Click to collapse
I think i know whats happening. It isnt the Ide and using eclipse wont make much of a difference.
Are you usIng something like -c 5 in the ping command? Cause if you aren't i think ping is Going to take a really long time and all you'll see on the screen is "Pinging..."
Yea I have got that in the command, when the code is correct and working I dont get anything all I get is a black screen. Its when I error the command up a bit that I get pinging
Sent from my Sony Tablet S using XDA
hey guys,
just got eclipse running and tested it on an avd emulator and it runs perfect, so the question is what would cause it not to run on my tablet?
I have no idea! I don't have access to an Android Tablet, so I can't test it out! I tested it out on my phone too and it works just fine.
One thing I can suggest though is:
Put log statements throughout the program to signify where the the control has reached. Then run it on your tablet. That should shed some light on where the code is failing on your tablet.
tested it on my phone which is running 2.3 and it failed on there as well, "ping......" would pop up for about a second then disappear, also if I use a command like ls instead of ping on my tablet it will work perfectly fine so I'm guessing for some reason past android 2.1 it doesn't like the ping command. any ideas?
I tested it out on phone with 2.2. Did you try putting log statements and checking where it is failing.
You could also add breakpoints in your code and run it?

Help with XML NodeList

Im a noob to Android programming but have some experience with xml in other languages, here is what i have....
This is my XML
Code:
<myrooms>
<room type="private" lastupdate="1347500059" roomname="03EBFDF4AB76E9599A979AB1FED4" roomalias="Ryan,,,chronic Chris">
<entry timestamp="1347493468" handle="Ryan" sender="599A979AB1FED4">Entry one</entry>
<entry timestamp="1347499372" handle="Ryan" sender="599A979AB1FED4">entry two</entry>
<entry timestamp="1347500059" handle="Ryan" sender="599A979AB1FED4">entry three</entry>
</room>
</myrooms>
Code:
Document doc_roomlist;
NodeList nodelist_roomlist;
doc_roomlist = WebRequest.XMLfromString(newxml); //newxml contains the above xml
nodelist_roomlist = doc_roomlist.getElementsByTagName("room");
int size = nodelist_roomlist.getLength();
for (int z = 0; z < size; z++) {
mNode=nodelist_roomlist.item(z);
String roomname = mNode.getAttributes()
.getNamedItem("roomname").getNodeValue();
/// so the above is okay i can get all the attributes of the room nodes
/// but i'm having problems getting the entry nodes
NodeList entries=mNode.getElementsByTagName??????? <<< thats what i cant figure out how to get the entries into another nodelist
}
I cannot seem to get the entry nodes into another nodelist any help would really be appreciated.
when i do
NodeList nl = mNode.getChildNodes();
instead of returning the 3 entry nodes
it returns 6 nodes
one called #text and then my node
then #text and my other entry
so why is getChildNodes making two nodes for each <entry> and what is the #text?
Questions or Problems Should Not Be Posted in the Development Forum
Please Post in the Correct Forums & Read the Forum Rules
Thanks ✟
Moving to Q&A
really nobody can explain this nodelist thing to me? why there are 2 nodes for every one ??

How to add list view items to an array list? or a work around to achieve the below?

In the below code, where the item’s position and title is getting saved in list_items works fine if list_items and items are ArrayList. For ex;
Code:
ArrayList.add(ArrayList.get(position));
But I am trying to use listview:
Code:
ArrayList.add(Listview.get(position));
The problem exists on line # 10
Code:
1. final Listadapter Adapter = new Listadapter(this,packageList1, packageManager);
2. items.setAdapter(Adapter);
3. items.setChoiceMode(apps.CHOICE_MODE_MULTIPLE_MODAL);

4. items.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
5. @Override
6. public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {

7. count = count +1;

8. mode.setTitle(count + "items selected");
9. //problem resides in the below line

10. [B]list_items.add(items.get(position));[/B]

11. }

In the above code, on selecting an item from listview I am trying to save it in an Arraylist i.e list_items which could be used in onActionItemClicked method for further action on that respective item.
Any ideas on how to solve or find a workaround to use a listview in the above situation?

Categories

Resources