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
Related
I have a Problem with a ListView.
Here is my code:
private void setViewProtocol() {
setContentView(R.layout.protocol);
List<String> logs = new ArrayList<String>();
logs.add("TEST 1");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.protocol, logs);
ListView lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(adapter);
}
It crashes - but when I remove the line logs.add("TEST 1") it works.
Can anyone help me? Why does it work when I call it with an empty list?
Thanks
Use the following:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, logs);
If you give a custom layout you have to have an TextView with the ID android.R.id.text1 or you have to override the getView()
Try this way,hope this will help you to solve your problem.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.protocol);
List<String> logs = new ArrayList<String>();
logs.add("TEST 1");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.simple_list_item_1, logs);
ListView lv = (ListView) findViewById(android.R.id.listView1);
lv.setAdapter(adapter);
}
I'm trying to show files in list view :
It works perfectly. Now I'm trying to add TextView to this Listview so I tried
this i:
I don't want to use adapter class.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv =(TextView) findViewById(R.id.textView2);
path = Environment.getExternalStorageDirectory().getPath();
file = new File(path);
setListView();
}
public void setListView(){
String[] mFilesList = file.list();
mListView = (ListView)findViewById(R.id.listView1);
mArray = new ArrayList<String>();
fArray = new ArrayList<String>();
for(int i = 0; i<mFilesList.length; i++){
mArray.add(mFilesList[i]);
}
for (int i = 0; i < mFilesList.length; i++) {
fArray.add(mFilesList[i].length()+" files");
}
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(getApplicationContext()
, R.layout.rows, R.id.textView1,mArray);
ArrayAdapter<String> mAdapterSecond = new ArrayAdapter<String>(getApplicationContext()
, R.layout.rows, R.id.textView2,fArray);`
mListView.setAdapter(mAdapterSecond);
mListView.setAdapter(mAdapter);
It's showing just one textview. Why?
The ListView accepts only one Adapter. Your last code line changes the ListView adapter from mAdapterSecond to mAdapter. What you need in your case is to implement your custom adapter that fill the two TextView as you wish.
I have a list view that displays records from a database. Each row in the listview has a checkbox. How do I...
Create a toast message displaying the value of the selected item?
Identify the records selected by the user in the listview?
Iterate through each selected item and delete only the rows selected in the listview from the database?
I've been following this helpful tutorial here: //http://www.vogella.com/articles/AndroidSQLite/article.html
public class PhoneNumberDataBaseListView extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
NumbersDataSource datasource = new NumbersDataSource(this);
datasource.open();
String number = "123";
datasource.createNumber(number);
List<Number> values = datasource.getAllNumbers();
ArrayAdapter<Number> adapter = new ArrayAdapter<Number>(this, android.R.layout.simple_list_item_multiple_choice, values);
setListAdapter(adapter);
}
public void deleteNumber() {
NumbersDataSource datasource = new NumbersDataSource(this);
datasource.open();
ListView LV = (ListView) findViewById(android.R.id.list);
List<Number> values = datasource.getAllNumbers();
ArrayAdapter<Number> adapter = new ArrayAdapter<Number>(this, android.R.layout.simple_list_item_multiple_choice, values);
setListAdapter(adapter);
SparseBooleanArray checkedItems = LV.getCheckedItemPositions();
for (int i = 0; i < checkedItems.size(); i++) {
if(checkedItems.valueAt(i)) {
Number item = adapter.getItem(i);
Toast t = Toast.makeText(this, item.getNumber().toString(), Toast.LENGTH_LONG);
t.show();
}
}
}
Method getCheckedItemPositions() returns SparseBooleanArray that indicates which items are checked (if item on specific position is checked, method valueAt(position) invoked on that array returns true). Class DBAdapter from tutorial you are following provides you with deleteTitle(long rowId) method which you should use to acheive what you want.Therefore your code should look more or less like this:
SparseBooleanArray checkedItems = listView.getCheckedItemPositions();
for (int i = 0; i < checkedItems.size(); i++) {
if(checkedItems.valueAt(i)) {
db.deleteTitle(adapter.getItemId(i));
}
}
Of course SimpleCursorAdapter and DBAdapter should be stored in object attributes so that you have access to them from both methods (onCreate and deleteRecord). By the way, View parameter passed to deleteRecord method is not needed.
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));
}
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.