I don't know why this code is using findViewById to get the list view. Isn't the list view native to the ContentView that I set earlier? If not how do I set up a ListView for that Content View? I really just want to know if the setting up of the list view is necessary.
setContentView(R.layout.imgp_activity_home);
final String[] values = new String[] {"No Docs", "Pending", "Processed"};
final ListView listview = (ListView) findViewById(R.id.???);
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i<values.length; i++) {
list.add(values[i]);
...
listview.setAdapter((ListAdapter) adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
...
I feel like I should be able to add the listener to the array list somehow.
This is the tutorial I used to get me started.
in you layout floder about the activity layout the XML file called imgp_activity_home.xml.in this file you may have the tags about<listview>. set tags like <listview android:id="#+id/listname">.then in the Java file you can findviewbyid(r.id.listname).if you didn't declare the listview tag.you must new a listview object in you code.
From the example you should write
final ListView listview = (ListView) findViewById(R.id.listview);
Related
Hello I'm currently trying to make my first app on android and I've gotten some problems. I'm trying to make a todo-list app so want some kind of input to be transformed into checkboxes. I've made it work with radio buttons using radioGroup. But when using Checkboxes with ListView it just doesn't work.
Here's my code:
public class MainActivity extends AppCompatActivity {
EditText t;
ListView listView;
ArrayList<CheckBox> checkList = new ArrayList<>();
int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void add(View view){
t = findViewById(R.id.input);
String s = t.getText().toString();
CheckBox check = new CheckBox(this);
checkList.add(check);
listView = findViewById(R.id.list);
checkList.get(i).setText(s);
listView.addView(checkList.get(i));
i++;
t.setText("");
}}
The app crashes saying something about adapterView
You're unfortunately doing something wrong here. You are trying to add your view to Listviews on
listView.addView(checkList.get(i));
This is not the correct way to use ListViews. You have to use an adapter. The adapter would need to provide data for the listview to load.
Please have a look at this tutorial for how to use listviews.
Below is a summary of the steps to use a listView correctly.
Create a new layout file (say custom_cell.xml) inside your layouts folder.
Insert a checkbox inside your custom_cell.xml and place an Id which you can use later to identify the checkbox
Create an adapter for your listview
Override the getView method
Inside the getView method, inflate a new cell using the custom_cell.xml or reuse an existing cell if provided
Reference the Checkbox by using the Id you provided in the custom_cell.xml file
i'm doing a system of news feed, so, i've a difficulty for adapter my ArrayAdapter insert value into ListView, with: "username, name, picture, content and others values" i'm newbie in Android Studio.
Searching the internet I found this function
HashMap<String,String> persons = new HashMap<String,String>();
persons.put(TAG_ID,id);
persons.put(TAG_NAME,name);
persons.put(TAG_ADD,address);
personList.add(persons);
}
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, personList, R.layout.list_item,
new String[]{TAG_ID,TAG_NAME,TAG_ADD},
new int[]{R.id.id, R.id.name, R.id.address}
);
list.setAdapter(adapter);
But, i use ArrayList and ArrayAdapter:
ArrayList<Post> postList =
new JsonConverter<Post>().toArrayList(s, Post.class);
ArrayList<String> postador = new ArrayList<String>();
for(Post value: postList){
postador.add(value.id_postador);
postador.add(value.post_legenda);
if(value.post_foto != null) {
postador.add(value.post_foto);
}
postador.add(value.post_data);
postador.add(value.curtidas);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this,
android.R.layout.simple_list_item_1, postador
);
lvPost.setAdapter(adapter);
I tried it, but no success.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this,
R.layout.list_item, postador, new String[]{TAG_ID,TAG_NAME,TAG_ADD},
new int[]{R.id.id, R.id.name, R.id.address}
);
There a method convert ListAdapter in ArrayAdapter or adapter my ArrayAdapter for ListAdapter?
If you are a newbie to Android, I would like to recommend few things to you.
1). Start Using RecyclerView from the very beginning instead of ListView. It is more advanced, flexible and efficient version of ListView. You can find a sample demo for the RecyclerView here and here
2). If you still want to use ListView. Then you further have two options, either to use BaseAdapter or ArrayAdapter.
a). Base Adapter as the name suggests, is a base class for all the adapters. If you need more flexibility, you can go with it. You can find the demo sample here and here
b). On the other hand, ArrayAdapter is a concrete BaseAdapter that is backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView.If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.To use something other than TextViews (like in your case) for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want. You can find the demo sample here and here.
Hope it will help you in future and also I suppose you will get answer to your question in the specified demo samples. Thanks and have a good day.. :)
I'm trying to build and Android app:
How can I build a list on screen from an array and have the list be clickable?
with a value of the name to be able to use later stored in a String.
What I have now.
String game_list[] = {"game_0","game_2","game_3","game_3","game_4","game_5"};
for(int i=0; i<game_list.length; i++){
myText = myText + game_list[i] +"\n";
}
myTextView.setText("");
myTextView.append(myText);
Also you can use a spinner and adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, game_list); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Here's a super simple example that I have used: http://www.javacodegeeks.com/2013/06/android-listview-tutorial-and-basic-example.html.
You basically need to make a listview object and add it to whatever view you happen to be using. Then you add "extends ListActivity" to the activity and add your list to the listactivity like so:
ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,R.layout.file_row, this.directoryEntries);
ListView fileList = (ListView) findViewById(android.R.id.list);
fileList.setAdapter(directoryList);
Call:
protected void onListItemClick(ListView l, View v, int position, long id)
to associate actions with your listview elements
You can build that with ListView control in android and here very good tutorial about it to get more understand how to build it with the array or from any other data source like from database
http://www.vogella.com/tutorials/AndroidListView/article.html
feed be back in any thing not obvious for you
Can anybody help me? I'm trying to create a ListView in Android, and I'm trying to load items into it using code (not using XML)
Here's the code that I have so far
tweetList = (ListView)this.findViewById(R.id.tweetListView);
TextView tv;
for(int i=0;i<20;i++)
{
tv = new TextView(this);
tv.setText("I'm a textView");
tweetList.addHeaderView(tv);
}
tweetList.invalidate();
What am I doing wrong? The items are not showing in runtime
EDIT: I changed the code as per the answers below and here's the code I have now
tweetList = (ListView)this.findViewById(R.id.tweetListView);
ArrayAdapter<TextView> aa = new ArrayAdapter<TextView>(this, R.id.tweetListView);
tweetList.setAdapter(aa);
TextView tv;
for(int i=0;i<20;i++)
{
tv = new TextView(this);
tv.setText("I'm a textView");
//tweetList.addHeaderView(tv);
aa.add(tv);
}
tweetList.invalidate();
I'm getting an exception now
11-10 01:32:16.002: ERROR/AndroidRuntime(867): android.content.res.Resources$NotFoundException: Resource ID #0x7f050030 type #0x12 is not valid
Why am I not able to add them dynamically now?
You need to use ArrayAdapter and add it to the ListView. Then just add elements to the ArrayAdapter dynamically.
For example:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1);
tweetList.setAdapter(arrayAdapter);
// ...
arrayAdapter.add("New Item");
The header view is just a header, its not the items in the list. Your listview receives its contents through an Adapter. The ListView does not keep Views for its children, but its the adaptor who creates them and fills them with the appropiate data when they need to be shown at the screen. Search for basic tutorials on how to do this, there are lots out there; and remember adapters are the key.
I have following code:
public class ShowActivity extends ListActivity implements OnItemClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.show_list);
ListView lv = getListView();
String[] projection = new String[]{
BaseColumns._ID,
DepotTableMetaData.ITEM_NAME,
DepotTableMetaData.ITEM_AMOUNT,
DepotTableMetaData.ITEM_UNIT,
DepotTableMetaData.ITEM_PPU,
DepotTableMetaData.ITEM_TOTAL,
DepotTableMetaData.ITEM_COMMENT};
Cursor c = managedQuery(ContentProviderMetaData.DepotTableMetaData.CONTENT_URI, projection, null, null, ContentProviderMetaData.DepotTableMetaData.DEFAULT_SORT_ORDER);
String[] columns = new String[]{BaseColumns._ID,ContentProviderMetaData.DepotTableMetaData.ITEM_NAME, ContentProviderMetaData.DepotTableMetaData.ITEM_AMOUNT, ContentProviderMetaData.DepotTableMetaData.ITEM_UNIT, ContentProviderMetaData.DepotTableMetaData.ITEM_PPU, ContentProviderMetaData.DepotTableMetaData.ITEM_TOTAL, ContentProviderMetaData.DepotTableMetaData.ITEM_COMMENT};
int[] to = new int[]{R.id.lname,R.id.lamount,R.id.lunit,R.id.lppu,R.id.ltotal,R.id.lcomment};
Log.d("ShowActivity","Cursor, columns, to set - now setting adapter");
SimpleCursorAdapter simpleadapter = new SimpleCursorAdapter(this,R.layout.list_entry, c, columns, to);
// ListView lv = (ListView)findViewById(R.id.ListView);
this.setListAdapter(simpleadapter);
lv.setOnItemClickListener(this);
// intentCheck();
}
It works fine, but as you might have seen at my comments, i don't want to create the listview via getListView(), i want to do a usual setContentView(R.Layout.---) and populate a Listview, which is defined there. But all tutorials I have seen yet do it like that, and I have no clue how to change that to my wishes.
How does my layout-xml have to look like? how do i "tell him to use it" ?
Thanks in advance.
You can use directly setContentView(R.layout.main); on a ListActivity as long as you define a listView with the android:list id on your xml:
<ListView android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
To set the adapter call directly setListAdapter from your ListActivity
Don't use ListActivity as your base class, extend Activity, set your content to the layout, grab a reference to your expanded ListView (findViewById(...)) and then handle setting the adapter that way.
ListView lv = (ListView)findViewById(android.R.id.list);
No change needed in the .xml