ListView and hidden Id. How it is possible? - android

I'm developing an application for android and now I have implemented a ListView that shows a list of courses, connected to a database.
I would like to know how to include, with the name, an hidden id (that come from the db) so that once the user click on the elements the app goes to the relative view of the selected courses.
And how can I maintain the id during the navigation inside the course-view?
At the moment my code just load the name of the courses from the db and set in the list view:
ArrayAdapter<String> result = new ArrayAdapter<String>(CourseActivity.this, android.R.layout.simple_list_item_1);
for (CourseRecord c : get())
result.add(c.getFullname());
lv.setAdapter(result);
obviously I'm able to do also c.getid() but I don't where to put the id.
Thank you very much.
P.S.: Maybe does someone have also a really nice graphics of list view?

change your array adapter like this.
private ArrayAdapter<String> result = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1){
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
v.setTag(getMyIdForPosition(position));
return convertView;
}
};
and have an item click handler to recieve the selected ids
private OnItemClickListener itemClickedHandler = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id)
{
String myId = (String)v.getTag();
doYourStuff(myId);
}
};
assign the listener to the list
myList= (ListView)findViewById(R.id.history);
myList.setOnItemClickListener(itemClickedHandler);

You can store the id in a hidden TextView. In the list item XML, add 'android:visibility="gone"' to the TextView. Likewise in the click handler you can read the id from the textview.

You can also store id using setTag(Object object) method of a View. Use getTag() method to extract that id from that view.

Related

Android: Determine which View clicked within ListView row

I have a ListView with lots of icons which may be clicked to change the state of their respective row items. I realise that I can create onClick handlers for all of them but I would like to a generic means of identifying which icon (View) has been clicked. (e.g. determine View at touch coords x,y?
Any idea how I can do that?
I am getting row clicks with the below handler:
lvClickListener = new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick( AdapterView<?> arg0, View arg1, int position, long arg3 )
...
On each row I have 5 ImageViews.
I assume all rows are being inflated with the same layout and you are using the holder pattern (http://developer.android.com/training/improving-layouts/smooth-scrolling.html).
This way, you can have a single OnClickListener to "listen" for any of your ImageView (or whatever they are). You only need to set the listener in the momento you actually inflate the view (not when you reuse it), and then set for all ImageView in the same row the row position as the Tag, so, then in the onClick method, you can chech to which row they belong to (by the tag) and which ImageView is it (by the id)
class MyAdapter extends BaseAdapter implements OnClickListener
[...]
#Override
public View getView (int position, View convertView, ViewGroup parent)
{
MyHolder holder = null;
if(convertView != null)
{
holder = new MyHolder();
convertView = inflater.inflate(...)
holder.imageView1 = (ImageView)convertView.findViewById(R.id.[...] )
holder.imageView1.setOnClickListener(this);
holder.imageView2 = (ImageView)convertView.findViewById(R.id.[...] )
holder.imageView2.setOnClickListener(this);
[...]
convertView.setTag(holder);
}
else
{
holder = (HyHolder)convertView.getTag();
[...]
}
holder.imageView1.setTag(position);
holder.imageView2.setTag(position);
[...]
return convertView;
}
#Override
public void onClick(View v)
{
int id = v.getId();
Integer position = (Integer)v.getTag();
[...]
}
Of course you can then optimize this, put the views in an array or whatever or put the OnClickListener outside the Adapter (in the activity, or an instance variable), but this is the basic idea.
As a note, you should check what happens with the onItemClickListener, I'm not pretty sure, but it may cause problems intercepting touches (or it may no, I don't know), but if you're not going to use the whole row click, then you can remove it
You can do this in multiple ways, You can tag each ImageView to your AsyncTask(or whatever) you use. Or, refer https://github.com/square/picasso

How to Handle Android ListView Subitems Click Events using SimpleAdapter?

I have been developing an android application and i am stuck in the middle.
The problem is i am using SimpleAdapter to do the adapter stuff and show items in the Listview and as far as i know i cannot override the getView() method of SimpleAdapter class to bound click listeners to the items.
There is a other way to handle click events of sub items like using XML, you can write in the XML like android:clickable="true"and android:onClick="clicklistenr", using this i can get the item but my problem is if i use this then i cannot get the position of the adapter which i need to get adapter item values and handle other tasks. So i am stuck here any help would be appreciable. thanks.
For example i have a ListView which contains one image, TextView, like Button, share Button in each of its items. And there is no way i can find that either its image or button clicked using setOnItemClickListener. So i need a way to handle click events of these sub items of a ListView, i am using SimpleAdapter.
Just call listView.setOnItemClickListener() with your implementation of the listener.
and use like
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
Where list=(ListView)findViewById(R.id.list); and list.setAdapter(your_adapter);
For More details Follow: http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
Hope It Will Help You.. :)
You can use like
myListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String, Object> obj = (HashMap<String, Object>) adapter.getItem(position);
String result= (String) obj.get("name");
Log.d("Yourtag", name);
}
});
First of all your problem is how to handle the items of a custom listview.. not sub. Anyways...
If you are using SimpleAdapter , you may use getView().But if you are using SimpleAdapter you don't need to use the getView() as the it handles the mapping of data to your layout via the resource. For inforamtion check the SimpleAdapter in developer site.
Another thing is there is not mandatory for using any particular adapter. You can create any adapter class which will extends the BaseAdapter which will implement the getView().
Inside getView() you can able to inflate your custom layout(which contains the image,button etc..) to your view or convertview.something like:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.yourcustomlayout, null);
//Initialize your custom views here
TextView someText= (TextView) vi.findViewById(R.id.tvSometext);
Button likeButton = (Button ) vi.findViewById(R.id.btnLike);
//put data or call onclicklistener or
//whatever you want to do with custom views
likeButton .setOnClickListener(...){
//on click
}
}
Ans simply call this through your constructor with some data and appropriate context.
Amir,
I am not sure you found the solution or not. You can do this way:
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
//for each subitem, set click listener & set tag having position as
// value.
ImageView imv = (ImageView)view.findViewById(R.id.live_view);
imv.setTag(i);
imv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("INFO","Live view clicked");
//This will give you position in listview.
int position = (int) v.getTag();
}
});
}
I hope it will help you and others looking to find relevant answer.
May be there is some other solution too, but it will surely work.

Get the position of Button in List View

I have one problem.
I want to get the position of Button in List View.
In below image when I clicked in Edit or Delete button i just want its position in List View.
How it possible?
This is my Office Management Screen. All the data getting in ListView run-time from my web-service.
I get the data in List View by using this code-
ListAdapter adapter = new SimpleAdapter(Office_Mgmt.this, officeList, R.layout.office_mgmt_list_model, new String[] { "office_name" }, new int[] { R.id.label});
setListAdapter(adapter);
You can create your custom adapter, passing the context of your app as argument to set the click:
listView.setAdapter(new PesquisaAdapter(this, anunciantescidades, this);
Then, in the constructor of your adapter you will have a OnClickListener to recieve the argument this you passed:
public PesquisaAdapter(Context context, List<Anunciante> anunciantes, OnClickListener onClick1)
In the getView method of the adapter, you can set onClickListener of the button:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.your_custom_layout, null);
Button btn = (Button) v.findViewById(R.id.yourbutton);
btn.setOnClickListener(onClick1);
}
In your java, you can implements onClickListener, and then do what you want with your button:
#Override
public void onClick(View v) {
if(yourbutton.getId() == v.getId()){
final int position = listView.getPositionForView((LinearLayout)v.getParent());
}
}
So, you will have the position in the list that you clicked, and you can manage the click of the edit and delete separately.
Hope it helps!
implement onItemClick(AdapterView<?> parent, View view, int position, long id) for the listview.
Position and view can be identified from the handler. You can set additional info using setTag while creating the view inside the Adapter's getView method and retrieve in the handler and then verify in the handler if required.

In Android, How can I modify a text in GridView after clicking an item?

I'm trying to put a text above the Image in grid view but i'm not able to do this.
My app will have a gridview with pictures, and when I click over the each picture, I have put the quantity of clicks above of this image in real time.
I create an Adapter and My images are appearing on my Activity.
How I can do this, on Android?
You can do it through the onItemClickListener
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
// Create the view here
Holder tag = new Holder();
tag.textView = (TextView) view.findViewById(textViewId);
view.setTag(tag);
}
if (counts[position] == 0) {
tag.textView.setVisibility(View.INVISIBLE);
else {
tag.textView.setVisibility(View.VISIBLE);
tag.textView.setText("" + counts[position]);
}
...
}
gridView.setAdapter(adapter);
int[] counts = new int[// item count];
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Holder tag = (Holder) view.getTag();
if (tag != null) {
counts[position] = counts[position] + 1;
tag.textView.setText("" + counts[position]);
}
}
}
You need to track the count outside as a member variable.
In your adapter (or wherever appropriate to store this data depending on how long lived this click data should be), add some sort of data structure to maintain the number of clicks per position. In your adapter's getView(), you should set the text at that position according to the number of clicks you have stored in your data structure.
In your list item click listener, increment the click count in your data structure, and call notifyDatasetChanged() on your Adapter.

Populate ListView and their detail views with from multiple sqlite tables

currently I'm searching for a solution to populate a ListView with content from 3 different SQLite tables. When tapped, each ListView item (row) should show the according detailed view (Activity), which is based on one of the three xml layouts.
Any suggestions?
Edit:
Currently I solved this for one type of Object/SQLite table.
Fetching content from SQLite table into an ArrayList and pass it via putExtra to the detail activity, which handles setting texts/content.
The matching of the listView entry and starting the according detail view is solved with the arrayList index with matches to the listView index.
I'm not sure, if this is a proper solution. I guess it isn't...
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent typeOne_details_intent = new Intent(this, TypeOneDetailActivity.class);
typeOne_result = new TypeOneResult();
typeOne_result = typeOneResult_arr.get(position);
typeOne_details_intent.putExtra("result", TypeOneResult);
startActivity(typeOne_details_intent);
}
How can I solve this to add typeTwoDetails and get a better matching between listview and according detailview?
You can use a Custom Adapter that that can determine what Activity to start, set custom row view, etc. One way, you could create a row object Interface, and check the Object type to determine the layout for the row and set things such as the onClick listener and which Activity to launch.
Quick example below:
public class CustomAdapter extends BaseAdapter<ListObjectInterface>{
...
ListObjectInterface data[];
....
public CustomAdapter(Context context, int layoutResourceId, ListObjectInterface[] data) {
...
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
... /* do things. such as check if instance is present, etc. */
ListObjectInterface object = data[position];
if (object instanceOf TypeA) {
row = inflater.inflate(layoutResourceIdA, parent, false);
...
... /* get View objects and set Button listener to launch Activity A */
} else if (object instanceOf TypeB) {
row = inflater.inflate(layoutResourceIdB, parent, false);
...
... /* get View objects and set Button listener to launch Activity B */
} ... /* etc */
... /* more logic */
return row;
}
}

Categories

Resources