textView how to display text - java

How can I display text in TextView at get view after this call
ListAdapter adapter = new MyCustomAdapter (
ManageSection.this, studentList,
R.layout.list_student, new String[] { TAG_StudentID,
TAG_StudentNo,TAG_FullName},
new int[] { R.id.StudentID, R.id.StudentNo,R.id.FullName});
setListAdapter(adapter);
and the class MyCustomAdapter has a get view, I will display the text
holder.FullName= (TextView) convertView.findViewById(R.id.FullName);
holder.FullName.setText();
holder.StudentNo=(TextView) convertView.findViewById(R.id.StudentNo);
holder.StudentNo.setText();
So at set text what should I write cause I do
no=student.get(holder.position).get(TAG_StudentNo);
name =student.get(holder.position).get(TAG_FullName);
and take no, name but the text is duplicated in the whole list
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView==null)
{
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(resource, parent, false);
holder = new ViewHolder();
no=student.get(holder.position).get(TAG_StudentNo);
name =student.get(holder.position).get(TAG_FullName);
holder.StudentID= (TextView) convertView.findViewById(R.id.StudentID);
holder.FullName= (TextView) convertView.findViewById(R.id.FullName);
holder.FullName.setText();
holder.StudentNo=(TextView) convertView.findViewById(R.id.StudentNo);
holder.StudentNo.setText();
holder.DeleteStudent = (ImageView) convertView.findViewById(R.id.DeleteStudent);
holder.AlertIcon = (ImageView) convertView.findViewById(R.id.Alert);
// add a listener for phone call
holder.DeleteStudent.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
id = student.get(holder.position).get(TAG_StudentID);
Toast.makeText(getContext(),id,Toast.LENGTH_LONG).show();
}
});
holder.AlertIcon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// String email = MyCustomAdapter.listMap.get(holder.position).get("email");
// ActivityHelper.startActivity(ActivityManager.EMAIL, email);
}
});
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.position = position;
return convertView;
}
private static class ViewHolder
{
ImageView DeleteStudent;
ImageView AlertIcon;
TextView StudentID, StudentNo ,FullName;
int position;
}
}
Can anyone tell me what the problem is here?

Try this code.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView==null)
{
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(resource, parent, false);
holder = new ViewHolder();
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.position = position;
no=student.get(holder.position).get(TAG_StudentNo);
name =student.get(holder.position).get(TAG_FullName);
holder.StudentID= (TextView) convertView.findViewById(R.id.StudentID);
holder.FullName= (TextView) convertView.findViewById(R.id.FullName);
holder.FullName.setText();
holder.StudentNo=(TextView) convertView.findViewById(R.id.StudentNo);
holder.StudentNo.setText();
holder.DeleteStudent = (ImageView) convertView.findViewById(R.id.DeleteStudent);
holder.AlertIcon = (ImageView) convertView.findViewById(R.id.Alert);
// add a listener for phone call
holder.DeleteStudent.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
id = student.get(holder.position).get(TAG_StudentID);
Toast.makeText(getContext(),id,Toast.LENGTH_LONG).show();
}
});
holder.AlertIcon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// String email = MyCustomAdapter.listMap.get(holder.position).get("email");
// ActivityHelper.startActivity(ActivityManager.EMAIL, email);
}
});
return convertView;
}
private static class ViewHolder
{
ImageView DeleteStudent;
ImageView AlertIcon;
TextView StudentID, StudentNo ,FullName;
int position;
}
}
What you were doing is just setting the data only when the convertView is null and if not you just returning the view after some Holder operation. You need to set Text etc, in the textView in every case. If you need more explanation, please ask.

its simple buddy U just have to put this line instead of blank setText()
no=student.get(holder.position).get(TAG_StudentNo);// the roll_number string
name =student.get(holder.position).get(TAG_FullName);//full name string
holder.StudentID= (TextView) convertView.findViewById(R.id.StudentID);
holder.FullName= (TextView) convertView.findViewById(R.id.FullName);
holder.FullName.setText(name);//pass fullname
holder.StudentNo=(TextView) convertView.findViewById(R.id.StudentNo);
holder.StudentNo.setText(no);//pass roll no.
you just have to pass the no and 'namevariable tosetText()` method.

Related

Custom List view value fluctuated when new value added?

I am trying do the chatting application. When send a message that should be in right side , received message should be in left side. I have done everything in good manner. when I add new value in list view , previous value has fluctuated. Please can any one solve my problem ? I have attached my source code.
Main Activity :
public class MainActivity extends AppCompatActivity {
ArrayList<ChatData> data = new ArrayList<ChatData>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView) findViewById(R.id.list_item);
final ChatAdaptor chatAdaptor = new ChatAdaptor(this, getDetails());
listView.setAdapter(chatAdaptor);
listView.setDivider(null);
ImageView imageView = (ImageView) findViewById(R.id.submit);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText objText = (EditText) findViewById(R.id.text_msg);
chatAdaptor.add(new ChatData(objText.getText().toString(), false));
listView.setSelection(chatAdaptor.getCount() - 1);
objText.setText(null);
// chatAdaptor.notifyDataSetChanged();
}
});
}
public ArrayList<ChatData> getDetails() {
data.add(new ChatData("Hi! Agent", false));
data.add(new ChatData("Hi! Agent ,Are you there ", false));
data.add(new ChatData("Yeah tell me how can I help you", true));
data.add(new ChatData("I am getting wrong balance", false));
return data;
}
}
Custom adapter:
public class ChatAdaptor extends ArrayAdapter<ChatData> {
private Activity activity;
String TAG = "bks";
private List<ChatData> originalData = null;
public ChatAdaptor(Activity activity, ArrayList<ChatData> objects) {
super(activity, 0, objects);
this.activity = activity;
this.originalData = objects;
}
#Override
//called when rendering the list
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
// convertView = mInflater.inflate(R.layout.search_data, true,null);
LayoutInflater inflater = activity.getLayoutInflater();
convertView = inflater.inflate(R.layout.test, null, false);
if (originalData.get(position).isAgentMsg())
convertView = inflater.inflate(R.layout.show_chat_agent, null, true);
else
convertView = inflater.inflate(R.layout.show_chat_client, null, true);
// Creates a ViewHolder and store references to the three views
// we want to bind data to.
holder = new ViewHolder();
holder.objMsg = ( TextView ) convertView.findViewById(R.id.msg) ;
// Bind the data efficiently with the holder.
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
holder = (ViewHolder) convertView.getTag();
}
holder.objMsg.setText(originalData.get(position).getMsg());
return convertView;
}
static class ViewHolder {
TextView objMsg;
/** TextView objClientText, objAgentText;
ImageView objClientImage1, objClientImage2;
ImageView objAgentImage1, objAgentImage2;
*/
}
}
Thanks
Kalanidhi M
Just change this line in your getView method
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.test, parent, true);
if (originalData.get(position).isAgentMsg())
convertView = inflater.inflate(R.layout.show_chat_agent, parent, true);
else
convertView = inflater.inflate(R.layout.show_chat_client, parent, true);

Different Button in List view

I have two ArrayList namely "one" and "two" ["two" is always a sub-set of "one"], And i have a list view which is populating by Arraylist "one". Now am checking condition that if element of "two" is present in "one", then set ImageButton in list view and if not the set Button in list view.
Below is my getView method. Any help or keywords will be appreciated.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
//HERE HOW TO SWITCH TWO LAYOUTS..
convertView = inflater.inflate(R.layout.invite_row, null);
viewHolder = new ViewHolder();
viewHolder.name = (TextView)convertView.findViewById(R.id.textView6);
viewHolder.text = (TextView) convertView.findViewById(R.id.childTextView);
viewHolder.button = (Button) convertView
.findViewById(R.id.childButton);
Typeface typeFace= Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf");
viewHolder.name.setTypeface(typeFace);
// viewHolder.button.setBackgroundResource(R.drawable.invitebuttonbackground);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final String temp = getItem(position);
final String tempname = getItem(position);
viewHolder.name.setText(tempname);
viewHolder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (customListner != null) {
customListner.onButtonClickListner(position, temp, tempname);
}
}
});
return convertView;
}
one thing you can do is
View convertView;
if (whatever condition) {
convertView = inflater.inflate(R.layout.invite_row, null);
} else {
convertView = inflater.inflate(R.layout.invite_row_two, null);
}
if you can post your layout , i will have clear undestanding of what you want to doing.

Android ListView Get row on button click

I have a custom adapter class for a listview and I want to be able to access the content of a specific row by clicking a button on it. I tried to create a ViewHolder, but I get a NPE error when I try to click it.
static class ViewHolder {
TextView camera;
TextView players;
TextView max_players;
ImageView privata;
Button Buton;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
String variabile[] = getItem(position).split("\\s+");
LayoutInflater linflater = LayoutInflater.from(getContext());
View customView = linflater.inflate(R.layout.custom_row, parent, false);
final ViewHolder holder = new ViewHolder();
holder.camera = (TextView) customView.findViewById(R.id.Nume);
holder.players = (TextView) customView.findViewById(R.id.players);
holder.max_players = (TextView) customView.findViewById(R.id.max_players);
holder.privata = (ImageView) customView.findViewById(R.id.privata);
holder.Buton = (Button) customView.findViewById(R.id.Buton);
holder.camera.setText(variabile[0]);
if (!variabile[1].equals("true")) {
parola = false;
holder.privata.setVisibility(View.INVISIBLE);
}
holder.players.setText(variabile[2]);
holder.max_players.setText(variabile[3]);
room_id = variabile[4];
nume = variabile[5];
holder.Buton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
hash = new HashMap<String, String>();
hash.put("name", nume);
hash.put("room", room_id);
if (intra) {
holder.Buton.setText("Iesi");
site = siteul + "/join";
intra = false;
} else {
holder.Buton.setText("Intra");
site = siteul + "/leave";
intra = true;
}
new ATask().execute(site);
}
});
return customView;
}
When using the ViewHolder pattern, you should check if the convertView in null or has been created before, in the getView method, and after that use setTag and getTag methods. like this :
if (convertView == null)
{
LayoutInflater linflater = LayoutInflater.from(getContext());
convertView = linflater.inflate(R.layout.your_list_item_view, parent, false);
viewHolder.textView = (TextView)convertView.findViewById([the id]);
.
.
.
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
You need to check if the convertView is null so it is already has been visited or not then store the holder in tag Like
ViewHolder holder;
if (convertView == null) {
LayoutInflater linflater = LayoutInflater.from(getContext());
holder = linflater.inflate(R.layout.custom_row, parent, false);....
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}//Common code

Set onClickListener into custom adapter

Hello at all the community,
i've this adapter with 2 button and 2 textview.
#Override
public View getView(int position, View view, ViewGroup parent) {
if(view == null) {
holder = new Holder();
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.my_adapter, null);
holder.result = (TextView)view.findViewById(R.id.description);
holder.value = (TextView)view.findViewById(R.id.value);
holder.add = (Button)view.findViewById(R.id.add);
holder.subtract = (Button)view.findViewById(R.id.subtract);
myObject = getItem(position);
holder.result.setText(myObject.result);
holder.value.setText(myObject.value);
view.setTag(holder);
} else {
holder = (Holder)view.getTag();
}
holder.add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
}
});
return view;
}
Now my question is: if I want that when the user press on add button set the text of the textview with (for example) 5 how can i do this? If I put into the onCLick method
holder.result.setText("My text") set the text of the last textview and not the correspond textview of the selected row item (I disabled the click on the listview with):
#Override
public boolean isEnabled(int position) {
return false;
}
Is there any solution for my problem?
You should put your code in the getView method inside the adapter and remember to use references to the current Button/TextVeiw so that each Button would correspond to that specific TextView
P.S. I found something with your code check this out:
#Override
public View getView(int position, View view, ViewGroup parent) {
if(view == null) {
holder = new Holder();
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.my_adapter, null);
holder.result = (TextView)view.findViewById(R.id.description);
holder.value = (TextView)view.findViewById(R.id.value);
holder.add = (Button)view.findViewById(R.id.add);
holder.subtract = (Button)view.findViewById(R.id.subtract);
view.setTag(holder);
} else {
holder = (Holder)view.getTag();
}
myObject = getItem(position);
holder.result.setText(myObject.result);
holder.value.setText(myObject.value);
final TextView tv = holder.result;
holder.add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
tv.setText("bla bla");
}
});
return view;
}
You mixed something up, the getItem has to be below the view creation stuff. First create the view, then set the values.
#Override
public View getView(int position, View view, ViewGroup parent) {
if(view == null) {
holder = new Holder();
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.my_adapter, null);
holder.result = (TextView)view.findViewById(R.id.description);
holder.value = (TextView)view.findViewById(R.id.value);
holder.add = (Button)view.findViewById(R.id.add);
holder.subtract = (Button)view.findViewById(R.id.subtract);
view.setTag(holder);
} else {
holder = (Holder)view.getTag();
}
myObject = getItem(position);
holder.result.setText(myObject.result);
holder.value.setText(myObject.value);
holder.add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//What should happen here?
}
});
return view;
}

Button in ListView Item

I have a problem.
In my app, i've got a custom ListView with two Button for each Row.
I've created a class extends ArrayAdapter where i try to manage the OnClickListner of the Button, but it Doesn't work. If i press a buttton, nothing happens.
This is my class:
public class AdapterMovimentazioni extends ArrayAdapter<ItemWithIdMov> {
private Context context;
private ArrayList<ItemWithIdMov> items;
private LayoutInflater vi;
public AdapterMovimentazioni(Context context, ArrayList<ItemWithIdMov> items) {
super(context, 0, items);
this.context = context;
this.items = items;
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
final ItemWithIdMov i = items.get(position);
if (convertView == null) {
convertView = vi.inflate(R.layout.item_movimentazioni, null);
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.imageMov);
holder.data = (TextView) convertView.findViewById(R.id.DataMov);
holder.importo = (TextView) convertView.findViewById(R.id.ImportoMov);
holder.locale = (TextView) convertView.findViewById(R.id.localeMov);
holder.delete = (Button) convertView.findViewById(R.id.ButtonDeleteMov);
holder.set = (Button) convertView.findViewById(R.id.ButtonSetMov);
holder.delete.setTag(holder);
holder.delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "DELETEEEEEE", Toast.LENGTH_LONG);
}
});
holder.set.setTag(holder);
holder.set.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "SET", Toast.LENGTH_LONG);
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (i != null) {
holder.icon.setImageResource(i.getIcon());
holder.data.setText(i.getData());
holder.importo.setText(i.getImporto());
holder.locale.setText(i.getLocName());
}
return convertView;
}
private class ViewHolder {
public ViewHolder() {
}
protected Button delete;
protected Button set;
protected ImageView icon;
protected TextView data;
protected TextView importo;
protected TextView locale;
}
Can you help me??
Thank you
use Toast Like
Toast.makeText(context, "SET", Toast.LENGTH_LONG).show();
change your getview method after createing views decleration onClicklistener;
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
final ItemWithIdMov i = items.get(position);
if (convertView == null) {
convertView = vi.inflate(R.layout.item_movimentazioni, null);
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.imageMov);
holder.data = (TextView) convertView.findViewById(R.id.DataMov);
holder.importo = (TextView) convertView.findViewById(R.id.ImportoMov);
holder.locale = (TextView) convertView.findViewById(R.id.localeMov);
holder.delete = (Button) convertView.findViewById(R.id.ButtonDeleteMov);
holder.set = (Button) convertView.findViewById(R.id.ButtonSetMov);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "DELETEEEEEE", Toast.LENGTH_LONG);
}
});
holder.set.setTag(holder);
holder.set.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "SET", Toast.LENGTH_LONG);
}
});
if (i != null) {
holder.icon.setImageResource(i.getIcon());
holder.data.setText(i.getData());
holder.importo.setText(i.getImporto());
holder.locale.setText(i.getLocName());
}
return convertView;
}

Categories

Resources