i've tried everything but nothing have worked.
I have a listView like this:
[ checkbox ] | [textview] | [button]
What i'm trying to do is change the text of [button] only in the row witch [ checkbox ] is checked.
Here is the problematic block of code:
public View getView(int position, View convertView, ViewGroup parent){
final ViewHolder vh;
final View conv;
if(convertView == null){
LayoutInflater vi=(LayoutInflater)pContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=vi.inflate(R.layout.rest_dishes_item, null);
conv=convertView;
final Button b[];
b=new Button[itens.size()];
vh=new ViewHolder();
vh.txt=(TextView)convertView.findViewById(R.id.dish_name);
vh.checkBox=(CheckBox) convertView.findViewById(R.id.dish_item);
vh.qnt=(Button) convertView.findViewById(R.id.qnt);
vh.quantidade=new Quantidade[itens.size()];
for(int i=0;i<itens.size();i++){
b[i]=(Button)conv.findViewById(R.id.qnt);
vh.quantidade[i].quantidade=1;
vh.quantidade[i].order=i;
}
vh.qnt.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
for(int i=0;i<itens.size();i++){
Titem item=itens.get(i);
if(item.isCheck()){
vh.quantidade[i].quantidade++;
//Log.d("teste",i+""+vh.quantidade[i].quantidade);
b[i].setText(String.valueOf(vh.quantidade[i].quantidade));
}
else if(!item.isCheck()){
Log.d("teste",i+"1");
b[i].setText(String.valueOf(1));
}
}
}
});
//(Button) convertView.findViewById(R.id.qnt);
convertView.setTag(vh);
vh.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
CheckBox box=(CheckBox) arg0;
Titem item=(Titem)box.getTag();
item.setCheck(box.isChecked());
}
});
}else{
vh=(ViewHolder)convertView.getTag();
}
Titem item=itemList.get(position);
vh.txt.setText(item.getName());
vh.checkBox.setText(item.getName());
vh.checkBox.setChecked(item.isCheck());
vh.checkBox.setTag(item);
//Log.d("teste","chegou aqui");
return convertView;
}
With this code the text of the buttons doesn't change at all, no matter which button i click his text still "1".
Without the line:
b[i].setText(String.valueOf(1));
The button of a checked row has his text changed like i want. But When i click a button of a non checked row, the clicked button has text changed.
Maybe i'm doing something stupid or just missing something in the algorithm, at the moment i'm stuck.
Please help.
Thanks.
Sounds like you are confused, getView() is executed once per visible item. This is your code:
vh.qnt=(Button) convertView.findViewById(R.id.qnt);
vh.quantidade=new Quantidade[itens.size()];
for(int i=0;i<itens.size();i++){
b[i]=(Button)conv.findViewById(R.id.qnt);
vh.quantidade[i].quantidade=1;
vh.quantidade[i].order=i;
}
vh.qnt and all instances of b[i] reference the exact same Button (R.id.qnt) in the row that is being rendered.
You also need to update the text of the button after the if/else, when update the rest of the vh views.
I did it!!
I've used setTag() and getTag(), that allow me to use the same id (R.id.qnt) for different buttons. Here is the code:
int aux=0; //before onCreate()
.
.
.
vh.qnt.setTag(aux);//set a tag for each button in row
aux++; //this will be the number of rows
vh.qnt.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
int index=(Integer)vh.qnt.getTag();
Titem item=itens.get(index);
if(item.isCheck()){
vh.qnt.setText(String.valueOf(vh.quantidade[index]++));
}
}
});
Thanks for the hint #dmon, you opened my eyes for the " same id " fact.
Related
Hello every one i have a listview of array data of checkboxes which the user will select as the hobbies below is preview
and the hobbies will be shown upside but i am stuck of removing the hobbies by clicking it on up side and also when i select the 2 hobies then in show list it shows 2 times
bellow is the preview
also if i select the 3rd hobby then it show 3 times so please fix this for me and also if i uncheck the box it should remove from the view list also if i click on any hobby in the show list it still remove and uncheck automatically and if i click on save button all the hobbies should be selected
please fix this for me bellow is my code
Hobbies_Hobbies_List.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (!Hobbies_Hobbies_List.isItemChecked(i))
{
Hobbies_Hobbies_List.setItemChecked(i,false);
}
else
{
Object object = Hobbies_Hobbies_List.getItemAtPosition(i);
String Hobby = String.valueOf(object);
for (int j=0;j<Hobbies_Hobbies_List.getCount();j++)
{
if (Hobbies_Hobbies_List.isItemChecked(j))
{
TextView text = new TextView(HobbiesActivity.this);
text.setText(Hobby+" ");
text.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Hobbies_Show.addView(text);
}
}
}
}
});
hobbies_save_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
It seems you don't have an adapter.
In an adapter you can modify your list (add or remove item, change text, color etc)
If you want to make your own list you will have to make your own adapter for your list. You can remove items in the adapter.
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View rowView = inflater.inflate(resource, parent, false);
TextView textView = rowView.findViewById(R.id.textView);
CheckBox checkBox = rowView.findViewById(R.id.checkBox);
textView.setText(dataSet.get(position).myText);
checkBox.setChecked(dataSet.get(position).myBool);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
// You can remove item here
}
});
return rowView;
}
For this you need your own data type.
Here a project with an example:
https://github.com/Asuki/SimpleListView
You also have to add the remove items to the other one list (maybe from the adapter)
I have a litview in my code and into each item there is an edittext with a button , So what i want to do is get the edittext's value when press on the button of an listview's item.
I'v tried to get the value by declaring it like this , but it returns null value !
public View getView(int i, View view, ViewGroup viewGroup){
LayoutInflater linflater = getLayoutInflater();
View view1 = linflater.inflate(R.layout.row_view, null);
final EditText replyfld = (EditText) view1.findViewById(R.id.replyfld);
final Button sendreplybtn = (Button)view1.findViewById(R.id.sendreplybtn);
sendreplybtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String thereplyvalue = replyfld.getText().toString();
Toast.makeText(MessagesActivity.this, thereplyvalue, Toast.LENGTH_SHORT).show();
}
});
}
What to do please ?
From the code at pastebin.com/HejVn4bb .
Looks like calling replytogglebtn.setFavorite(false,true); (Line:101) in onClickListener before getting text is the problem because setOnFavoriteChangeListener() clears the edittext.
just get text before the setFavorite(false,true);
You have to do it inside your Adapter. Can you past the code of it?
I have a Horizontal Listview with a textview. When I click on a textview in the view, that particular textview gets a border.
public View getView(int position, View convertView, ViewGroup parent) {
View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.minute_listview, null);
Typeface afBold = Typeface.createFromAsset(getAssets(), "fonts/AftenScreen-Bold.ttf");
minuteText = (TextView) retval.findViewById(R.id.title);
Button button = (Button) retval.findViewById(R.id.clickbutton);
button.setOnClickListener(mOnButtonClicked);
minuteText.setText(dataObjects[position]);
minuteText.setTypeface(afBold);
//THIS IS WHERE THE BORDER GETS SET
minuteText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.border);
}
});
return retval;
}
Now, how can I remove this border and set it on the next textview thats clicked?
Use this it will give you a white background or a translucent one, in other words it has been removed
WhatEverView.setBackground(new ColorDrawable(Color.TRANSPARENT));
okay full code
int pos -1;// default is -1, which means no one has altered it
// replicate this onclick listener logic
minuteText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(pos != -1){ // it is not -1 that means some1 has altered it
parentView.findViewById(pos).
setBackground(new ColorDrawable(Color.TRANSPARENT));
// the above line searched for the view and changed the background
}
pos = v.getId(); // the id of the new view, keep doing it
v.setBackgroundResource(R.drawable.border);
}
});
so use this for all onclick listeners you want that effect on
Does it fit your requirement?
add this
TextView ClicledTv ;//to save the clicked tv id
then
minuteText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(Clickedtv!=null)
clickedtv.setBackground(R.drawable.anotherOne);
v.setBackgroundResource(R.drawable.border);
clickedTv=minuteText;
}
});
In my android app, I have a activity that contain a listview. And within the each row of the listview, i have another listview let called it lv. Inside this lv, it contain some textview. My question is how can i get the text from the listview (A)?
I know that if there is only one listview and in order to get the text of each row when i click on a button, i can use the following code
textview.getText().toString();
However, when i try with this code to get the text in lv, it always give me the last value (n row) in lv. I am not able to get the text of 1st row to n-1 row of lv.
UPDATE:
The follow are the code where i want to get the text, the ordered[] is passed from a custom adapter to another custom adapter (which is the following code)
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=((Activity) context).getLayoutInflater();
View rowView=inflater.inflate(layoutResourceId, null,true);
txt1 = (TextView)rowView.findViewById(R.id.textView1);
txt2 = (TextView)rowView.findViewById(R.id.textView2);
txt3 = (TextView)rowView.findViewById(R.id.textView3);
txt4 = (TextView)rowView.findViewById(R.id.textView4);
txt5 = (TextView)rowView.findViewById(R.id.textView5);
txt6 = (TextView)rowView.findViewById(R.id.textView6);
txtoid = (TextView)rowView.findViewById(R.id.textView7);
final Button btnGet = (Button)rowView.findViewById(R.id.btnGet);
tmp = position;
String tmp2 = String.valueOf(ordered[position]);
txtoid.setText(tmp2);
btnGet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(btnconfirm.getText().toString().equals("Get")){
String tmp1 = txtq.getText().toString();
System.out.println("tmp1: "+tmp1);
}
}
});
....
Anyone help will be nice!
Thanks
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View rootView) {
// TODO Auto-generated method stub
TextView myText = (TextView)rootView.findViewById(R.id.textView7);
Log.d("","myText:" + myText.getText()); //you can get each row values.
}
});
Buttons added in GridView programmatically the total number of buttons depend on word length. Each button contains a letter of word, i want to get that letter when click the button using tag,
how can i get?
here is the code:
public View getView(final int position, View convertView, ViewGroup arg2) {
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.buttonlist, null);
}
final Button btn= (Button)v.findViewById(R.id.letterbtn);
btn.setText(word[position]+"");
btn.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
btn.setTag(btn);
Object tag=btn.getTag();
btn.setVisibility(View.GONE);
function(position);
}
});
use
String tag=btn.getTag().toString();
or
String tag=(String)btn.getTag();
instead of
Object tag=btn.getTag();
hope it will help you. And basically you can use getText() method instead of tag which returns text from view.