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 :)
Related
I need to get getItemIdAtPosition() from listView to get the id of the records from sqlite database. When i click on the listView on any item, it always shows "null" not showing the id.
this is the code:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
int id= ((int) listView.getItemIdAtPosition(i));
//create our intent, as per usual
Intent intent=new Intent(Tutorial10.this, Activity2.class);
intent.putExtra(ID_EXTRA, id);
startActivity(intent);
}
});
This activity2.java
public class Activity2 extends Activity{
String passedVar=null;
private TextView passedView=null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
//Get our passed variable from our intent's EXTRAS
passedVar=getIntent().getStringExtra(Tutorial10.ID_EXTRA);
//find out text view
passedView=(TextView)findViewById(R.id.passed);
//display our passed variable!!!
passedView.setText("YOU CLICKED ON="+passedVar);
}
}
You should change this line
int id= ((int) listView.getItemIdAtPosition(i));
with this
long id = adapterView.getItemIdAtPosition(i);
I hope this fixes your problem.
According to the Android official document here, there's no definition about the purpose of this function. It seems to call to getItemId() in Adapter, and getItemId() returns the value of Id in SQLite.
I guess you need to use SimpleCursorAdapter to work with the SQLite.
I have a POJO, which describes some model (Item?) and some custom Adapter.
I am setting my adapter for ListView and then, in onItemClick() I want to get value of one of variables which I had added Into the Item.
How I can reach this?
In my code I am doing something like:
private List<SomeItem> items = new ArrayList();
items.add(new SomeItem(firstValueString, secondValueBitmap, thirdValueString));
SomeAdapter adapter = new SomeAdapter(this, R.layout.list_item, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(
...
#Override
public void onItemClick(){
//How to reach for example firstValueString value of currently clicked item??
}
)
Use android.widget.AdapterView.OnItemClickListener:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.d(LOG_TAG, "itemClick: position = " + position + ", id = "
+ id);
SomeItem item = items.get(position); // specific item
}
});
Based on Android SDK documentation:
Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.
If you implement your adapter completely then you can get item like bellow:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Item item = (Item)parent.getItemAtPosition(position);
}
});
So I can see that there are questions about this, but not in the scope of mine. I am buliding an app for Android that has two spinners. The first has an array of choices. However, I am not sure how to affect what choices the second one has based on the first one. I know that you can put in
AdapterView.OnItemSelectedListener
but I'm not sure how to implement this. I've read on this but it's not quite what I'm looking for. I'm also curious as to how I tell the spinner which array to choose, is it in the .xml or in the .java file?
Try this,
firstSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
string selectedValue = arg0.getSelectedItem().toString();
if(selectedValue.equalsIgnoreCase(string1)
{
ArrayAdapter<String> firstAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, firstArray);
secondSpinner.setAdapter(firstAdapter);//
}
else if(selectedValue.equalsIgnoreCase(string2)
{
ArrayAdapter<String> firstAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array2);
secondSpinner.setAdapter(firstAdapter);
}
}
Hope it will help you.
If it's a String array you could define it in XML then use getResource().getStringArray() or declare it in Java.
In your listener for the first spinner, you could do the following to set the choices for the second spinner.
secondSpinnerAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, newArray);
secondSpinner.setAdapter(secondSpinnerAdapter);
Tested and working
update the array list of second spinner in 1st spinner setOnItemSelectedListener
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
string str=spinner1.getSelectedItem().toString();
if(str.equals("spinner item"))//spinner item is selected item of spinner1
{
ArrayAdapter<String>adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, array1);
//adapter1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner2.setAdapter(adapter1);//
}else if{
ArrayAdapter<String>adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array2);
//adapter1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner2.setAdapter(adapter2);
}
}
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;
/....
}
}):
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