how to get textview text - java

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.
}
});

Related

Add item to a specific column in ListView Adapter

I have an increment system and I want when onClick to save value from DialogAlert in a column in ListView Adapter.
Here is my Adapter:
public class VanzatorProduseList extends ArrayAdapter<VanzatorProduse> {
private Activity context;
private SparseBooleanArray selectedListItemsIds;
private List<VanzatorProduse> vanzatorProduseList;
public VanzatorProduseList(Activity context, List<VanzatorProduse> vanzatorProduseList){
super(context, R.layout.list_produse_vanzator, vanzatorProduseList);
this.context = context;
selectedListItemsIds = new SparseBooleanArray();
this.vanzatorProduseList = vanzatorProduseList;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.list_produse_vanzator, null, true);
TextView textViewProdus1 = (TextView) listViewItem.findViewById(R.id.textViewProdus1);
TextView textViewPret1 = (TextView) listViewItem.findViewById(R.id.textViewPret1);
final TextView textViewCantitate1 = (TextView) listViewItem.findViewById(R.id.textViewCantitate1);
final VanzatorProduse vanzatorProduse = vanzatorProduseList.get(position);
textViewProdus1.setText(vanzatorProduse.getProdus());
textViewPret1.setText(vanzatorProduse.getPret());
textViewCantitate1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_alert);
dialog.setTitle("Alege Canitatea");
// set the custom dialog components - text, image and button
final Button dialogBtn = (Button) dialog.findViewById(R.id.dialog_btn);
final ElegantNumberButton elegantNumberButton = (ElegantNumberButton) dialog.findViewById(R.id.elegantNumberButton);
// if button is clicked, close the custom dialog
dialogBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String num = elegantNumberButton.getNumber();
vanzatorProduseList.add(num);
dialog.dismiss();
}
});
dialog.show();
}
});
return listViewItem;
}
Here is what I tried but I got an error and no idea what to do next :
String num = elegantNumberButton.getNumber();
vanzatorProduseList.add(num);
Error:
'add(com.dev.darius.neglije.Vanzator.ListaProduse.VanzatorProduse)' in 'java.util.List' cannot be applied to '(java.lang.String)'
Here I'm stuck I want to display in TextView called textViewCantitate1.
Hope you understand.
As stated in earlier answers by #jibrahim:
You are trying to add String type into List vanzatorProduseList. You can add only VanzatorProduse type into vanzatorProduseList list. So, the cause of the problem is:
String num = elegantNumberButton.getNumber();
vanzatorProduseList.add(num);
You need to do something like this Create Object of your Custom class and assign value to that:
String num = elegantNumberButton.getNumber();
VanzatorProduse numVanProObject = new VanzatorProduse();
numVanProObject .setNumber(num );
vanzatorProduseList.add(numVanProObject );
But your custom call should have method to cater this type issue
Edit: Do not forget to call notifyDataSetChanged() method to refresh the list
VanzatorProduse producse = new VanzatorProduse();
producse.setProdus("add value here")
vanzatorProduseList.add(producse);
You are trying to add String type into List<VanzatorProduse> vanzatorProduseList. You can add only VanzatorProduse type into vanzatorProduseList list. So, the cause of the problem is:
String num = elegantNumberButton.getNumber();
vanzatorProduseList.add(num);
In your code you have following lines:
final VanzatorProduse vanzatorProduse = vanzatorProduseList.get(position);
textViewProdus1.setText(vanzatorProduse.getProdus());
textViewPret1.setText(vanzatorProduse.getPret());
So, might be you add num value into vanzatorProduse.setProdus(num) or any other appropriate field and then add it into the list:
vanzatorProduseList.add(vanzatorProduse);

Get an Edittext value of an listview when click on it's item

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?

How to modify a single list item after Asynctask is done in a BaseAdapter to

In my main activity I display a ListView which uses a custom BaseAdapter (ThoughtListAdapter).
listView = (ListView) findViewById(R.id.list);
adapter = new ThoughtListAdapter(this, resultingThoughts);
listView.setAdapter(adapter);
Every item in the ListView has a custom layout containing a TextView and two Button.
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_item_thought, null);
}
thoughtText = (TextView) convertView.findViewById(R.id.thought_text_view);
likeButton = (Button) convertView.findViewById(R.id.thought_like_button);
dislikeButton = (Button) convertView.findViewById(R.id.thought_dislike_button);
When a Button is clicked an AsyncTask (AsyncPost) is called which connects to my database and makes some changes.
likeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
System.out.println("LIKE CLICKED");
Thought t = thoughtItems.get(position);
thoughtId = t.getId();
opinion = 1;
AsyncPost asyncPost = new AsyncPost(activity,ThoughtListAdapter.this);
asyncPost.execute(SHARE_THOUGHT_URL,
TAG_PERSON_EMAIL, "m#b.it",
TAG_THOUGHT_ID, thoughtId.toString(),
TAG_OPINION, opinion.toString());
}
});
What I need is making both Button-s of a list item disappear after the AsyncTask is done with a successful outcome. I have a method onComplete(JSONObject json) which elaborates the JSONObject returned by the AsyncTask. I try to make the buttons non visible inside the onComplete method, but this doesn't work because onComplete() doesn't know which exact button has been clicked.
How can I pass an instance of the exact clicked button inside onComplete() and make disappear only the Like and Dislike buttons of the concerned list item?
AsyncPost is a global AsyncTask used by all my other activities. I would strongly prefer to leave it alone. The onComplete() method functions as the onPostExecute() method of the AsyncTask.
Here are the getView() and onComplete() methods inside my BaseAdapter, which contain all the code shown above.
Thank you.
public View getView(final int position, View convertView, ViewGroup parent) {
if (layoutInflater == null) {
layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_item_thought, null);
}
thoughtText = (TextView) convertView.findViewById(R.id.thought_text_view);
likeButton = (Button) convertView.findViewById(R.id.thought_like_button);
dislikeButton = (Button) convertView.findViewById(R.id.thought_dislike_button);
//thoughtItems is a list of custom ojbects (Thought)
Thought t = thoughtItems.get(position);
//Here i set the content of the current TextView
thoughtText.setText(t.getText());
//the two buttons do basically the same thing when get clicked
likeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Thought t = thoughtItems.get(position);
thoughtId = t.getId();
opinion = 1;
AsyncPost asyncPost = new AsyncPost(activity,ThoughtListAdapter.this);
asyncPost.execute(SHARE_THOUGHT_URL,
TAG_PERSON_EMAIL, "m#b.it",
TAG_THOUGHT_ID, thoughtId.toString(),
TAG_OPINION, opinion.toString());
}
});
dislikeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Thought t = thoughtItems.get(position);
thoughtId = t.getId();
opinion = 0;
AsyncPost asyncPost = new AsyncPost(activity,ThoughtListAdapter.this);
asyncPost.execute(SHARE_THOUGHT_URL,
TAG_PERSON_EMAIL, "m#b.it",
TAG_THOUGHT_ID, thoughtId.toString(),
TAG_OPINION, opinion.toString());
}
});
return convertView;
}
#Override
public void onComplete(JSONObject json) {
if (json != null) {
try {
if (json.getInt(TAG_SUCCESS) == 0) {
Toast.makeText(activity, "Operazione non riuscita.", Toast.LENGTH_LONG).show();
} else {
//if everything is good i try to make the buttons of that particular list item disappear
likeButton.setVisibility(View.GONE);
dislikeButton.setVisibility(View.GONE);
}
}
catch (JSONException e) {
Log.e(TAG_LOG, "JSONException", e);
}
}
else Toast.makeText(activity, "Errore connessione!", Toast.LENGTH_LONG).show();
}
One solution to this would be to have something on your Thought object to indicate whether or not to show the buttons.
So in your getView() method you check this
likeButton = (Button) convertView.findViewById(R.id.thought_like_button);
dislikeButton = (Button) convertView.findViewById(R.id.thought_dislike_button);
Thought t = thoughtItems.get(position);
if (t.hideButtons()) {
likeButton.setVisibility(View.GONE);
dislikeButton.setVisibility(View.GONE);
}
else {
likeButton.setVisibility(View.VISIBLE);
dislikeButton.setVisibility(View.VISIBLE);
}
Then you would need to have your onComplete method return the id of the Thought object that it related to. Then inside your onComplete you could do
int id = //get your id from your JSON response
for(Thought t : thoughtItems) {
if (t.getId() == id) {
t.setHideButtons(true);
notifyDataSetChanged();
break;
}
}
By calling notifyDataSetChanged() it will redraw your list and when it does the check for whether it should show the buttons or not it will not show them because it was set on that thought item

How can I remove setBackgroundResource when new View is clicked?

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;
}
});

Android - Change text of button in a selected row

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.

Categories

Resources