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));
}
Related
I'm following the following tutorial http://www.devexchanges.info/2015/03/combining-gridview-and-listview-in-one.html
I'm attempting to change the list view to use an array of strings, and display text rather than images for the bottom part of this application.
I'm not entirely sure what I should be using as my ListView ID here. It seems like I can only use a findViewbyID, but I'd rather just give it an array of strings to populate that list view. Am I missing something simple here?
ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, R.layout.item_grid, CORPS);
ListView lv = (ListView)findViewById(????);
lv.setAdapter(adapter);
Here is the code
public class ListViewActivity extends Activity {
private ExpandableHeightGridView gridView;
private ListView listView;
//drawables array for listview
private static final int[] corpImage = { R.drawable.apple, R.drawable.blackberry_logo,
R.drawable.google, R.drawable.microsoft, R.drawable.mozilla, R.drawable.oracle };
static final String[] CORPS = new String[] { "Microsoft", "Google", "Apple" };
//drawables array for gridview
private static final int[] osImage = { R.drawable.bbos, R.drawable.firefox_os,
R.drawable.ic_launcher, R.drawable.ios, R.drawable.tizen, R.drawable.ubuntu_touch,
R.drawable.wpos, R.drawable.symbian };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
addGridViewAsListViewHeader();
setAdapters();
}
/**
* set header for listview (it's a gridview)
*/
#SuppressLint("InflateParams")
private void addGridViewAsListViewHeader() {
View header = (View) getLayoutInflater().inflate(R.layout.layout_gridview, null);
listView.addHeaderView(header);
gridView = (ExpandableHeightGridView) header.findViewById(R.id.gridview);
// set expand to show all gridview rows
gridView.setExpanded(true);
}
/**
* set adapters for list view and gridview
*/
private void setAdapters() {
// convert int array to Integer array by apache common lang library
Integer[] osImageList = ArrayUtils.toObject(osImage);
Integer[] corpImageList = ArrayUtils.toObject(corpImage);
//Added by me but not working correctly
//ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, R.layout.item_grid, CORPS);
//ListView lv = (ListView)findViewById(R.id.image);
// lv.setAdapter(adapter);
// set listview and gridview adapters
CustomAdapter gridViewAdapter = new CustomAdapter(this, R.layout.item_grid, R.id.image, osImageList);
CustomAdapter listViewAdapter = new CustomAdapter(this, R.layout.item_grid, R.id.image, corpImageList);
gridView.setAdapter(gridViewAdapter);
listView.setAdapter(listViewAdapter);
}
}
If your goal is to only use one string in each line, and display it as a list of items you can simply do this by using array adapters as follows.
ArrayList<String> companies = new ArrayList<>();
companies.add("Google");
companies.add("Microsoft");
// Find a reference to the {#link ListView} in the layout
ListView companiesListView = (ListView) findViewById(R.id.list);
// Create a new {#link ArrayAdapter} of earthquakes
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, earthquakes);
// Set the adapter on the {#link ListView}
// so the list can be populated in the user interface
companiesListView.setAdapter(adapter);
In your layout file you should declare a list view where you want to display this list of items. In this case id of that list view would be "list"
The app crash because of the following code lines.
I have two string-array in a XML file named as Telephones in a folder named values and two TextView in text.xml.
I can not figure out what's going wrong here.
public class MainActivity extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
String[] name= getResources().getStringArray(R.array.names);
String[] number= getResources().getStringArray(R.array.numbers);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.text, name);
listView.setAdapter(arrayAdapter);
ArrayAdapter<String> arrayAdapter1= new ArrayAdapter<String>(this, R.layout.text, number);
listView.setAdapter(arrayAdapter1);
}
}
Your help would be appreciated.
Thank You!
I forgot to add TextView id in MainActivity java but after adding the names_txt won't show up the only one show up is numbers_txt, how fix it please?
public class MainActivity extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
String[] name= getResources().getStringArray(R.array.names);
String[] number= getResources().getStringArray(R.array.numbers);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.text, R.id.names_txt, name);
listView.setAdapter(arrayAdapter);
ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<String>(this, R.layout.text, R.id.numbers_txt, number);
listView.setAdapter(arrayAdapter1);
}
}
so i've got this piece of code and i want it to output attr1 and attr2 in the listview, but the current error i get is: cannot resolve constructor. Here is the piece of code:
private String[][] content = {
{"attr1", "url1"},
{"atrr2", "url2"}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
ListView lv = (ListView) findViewById(R.id.lv);
for(int i = 0; i < content.length; i++) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, content[i][0]);
lv.setAdapter(adapter);
}
}
Hopefully somebody could help me, thanks in advance (sorry for the bad english)
Move the creation of the list outside your for loop like so:
ListView lv = (ListView) findViewById(R.id.lv);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
for(int i = 0; i < content.length; i++) {
adapter.add(content[i][0]);
}
lv.setAdapter(adapter);
You can get the index of the clicked item and use that information to access your array again and get the second piece of information
Hi i want to create dynamical number of checkboxes (it depends on the length of array "genre") in an Android app then i did:
CheckBox [] checkbox;
String [] genre={"party","sport","music","café","education"};
#Override
protected void onCreate(Bundle savedInstanceState) {
checkbox=new CheckBox[genre.length];
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
for(int i=0;i<genre.length;i++){
CheckBox c=new CheckBox(this);
c.setText(genre[i]);
checkbox[i]=c;
}
ArrayAdapter<CheckBox>adapter= new ArrayAdapter<CheckBox>(this, android.R.layout.simple_list_item_1,checkbox);
ListView lv= (ListView)findViewById(R.id.genre_list);
lv.setAdapter(adapter);
}
but in the listview it shows numbers of the checkboxes in memory(I guess), why?
like : Android.wiget.CheckBox#44f03160
Android.wiget.CheckBox#44f03d60
...
You'd rather use the android.R.simple_list_item_checked item layout and enable multiple-choice on the list:
String [] genre={"party","sport","music","café","education"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, genre);
ListView lv= (ListView)findViewById(R.id.genre_list);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setAdapter(adapter);
}
This way you don't have to create the checkboxes manually by yourself
Because of this line
c.setText(genre[i]);
String [] genre={"party","sport","music","café","education"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, genre);
ListView lv= (ListView)findViewById(R.id.genre_list);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setAdapter(adapter);
}
This looks like checkboxes.
I have a ListView with multiple checkboxes (one for each list item). What would be the best way to create an array containing information from the selected checkboxes.
For Example, here's a list.
Item 1 "Ham" Checked
Item 2 "Turkey" NotChecked
Item 3 "Bread" Checked
I would like to create an array containing "ham" and "turkey"
If you used ListView's built-in checkbox method with setChoiceMode() then you only need to call getCheckedItemIds() or getCheckedItemPositions().
public class Example extends Activity implements OnClickListener {
ListView mListView;
String[] array = new String[] {"Ham", "Turkey", "Bread"};
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, array);
mListView = (ListView) findViewById(R.id.list);
mListView.setAdapter(adapter);
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
public void onClick(View view) {
SparseBooleanArray positions = mListView.getCheckedItemPositions();
int size = positions.size();
for(int index = 0; index < size; index++) {
Log.v("Example", "Checked: " + array[positions.keyAt(index)]);
}
}
}
If the Adapter is bound to a Cursor, then this becomes much more practical.