i have a listView that each row of that shows a text. each row fetched from database that has 2 fields:
1- id
2-text
now, in my list i have to show only my text not id. but i need when i click on a list row fetch each rows id that related to his text. in fact i fetch text,id pair from db and show the text and use id in on click event. but how i can do that? how i related id to each row?
Set id as a tag to each view inside the listView and in onClick retrieve it
In your Adapter, set the tag of the TextView.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View listView = null;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if( convertView == null){
listView = inflater.inflate(R.layout.list_item, null);
}
else{
listView = convertView;
}
TextView tv = (TextView) listView.findview(R.id.textview);
tv.setText("my-text");
tv.setTag("my-id");
return listView;
}
Retrieve the id :
// Main Activity
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
TextView tv = (TextView)v.findViewById(R.id.textview);
String id = tv.getTag().toString();
}
Related
Hello guys I want to check through my GridView and see if any of the checkBox is checked or not. Eventually, I want to delete the rows in my database and update my custom adapter but I'm lost on what function to use or how to approach it. I know to use some kind of while/for loop but which data type can surf and iterate each list items? I was looking around and .getChildAt(index) seems like something that would be useful but it doesn't work/appear for my custom adapter variable in my MainActivity.
Here is my Custom Adapter class for reference
//variable responsible for making checkbox visible or not
private boolean displayCheckBox;
//constructor - it takes the context and the list of words
WordAdapter(Context context, ArrayList<WordFolder> word){
super(context, 0, word);
}
//sets the visibility of the checkBox
public void setCheckBoxVisibility(boolean visible){
this.displayCheckBox = visible;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View listItemView = convertView;
if(listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.folder_view, parent, false);
}
//getting the checkBox view id
CheckBox checkBox = (CheckBox) listItemView.findViewById(R.id.check_box);
checkBox.setVisibility(displayCheckBox ? View.VISIBLE : View.GONE);
//Getting the current word
WordFolder currentWord = getItem(position);
//making the 3 text view to match our word_folder.xml
TextView date_created = (TextView) listItemView.findViewById(R.id.date_created);
TextView title = (TextView) listItemView.findViewById(R.id.title);
TextView desc = (TextView) listItemView.findViewById(R.id.desc);
//using the setText to get the text and set it in the textView
date_created.setText(currentWord.getDateCreated());
title.setText(currentWord.getTitle());
desc.setText(currentWord.getTitleDesc());
return listItemView;
}
}
I have written a small app that has a ListView with a custom adapter. Each row contains some Buttons, which will change background color when clicked, and I got the list items to be clickable as well by putting
android:descendantFocusability="blocksDescendants"
in the xml of the list items. But now I have this weird bug where clicking on the list item reverts all clicked Buttons back to their original colorless state. How can I get the Buttons to keep their color?
Details:
Part of the custom adapter:
View.OnClickListener onButtonClicked = new View.OnClickListener() {
#Override
public void onClick(View button) {
View listItem = (View) button.getParent();
final long DBid = (long) listItem.getTag();//database ID
final Button b = (Button) button;
sqldataDataSource datasource = new sqldataDataSource(context);
datasource.open();
datasource.updateButton(DBid);
datasource.close();
b.setBackgroundColor(0xFF386F00);
}
};
As you can see, I change the background color AND change the database entry, so when the whole list is reloaded, the Button keeps its color (another part of my custom adapter):
public View getView(int i, View convertView, ViewGroup parent) {
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.hrlistitems, parent, false);
Button b = (Button) rowView.findViewById(R.id.HRlistB);
b.setOnClickListener(onButtonClicked);
if(!(values.get(i).getB().equals(""))){
b.setBackgroundColor(0xFF386F00);
}
return rowView;
}
This works fine when going to another activity and coming back to this one. The buttons are created colored as expected.
So my guess was that the list is recreated from the original listItem array when an item is clicked, which is why I tried to fix this by reloading my database, like so (from my activity):
#Override
protected void onStart() {
super.onStart();
datasource = new sqldataDataSource(this);
datasource.open();
listItems = datasource.getOnlyRoutes(id);//this works fine
Collections.sort(listItems, HallenRoute.vergleichen());
if (mListView == null) {
mListView = (ListView) findViewById(R.id.listViewHalle);
}
adapter=new customAdapter(this, listItems);
setListAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int pos, long nid) {
listItems.get(pos).increaseCount();
datasource.updateCountHR(listItems.get(pos));
listItems = datasource.getOnlyRoutes(id);//fix I tried, doesn't work
Collections.sort(listItems, HallenRoute.vergleichen());
adapter.notifyDataSetChanged();
}
});
}
But this doesn't work.
How can I get the ListView to either not reload on ItemClick or reload properly (i.e. from database)?
You don't have to reload the whole data for every Button click.
In your Button click you're just updating the data base and not your adapter dataset values, this is why you always get the old background color.
public View getView(int i, View convertView, ViewGroup parent) {
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.hrlistitems, parent, false);
Button b = (Button) rowView.findViewById(R.id.HRlistB);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View button) {
View listItem = (View) button.getParent();
final long DBid = (long) listItem.getTag();//database ID
final Button b = (Button) button;
sqldataDataSource datasource = new sqldataDataSource(context);
datasource.open();
datasource.updateButton(DBid);
datasource.close();
//b.setBackgroundColor(0xFF386F00); no need for this line, getView() method will take care of the background
//update your adapter dataset, eg: values.get(i).setB("newColor");
notifyDataSetChanged(); // to refresh your adapter
}
});
if(!(values.get(i).getB().equals(""))){
b.setBackgroundColor(0xFF386F00);
}
return rowView;
}
PS: It's better if you save your "database ID" in your Model object not as a View tag.
i'm trying to set textView in custom row view of listView like below but the app crashes returning null pointer exception
TextView txtrow = (TextView) getListView().getChildAt(i).findViewById(R.id.text11);
txtrow.setText("text");
but when i put it under setOnClickListener it works
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView txtrow = (TextView) getListView().getChildAt(i).findViewById(R.id.text11);
txtrow.setText("text");
}
});
You should be doing this inside your ListView's adapter. Whatever data you are using the back your adapter should contain the text you want so that you can set it on the appropriate TextView when you actually create the row view in getView().
It's important to note that getChildAt(index) returns the child at the given index from the ListView's current children. In other words, getChildAt(0) does not necessarily give you the list item at position 0. The user may have scrolled the list so that it now shows data from positions 10 through 15 (for example), in which case getChildAt(0) gives you the view for position 10, not position 0.
To set text in custom row in listview
Please follow the following points
1. custom arrayadapter by extending Arrayadpater (for simple)
2. and overwrite getview as following
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(values[position]);
return rowView;
}
3. and set the adapter to the listview
And for more detial please refer this site
[http://www.vogella.com/tutorials/AndroidListView/article.html][1]
When I retrieve data from database, I have for each row unique ID I want match id with string in listview but ID I want it to be invisible, so that when i click any item in listview trans me to another activity have data about this item.
That means I have Two table in database match together I want to retrieve one as listview , when click item trans me to data match the item i have been clicked.one as listview , when click item trans me to data match the item i have been clicked.
How can I do this?
this should work
myAdapter.setViewBinder(new MyViewBinder());
public class MyViewBinder implements ViewBinder {
#Override
public boolean setViewValue(View view, Object data, String text){
//Since it iterates through all the views of the item, change accordingly
if(view instanceof TextView){
((TextView)view).setTag("whatever you want");
}
}
}
You can use custom adapter and use like this method to achive yor goal
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if(vi == null){
vi = inflater.inflate(R.layout.row_layout, null);
}
final int id = idGetFuncttion(position);
vi.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
doSomethingWithID(id);
}
});
return vi;
}
1.set your unique id to each row by setTag property and retrieve it by getTag property
2.use custom adapter and custom listview layout for the listview and set your unique id to invisible textview in custom listview layout through custom adapter
I got the following App with Costume adapter showing 2 images and 1 textview for each listview.
I can push/click/press each ListView just fine but i want to being able to recognize the X press aswell and i seem to not being able to get the view name or resource name.
My mainclass with the setOnItemclickListener looks like this
serverListView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
//recieve and check if X image is pressed
}
});
This is the getView method in my Adapter. I tried to recognize the different image clicks in here without any different results.
#Override
public View getView(final int position,final View convertView,final ViewGroup parent) {
View row = convertView;
ServerHolder holder = null;
if(row == null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ServerHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.thumbImage);
holder.txtTitle = (TextView)row.findViewById(R.id.name);
holder.removeIcon = (ImageView)row.findViewById(R.id.removeServer);
row.setTag(holder);
}
else{
holder = (ServerHolder)row.getTag();
}
RowServer rs = servers.get(position);
holder.imgIcon.setImageResource(rs.getIcon());
holder.txtTitle.setText(rs.getName());
return row;
}
You probably have to add listeners to the subitems in getView(...). Try setOnClickListener(...) for the views you want to have events attached.