Hi I am new in Android. Could anyone tell me pls whats the wrong with the following code:
public class ListApp extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView lText = new TextView(this);
lText.setId(0);
ListView lView = new ListView(this);
String[] lStr = new String[]{"AA","BB", "CC"};
ArrayAdapter lAdap = new ArrayAdapter(this,lText.getId(),lStr);
lView.setAdapter(lAdap);
lView.setFocusableInTouchMode(true);
setContentView(lView);
}
}
here's a solution that does not require you to write any xml layouts. it uses standard android layouts where possible and no inflation is necessary:
Dialog dialog = new Dialog(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Color Mode");
ListView modeList = new ListView(this);
String[] stringArray = new String[] { "Bright Mode", "Normal Mode" };
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
modeList.setAdapter(modeAdapter);
builder.setView(modeList);
dialog = builder.create();
Try this..
Paste the following code in list_item.xml.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" android:textColor="#ffffff" android:textStyle="bold" android:background="#drawable/border_cell">
</TextView>
Here is the activity class....
public class UsersListActivity extends ListActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] statesList = {"listItem 1", "listItem 2", "listItem 3"};
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
statesList));
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
"You selected : "+((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
Best practice is to separate view (xml layout) and controller (Activity).
If you dont want this, try to put
setContentView(lView);
at the beginning of onCreate
public class ListApp extends ListActivity
Make a layout XML file with your listview and use findViewById to set it's adapter after first setting the content view to your layout
Related
I don't need anything fancy, just bigger items in a list (it's too tiny) and different background. Currently it looks like so:
declaration done like this:
<Spinner android:id="#+id/sp_serverName" android:layout_width="fill_parent" android:layout_height="wrap_content" />
And I use code to bind items:
String[] items = new String[] { "Chai Latte", "Green Tea", "Black Tea" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
spinnerServerName.setAdapter(adapter);
spinnerServerName.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
Log.v("item", (String) parent.getItemAtPosition(position));
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
// TODO Auto-generated method stub
}
});
In place of android default layout give yours like :
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
replace with :
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.custom_spinner_item, items);
I'm trying to add a search form with a edittext and dropdownlist spinner to alert dialogue. I have managed to get the edit text to display, but I cannot figure out how to add the spinner. I'm new to android so any help would be appreciated.
private void LoadSearchDialogue()
{
android.app.AlertDialog.Builder alertDialogSearchForm = new android.app.AlertDialog.Builder(this);
alertDialogSearchForm.setTitle("Search Form");
alertDialogSearchForm.setMessage("Please complete form");
final EditText textViewInputUsername = new EditText(this);
final Spinner spinnerSearchOptions = new Spinner(this);
Spinner dropdownSearchOptions = (Spinner)findViewById(R.id.spinnerSearchOptions);
String[] items = new String[]{"Customer", "Employee","Date" };
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdownSearchOptions.setAdapter(adapter);
mQuery = (EditText) findViewById(R.id.query);
mSearchOption = (Spinner) findViewById(R.id.spinnerSearchOptions);
alertDialogSearchForm.setView(textViewInputUsername);
alertDialogSearchForm.setPositiveButton("Continue",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Do nothing here, overriding alert dialogue button
}
});
final android.app.AlertDialog dialog = alertDialogSearchForm.create();
dialog.show();
dialog.getButton(android.app.AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Boolean closeAlertDialogueOnValidInput = false;
//Check if query is entered,
String mQueryString = textViewInputUsername.getText().toString().trim();
if(mQueryString.length()<=0)
{
Toast.makeText(SearchActivity.this, "Please enter query", Toast.LENGTH_SHORT).show();
}
else
{
String q = mQueryString.toString();
String o = mSearchOption.getSelectedItem().toString();
SearchResults(q, o);
}
if(closeAlertDialogueOnValidInput)
dialog.dismiss();
}
});
}
Use the below code
public class WvActivity extends Activity {
TextView tx;
String[] s = { "India ", "Arica", "India ", "Arica", "India ", "Arica",
"India ", "Arica", "India ", "Arica" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ArrayAdapter<String> adp = new ArrayAdapter<String>(WvActivity.this,
android.R.layout.simple_spinner_item, s);
tx= (TextView)findViewById(R.id.txt1);
final Spinner sp = new Spinner(WvActivity.this);
sp.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
sp.setAdapter(adp);
AlertDialog.Builder builder = new AlertDialog.Builder(WvActivity.this);
builder.setView(sp);
builder.create().show();
}
}
I suggest that you create a simple xml layout for your dialog:
<!-- alert.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Spinner
android:id="#+id/mySpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
After that you inflate a View from this layout file, set this view in setView method, fill the Spinner with data and do all the stuff that you need to do. All the things that you currently do to define behavior for button clicks will remain the same; but the listeners for your EditText and Spinner, if needed, will be set outside the Dialog (with findViewById and setOnClickListener).
To inflate a View from xml you need to get LayoutInflater and use it to create a View:
View alertView = getLayoutInflater().inflate(R.layout.alert, null);
To populate the Spinner with data, you should use an Adapter. There is some code from here:
// you need to have a list of data that you want the spinner to display
List<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("item1");
spinnerArray.add("item2");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, spinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner sItems = (Spinner) findViewById(R.id.spinner1);
sItems.setAdapter(adapter);
If you would like to stick to your way of creation of the View, you need to add not only your EditText to your AlertDialog, but a ViewGroup (LinearLayout for example), that will contain both EditText and Spinner.
I am trying to set and get value to a spinner for item dynamically ?
any ideas ?
I just need help with the Spinner behavior right now, the rest should be quite easy.
Spinner spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, android.R.id.text1);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
spinnerAdapter.add("value");
spinnerAdapter.notifyDataSetChanged();
spinner.setSelection(0);
String text = spinner.getSelectedItem().toString();
1
2
3
XML file:
<Spinner android:id="#+id/Spinner01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Java file:
public class SpinnerExample extends Activity {
private String[] arraySpinner;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.arraySpinner = new String[] {
"1", "2", "3", "4", "5"
};
Spinner s = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner);
s.setAdapter(adapter);
}
// To get value from spenner
spinner.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parent, View view, int pos, long id) {
Object item = parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView parent) {
}
});
}
Try this this will help you.
Spinner mySpinner= (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> myAdapter= new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item);
mySpinner.setAdapter(myAdapter);
If you want to add the elements dynamically, you can by doing this:
myAdapter.add("newelement");
myAdapter.notifyDataSetChanged();
Spinner spinn = findViewById(R.id.socialmedia_spinner_adsocmeda);
ArrayList<String> fam = new ArrayList<>();
fam.add("INSTAGRAM");
fam.add("FACEBOOK");
fam.add("GOODWALL");
fam.add("TWITTER");
fam.add("YOUTUBE");
fam.add("LINKEDIN");
fam.add("SNAPCHAT");
ArrayAdapter<String> myAdapter= new ArrayAdapter<String> (ProfileActivity.this,
android.R.layout.simple_list_item_1, fam.toArray(new String[0]));
spinn.setAdapter(myAdapter);
That's a way we can add data to spinner.
I would like to know if it is possible to create a list view in runnable using something like this ? Could someone please give me an example to do it? Thank you.
public void testBtnListViewClick(View v) {
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
LinearLayout ll = new LinearLayout(this);
ListView lv = new ListView(this);
String[] values = new String[10];
for(int i=0;i<10;i++){
values[i] = ""+i;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, values);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
Toast.makeText(getBaseContext(), ""+arg2,Toast.LENGTH_SHORT).show();
Log.d("DEBUG", ""+arg2);
}
});
//ll.addView(lv);
ll.addView(lv, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
setContentView(ll);
}
}
Yes ..you can..runOnUiThread() which runs in the main thread.In android UI changes like Views can be modiied or added only in UI thread..Its in the UI thread so no problem for yours..
inside that this refers to runnable object..so change these lines
LinearLayout ll = new LinearLayout(this);
ListView lv = new ListView(this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, values);
into
LinearLayout ll = new LinearLayout(MainActivity.this);
ListView lv = new ListView(MainActivity.this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list_item, values);
I got this piece of code that creates new TextView, then adds it to ArrayList<View> and when it is finished adding TextViews to Array it sets adds that Array into ListView. But somehow my ListView is appearing empty. Any idea what am I doing wrong?
Here is the code:
ListView lv = (ListView) findViewById(R.id.listView1);
ArrayList<View> textvs = new ArrayList<View>();
for (int i=0; i<10;i++) {
TextView tv = new TextView(MainActivity.this);
tv.setText(""+i);
textvs.add(tv);
}
lv.addTouchables(portit); // lv is my listview
You should use ArrayAdapter. You did this the wrong way. Here is a sample:
public class ArrayAdapterDemo extends ListActivity {
String[] items = { "this", "is", "a", "really", "silly", "list" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1,
items));
}