Help!!
I have a listadapter with a detail view, the problem that I am having is trying to pass values from an xml file to the second view. I can currently do this but only if I show those values in the first view. What I am trying to acheive is on the firstview have just a title and when you click on that title it takes you to a detail view with more information about it.
here is the code that I currently have.
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_USERADDRESS, KEY_DATEDUE}, new int[] {
R.id.name, R.id.cost });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
//#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
// String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String cost = ((TextView)view.findViewById(R.id.cost)).getText().toString();
// String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
//in.putExtra(KEY_USERADDRESS, name);
in.putExtra(KEY_DATEDUE, cost);
//in.putExtra(KEY_DESC, description);
startActivity(in);
}
});
}
As you can see I have a list adapter,I only want to show the contents of the KEY_USERADDRESS on the first view and when they click it then the rest will be shown. If I add the rest of the Textviews to the listadapter it makes the textviews to big.
Sorry if I am a little confusing, I'm still a rookie when it comes to android development.
Any help will be appreciated!!
Assuming your data is stored in the menuItems Map object, how about something like this in your onItemClick():
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Your code here..
Map<String, String> myValueMap = menuItems.get(position);
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_USERADDRESS, myValueMap.get(KEY_USERADDRESS));
in.putExtra(KEY_DATEDUE, myValueMap.get(KEY_DATEDUE));
in.putExtra(KEY_DESC, myValueMap.get(KEY_DESC));
startActivity(in);
}
the position parameter in the onItemClick can be used to get the selected Map from your menuItems which you used the create your SimpleAdapter to populate the ListView
Related
I tried using getItem() method and in getView() method, problem not solved. How to do it in each item list view and to pass that specific item data to another activity.
Use this:
In XML:
<ListView
android:id="#+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
In Java:
First findViewById:
ListView lv = (ListView) findViewById(R.id.lv);
Then make a String list and use ArrayAdapter for static data if you are using dynamic data then make CustomAdapter:
String[] abc = new String[] {"A","B","C","D","E","F"};
List<String> abc_list = new ArrayList<String>(Arrays.asList(abc));
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, abc_list);
lv.setAdapter(arrayAdapter);
Now for select an item for a particular list item use setOnItemClickListener:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItem =(String)parent.getItemAtPosition(position);
Toast.makeText(this, selectedItem, Toast.LENGTH_SHORT).show();
// For next Activity use Intent
Intent intent = new Intent (this, SendDataActivityName.class);
intent.putExtra("pass_data_id_name", selectedItem);
startActivity(intent);
}
});
For basic things or dynamic data use these links:
https://www.codeproject.com/Tips/1023525/Dynamic-ListView-with-Custom-Adapter-in-Android
http://sampleprogramz.com/android/listview.php
https://www.developerfusion.com/article/145373/android-listviews-with-dynamic-data/
http://wptrafficanalyzer.in/blog/dynamically-add-items-to-listview-in-android/
https://www.survivingwithandroid.com/2012/10/android-listviewarrayadapter-and.html
https://www.android-examples.com/create-listview-in-android-programmatically/
https://www.android-examples.com/add-item-in-listview-in-android-programmatically/
https://androidexample.com/Dynamically_Create_View_Elements__-_Android_Example/index.php?view=article_discription&aid=115
To pass data to other activities:
intent.putExtra("myList",myList);
To recieve data from current activity:
ArrayList<E> myList =(ArrayList<E>)getIntent.getSerializableExtra("myList);
How can I get the item's text from an adapter's listview?
In my adapter I have this code:
final ArrayList> userList = controller.getAllUsers();
if (userList.size() != 0) {
//Set the User Array list in ListView
ListAdapter adapter = new SimpleAdapter(PoliceSmsRegisterReceiver.this, userList, R.layout.view_user_entry_register,
new String[]{"userId", "sender", "fullname", "homeaddress", "emailaddress", "phonenumber", "password", "deviceid"},
new int[]{R.id.userId, R.id.viewSender, R.id.viewFName, R.id.viewHAddress, R.id.viewEAddress, R.id.viewPNumber, R.id.viewPassword, R.id.viewA_ID});
ListView myList = (ListView) findViewById(android.R.id.list);
myList.setAdapter(adapter);
(p.s ; controller is my sqlite db)
then this is my onclick:
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
}
});
What I want to know is how I can toast the specific string in the adapter when clicking the specific item in the list. Example, I want to toast the viewSender.
You can use AdapterView and position provided by setOnItemClickListener to get any information about clicked item :
Toast.maketext(context,(ModelClass)parent.getItemAtPosition(position).getSpecificText(),Toast.LENGTH_SHORT).show();
You can use int position like this.
Toast.maketext(context,String.valueof(position),Toast.LENGTH_SHORT).show();
An OnItemClickListener handles clicking a list item.
You have to use a custom adapter in which you grab hold of all the Views in the row in getView() and set onClickListeners on them there.
I'm saving the images name in the database and these images are saved in the drawable, now i want to assign for each item an image from the database according to the item name, i want to compare it with the database.
In database where the image field and the category type (will be retrieved in the list view)
and here is the code for retrieving the category type into list view:
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.my_profile, c, from, to);
ListView list = (ListView) findViewById(R.id.listView1);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Intent myIntent = new Intent(view.getContext(), ShowWhereToGo.class);
myIntent.putExtra("id", position);
startActivityForResult(myIntent, 0); // display SubView.class }
}
#SuppressWarnings("unused")
public void onItemClick1(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}});
}
Thank you in advance :)
You have to create a custom list row to show images in ListView
you can refer this LINK for better understanding.
Try creating a custom list.You may refer to the following link:
http://w2davids.wordpress.com/android-listview-with-iconsimages-and-sharks-with-lasers/
Hope this will help in resolving your trouble :)
Here's how my app works. User records a sound and then gives it a certain name. In this example I'll be recording two sound files. Test1.mp3 and Test2.mp3. After I'm done recording for the first time, a dialog appears and I type in 'Test1', same goes for the second recording. Test1.mp3 and Test2.mp3 were now added to the listview. This is the code:
//filename is a variable for the name of the file, these lines execute everytime I record a new file
ArrayList<String> fileNames = new ArrayList<String>();
fileNames.add(filename.toString());
listView = (ListView) findViewById (R.id.mainListView);
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, fileNames);
listView.setAdapter(listAdapter);
So, after I've recorded both files and they're added to listview, I want to set onClickListeners to both listview elements. But how do I do it? How do I make it so that everytime a new recorded file has been added to the listview, it also automatically generates the onclick method. This wouldn't be as complicated but every recorded file, ofcourse, has a different path.
The code now:
//LISTVIEW
fileNames.add(filename.toString()); //adding each filename to ArrayList<String>
listView = (ListView) findViewById (R.id.mainListView);
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, fileNames);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast toast = Toast.makeText(getApplicationContext(), filename, Toast.LENGTH_SHORT);
toast.show();
}
});
This is the code that executes each time new file is recorded
You don't have to add a new listener every time you add an element to the list.
You can use a OnItemClickListener set once and for all, and you will be able to find the item that has been clicked by the index in the callback function
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
which is, parameter int position.
Inside onItemClick implementation, you can then retrieve the element which has been clicked by
arg0.getItemAtPosition(position)
Also, you don't have to add the onItemClickListener every time, you can just prepare it once in the onCreate method of your Activity and never change it.
What you will need to do instead, is making a new adapter (or adding a new item to the adapter) when your new file recording has terminated.
I prepared an almost full sample to show how to use the ListView:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<String> fileNames = new ArrayList<String>();
fileNames.add("Test1.mp3");
fileNames.add("Test2.mp3");
final ListView listView = (ListView) findViewById (R.id.mainListView);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fileNames);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Toast toast = Toast.makeText(getApplicationContext(),
(String)arg0.getItemAtPosition(position), Toast.LENGTH_SHORT);
toast.show();
}
});
}
This is enough if the items in the list do not change.
When a recording of your next mp3 file terminates, and you want to add an item to the list, you may just do:
ListView listView = (ListView) findViewById (R.id.mainListView);
fileNames.add("Test3.mp3");
((ArrayAdapter<String>)listView.getAdapter()).notifyDataSetChanged();
Replace "Test3" with your new recorded mp3's filename, and it will show up in the list.
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
switch(position){
case 0:
blablbla
break;
case1 1:
blabla
break;
/....
}
}):
i'm trying to solve something that i'm not sure about. I wants to show a parameter call "decription" in the single item's detail when the single listview item is selected but the "decription" should not be displayed on the single listview item. I know where the problem lies but i'm not sure how do i solve it. Please kindly give me your suggestions! Here is my code that the mistake lies:
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item, new String[] { KEY_TITLE, KEY_DESC, KEY_COUNTRY_NAME,
KEY_VENUE_NAME, KEY_START_TIME }, new int[] {
R.id.title,R.id.description, R.id.countryName, R.id.venueName,
R.id.startTime });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String title = ((TextView) view.findViewById(R.id.title))
.getText().toString();
String description = ((TextView) view
.findViewById(R.id.description)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
SingleMenuItemActivity.class);
in.putExtra(KEY_TITLE, title);
in.putExtra(KEY_DESC, description);
startActivity(in);
}
As you can see, the KEY_DESC is the one i'm talking about, but in order for me to use the in.putextra(KEY_DESC, description); for me to use the in.getStringExtra from another class. But the description gets from the R.id.description field which its declared in the ListAdapter, which is also displayed in the listview items. I wants to remove from the description from the listview items but still be able to use the in.putExtra(KEY_DESC, ??) method. May i know what should i do?
If you don't want the description to be shown in the main list, just remove the UI element that represents the description from your res/layout/list-item.xml (it probably would be TextView with id=#description).
In that case you won't see the description, but will still be available throught the SimpleAdapter.