Can't only see the first row on a listview - java

I just created a listview and I can see all the rows at my listview exept to the first one. anyone got any idea why this is happend. my adapter code is:
public class ChatAdapter extends ArrayAdapter<Message> {
public ChatAdapter(Context context, int resource, List<Message> items) {
super(context, resource, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.chat_raw, null);
}
Message message = getItem(position);
Log.d("GET VIEW MESSAGE: ", message.getText());
if (message != null) {
TextView tvName = (TextView) v.findViewById(R.id.tvChatName);
TextView tvMessage = (TextView) v.findViewById(R.id.tvChatMessage);
if (tvName != null) {
tvName.setText(message.getUserName());
}
if (tvMessage != null) {
tvMessage.setText(message.getText());
}
}
return v;
}
}
I must say that at line: Log.d("GET VIEW MESSAGE: ", message.getText()); I see all the messages and also the 1st one but at the listview I don't see the 1st one.
edit:
Just change the code according to what you seggest but I still have the same problem, I can't see the 1st row. my adapter code is:
public class ChatAdapter extends ArrayAdapter<Message> {
private Context context;
public ChatAdapter(Context context, int resource, List<Message> items) {
super(context, resource, items);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder viewHolder=null;
if(convertView==null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.chat_raw, null);
viewHolder = new ViewHolder(row);
row.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) row.getTag();
}
Message message = getItem(position);
if(message!=null) {
viewHolder.tvMessage.setText(message.getText());
viewHolder.tvName.setText(message.getUserName());
}
return row;
}
public class ViewHolder {
TextView tvName;
TextView tvMessage;
public ViewHolder(View view) {
tvName = (TextView) view.findViewById(R.id.tvChatName);
tvMessage = (TextView) view.findViewById(R.id.tvChatMessage);
}
}
}
and my code is:
public class ChatFragment extends Fragment {
private ListView lvChat;
private Button btChatSend;
private EditText etChatMessage;
private ArrayList<Message> alMessage;
private int groupID, userId;
private String userName;
private RequestQueue requestQueue;
private Gson gson;
public ChatFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
alMessage = new ArrayList<>();
groupID = ((DrawAndChat) getActivity()).getGroupId();
userName = ((DrawAndChat) getActivity()).getUserName();
userId = ((DrawAndChat) getActivity()).getUserId();
requestQueue = Volley.newRequestQueue(getActivity());
getAllMessages();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.chat_fragment, container, false);
lvChat = (ListView) view.findViewById(R.id.lvChat);
etChatMessage = (EditText) view.findViewById(R.id.etChatMessage);
btChatSend = (Button) view.findViewById(R.id.btChatSend);
lvChat.setAdapter(new ChatAdapter(getActivity(), R.layout.chat_raw, alMessage));
btChatSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sendMessage();
}
});
return view;
}

Related

How to change an attribute in a listview with ProgramAdapter class android studio JAVA

I need to change the clicked text in listview using a ProgramAdapter.
MainActivity.java:
public class MainActivity extends AppCompatActivity {
String[] programName = {"ex1", "ex2", "ex3"}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvProgram = findViewById(R.id.listView);
ProgramAdapter programAdapter = new ProgramAdapter(this, programName);
lvProgram.setAdapter(programAdapter);
}}
ProgramAdapter.java:
public class ProgramAdapter extends ArrayAdapter<String> {
Context context;
String[] programName;
public ProgramAdapter(Context context, String[] programName) {
super(context, R.layout.single_item2, R.id.titulo, programName);
this.context = context;
this.programName = programName;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View singleItem = convertView;
ProgramViewHolder holder = null;
if(singleItem == null){
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
singleItem = layoutInflater.inflate(R.layout.single_item2, parent, false);
holder = new ProgramViewHolder(singleItem);
singleItem.setTag(holder);
}
else{
holder = (ProgramViewHolder) singleItem.getTag();
}
holder.programTitle.setText(programName[position]);
}
}
My attempt was that:
ProgramViewHolder finalHolder = holder;
singleItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(programName[position] == "ex1") {
finalHolder.programTitle.setText("expected text");
}
}
});
This changes the text for a moment, but every time you go down the list and up again, the text goes back to the default text.

How to pass checked images in fragments

i have listView, and there are checkbox, imageView, TextView. I wanna pass that images, that was checked in listView. I know that's doing with bundle, but how to do the checkbox part? I have checkbox, but i don't know how to initialize it.
Here's the adapter part.
public class MyAdapter extends ArrayAdapter<MyAdapter> {
String names[];
int flags[];
Context mContext;
public MyAdapter(Context context, String[] languageNames, int[] countryFlags) {
super(context, R.layout.listview_items);
this.names = languageNames;
this.flags = countryFlags;
this.mContext = context;
}
#Override
public int getCount() {
return names.length;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder = new ViewHolder();
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listview_items, parent, false);
mViewHolder.mFlag = convertView.findViewById(R.id.imageView);
mViewHolder.mLanguage = convertView.findViewById(R.id.textView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder) convertView.getTag();
}
mViewHolder.mFlag.setImageResource(flags[position]);
mViewHolder.mLanguage.setText(names[position]);
return convertView;
}
static class ViewHolder {
ImageView mFlag;
TextView mLanguage;
}}
You see there's only an image and textview. How to add checkbox to there?
Here's the listView part.
public class FragmentLanguage extends Fragment {
View mainView;
ListView listView;
Context mContext;
Button next;
MyAdapter adapter;
ArrayList<Integer> mItems = new ArrayList<>();
int[] images = {R.drawable.download,
R.drawable.download,
R.drawable.download,
R.drawable.download,
R.drawable.download,
R.drawable.download,
R.drawable.download,
R.drawable.download};
String[] languages = {"Armenian", "Russian", "US English", "Portugal",
"Spanish", "Georgian", "French", "Italian"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mainView = inflater.inflate(R.layout.listview, container, false);
listView = mainView.findViewById(R.id.list_view);
listView.setAdapter(adapter);
adapter = new MyAdapter(getActivity(), languages, images);
listView.setAdapter(adapter);
next = mainView.findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return mainView;
}}
1)How to add the checkbox part? 2) How to send images from this fragment to another fragment with bundle? Thanks. I'll write the bundle part here
next.setOnClickListener(new View.OnClickListener()
OPTION 1:
make a list of Int val listOfSelectedImages = arrayListOf()
checkBox.setOnClickListener {
if (checkBox.isChecked) {
//when checked add image(example R.id.image1) to list
} else {
//remove from the list
}
}
from one fragment to another:
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
val mFragment2 = Fragment2()
val bundle = Bundle()
bundle.putParcelableArrayList(KEY, listOfSelectedImages)
mFragment2.arguments = bundle
supportFragmentManager.beginTransaction().add(R.id.fragment_container, mFragment2)
.commit()
}
});
in fragment 2 go val list = getArguments.getParcelableArrayList(KEY)

How to implement a ListView in a Fragment in Android

I have Fragment.java and its XML. In XML I put ListView and I want to update the ListView in Fragment and add some String.
I am trying to make a custom adapter. When I run the ListView in Activity with custom adapter all works great, but when I put the same code in Fragment nothing happens. I am using EventBus3.
This is my Activity send Event
EventBus.getDefault().post(new SendDataHelper(nameOfItem[myItemInt]));
This is my Fragment:
public class MyOrderFragment extends Fragment {
TextView name;
ListView listOrder;
private Context context;
List<cources> collegeList;
public MyOrderFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this);
}
#Subscribe
public void onMessageEvent(SendDataHelper event){
Toast.makeText(getActivity(), event.getText(), Toast.LENGTH_SHORT).show();
String text = event.getText();
name.setText(text);
cources college;
collegeList = new ArrayList<cources>();
college = new cources();
college.name = "sharon";
collegeList.add(college);
listOrder.setVisibility(View.VISIBLE);
ListAdapter adapter = new ListAdapter(collegeList, context);
listOrder.setAdapter(adapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.my_order_fragment, container, false);
name = (TextView) rootView.findViewById(R.id.textView);
listOrder = (ListView) rootView.findViewById(R.id.listOrder);
return rootView;
}
}
This is my adapter
public class ListAdapter extends BaseAdapter
{
Context context;
List<cources> valueList;
public ListAdapter(List<cources> listValue, Context context)
{
this.context = context;
this.valueList = listValue;
}
#Override
public int getCount()
{
return this.valueList.size();
}
#Override
public Object getItem(int position)
{
return this.valueList.get(position);
}
#Override
public long getItemId(int position)
{
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewItem viewItem = null;
if(convertView == null)
{
viewItem = new ViewItem();
LayoutInflater layoutInfiater = (LayoutInflater)this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
//LayoutInflater layoutInfiater = LayoutInflater.from(context);
convertView = layoutInfiater.inflate(R.layout.list_adapter_view, null);
viewItem.txtNamePlanche = (TextView)convertView.findViewById(R.id.Tx_name_planche);
viewItem.txtPriceBaget = (TextView)convertView.findViewById(R.id.Tx_price_baget);
viewItem.txtPricePlate = (TextView)convertView.findViewById(R.id.Tx_price_plate);
convertView.setTag(viewItem);
}
else
{
viewItem = (ViewItem) convertView.getTag();
}
viewItem.txtNamePlanche.setText(valueList.get(position).name);
viewItem.txtPriceBaget.setText(valueList.get(position).price_baget);
viewItem.txtPricePlate.setText(valueList.get(position).price_plate);
return convertView;
}
}
This is my ItemView
import android.widget.TextView;
class ViewItem
{
TextView txtNamePlanche;
TextView txtPricePlate;
TextView txtPriceBaget;
}
This is cources:
public class cources
{
public String name;
public String price_plate;
public String price_baget;
}
Change extends Fragment to extends ListFragment

My simple custom listview show wrong items, not working as intended. Can't see what is wrong

I am trying to create something like this: https://i.stack.imgur.com/l8ZOc.png
However, i ran into a problem. When i create the list with my adapter, it is supposed to be a list of 8 items. However, it just shows the first 4 of these items in a random order two times. Do you see what is wrong with my code?
public class MyActivity extends Activity{
String headers[];
String image_urls[];
List<MyMenuItem> menuItems;
ListView mylistview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
menuItems = new ArrayList<MyMenuItem>();
headers = getResources().getStringArray(R.array.header_names);
image_urls = getResources().getStringArray(R.array.image_urls);
for (int i = 0; i < headers.length; i++) {
MyMenuItem item = new MyMenuItem(headers[i], image_urls[i]);
menuItems.add(item);
}
mylistview = (ListView) findViewById(R.id.list);
MenuAdapter adapter = new MenuAdapter(this, menuItems);
mylistview.setAdapter(adapter);
mylistview.setOnItemClickListener(this);
}
public class MyMenuItem {
private String item_header;
private String item_image_url;
public MyMenuItem(String item_header, String item_image_url){
this.item_header=item_header;
this.item_image_url=item_image_url;
}
public String getItem_header(){
return item_header;
}
public void setItem_header(String item_header){
this.item_header=item_header;
}
public String getItem_image_url(){
return item_image_url;
}
public void setItem_image_url(String item_image_url){
this.item_image_url=item_image_url;
}
}
public class MenuAdapter extends BaseAdapter{
Context context;
List<MyMenuItem> menuItems;
MenuAdapter(Context context, List<MyMenuItem> rowItems) {
this.context = context;
this.menuItems = rowItems;
}
#Override
public int getCount() {
return menuItems.size();
}
#Override
public Object getItem(int position) {
return menuItems.get(position);
}
#Override
public long getItemId(int position) {
return menuItems.indexOf(getItem(position));
}
private class ViewHolder {
ImageView ivMenu;
TextView tvMenuHeader;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.menu_item, null);
holder = new ViewHolder();
holder.tvMenuHeader = (TextView) convertView.findViewById(R.id.tvMenuHeader);
holder.ivMenu = (ImageView) convertView.findViewById(R.id.ivMenuItem);
MyMenuItem row_pos = menuItems.get(position);
Picasso.with(context)
.load(row_pos.getItem_image_url())
.placeholder(R.drawable.empty)
.error(R.drawable.error)
.into(holder.ivMenu);
holder.tvMenuHeader.setText(row_pos.getItem_header());
Log.e("Test", "headers:" + row_pos.getItem_header());
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
You are setting the data only when the convertView is null.So when it is not null,list items are inflated with data of previous list items. getView should be something like this
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.menu_item, null);
holder = new ViewHolder();
holder.tvMenuHeader = (TextView) convertView.findViewById(R.id.tvMenuHeader);
holder.ivMenu = (ImageView) convertView.findViewById(R.id.ivMenuItem);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
MyMenuItem row_pos = menuItems.get(position);
Picasso.with(context)
.load(row_pos.getItem_image_url())
.placeholder(R.drawable.empty)
.error(R.drawable.error)
.into(holder.ivMenu);
holder.tvMenuHeader.setText(row_pos.getItem_header());
Log.e("Test", "headers:" + row_pos.getItem_header());
return convertView;
}
I think this is the solution
else {
holder = (ViewHolder) convertView.getTag();
holder.tvMenuHeader.setText(row_pos.getItem_header());
}
I also encountered this problem earlier and this one works for me.

Dynamically adding listitems to baseadapter an android

I want to dynamically add new listitems by pressing a button.but i don't no the right way to do this.
BaseAdapter
public class CompetitionResultListAdapter extends BaseAdapter{
private Activity activity;
private ArrayList<Result> result;
DataManager datamanager;
#Override
public int getCount() {
return result!=null ? result.size() : 0;
}
#Override
public Object getItem(int position) {
return result.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
// inflate the layout
LayoutInflater inflater = activity.getLayoutInflater();
convertView = inflater.inflate(R.layout.list_item_competition_result, parent, false);
}
EditText listItemDate = (EditText) convertView.findViewById(R.id.date);
EditText listItemPlace = (EditText) convertView.findViewById(R.id.place);
EditText listItemComment = (EditText) convertView.findViewById(R.id.comment);
result.get(position).setDate(listItemDate.getText().toString());
result.get(position).setPlace(listItemPlace.getText().toString());
result.get(position).setComment(listItemComment.getText().toString());
return convertView;
}
public void refreshResultList(ArrayList<Result> result){
this.result = result;
notifyDataSetChanged();
}
}
java class
public class AddCompetitionResult extends Fragment {
private CompetitionResultListAdapter competitionResultListAdapter;
private DataManager datamanager;
ArrayList<Result> result;
ListView listView;
private Horse horse;
private Button addResults;
int clickCounter=0;
public AddHorseCompetitionResult(Horse horse) {
this.horse = horse;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.add_horse_competition_result,
null);
addResults = (Button) view.findViewById(R.id.btn_add_competition_result);
listView = (ListView) view.findViewById(R.id.data_list);
// result = datamanager.create(getActivity());
competitionResultListAdapter = new CompetitionResultListAdapter();
View footerView = ((LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE)).inflate(
R.layout.competition_result_listview_footer, null, false);
listView.addFooterView(footerView);
listView.setAdapter(competitionResultListAdapter);
addResults.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String resultId = null;
// result.add();
competitionResultListAdapter.notifyDataSetChanged();
}
});
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (competitionResultListAdapter.getCount() == position) {
MainActivity mainActivity = MainActivity.getInstance();
mainActivity.setFragment(new AddHorseDetailedInfo(horse),
"Add horse more info");
// datamanager.createResult(result);
} else {
// ContactGroup cg = (ContactGroup)
// competitionResultListAdapter.getItem(position);
// MainActivity mainActivity = MainActivity.getInstance();
// mainActivity.setFragment(new
// ContactListView(cg.getGroupId()), "contacts");
}
}
});
return view;
}
}
// on button CLick
{
// toast a meesaage here : onClick()
result.add(urObject)// simply put create a object of ur result type and put "" values
// finallly call notifyDataSetChanged on adapter
adapter.notifyDataSetChanged();
}
Tryt to replace your adapter code with this.
public class CompetitionResultListAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<Result> result;
DataManager datamanager;
#Override
public int getCount() {
return result!=null ? result.size() : 0;
}
#Override
public Object getItem(int position) {
return result.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(activity).inflate(R.layout.list_item_competition_result, null, false);
holder.listItemDate = (EditText) convertView.findViewById(R.id.date);
holder.listItemPlace = (EditText) convertView.findViewById(R.id.place);
holder.listItemComment = (EditText) convertView.findViewById(R.id.comment);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.listItemDate = (EditText) convertView.findViewById(R.id.date);
holder.listItemPlace = (EditText) convertView.findViewById(R.id.place);
holder.listItemComment = (EditText) convertView.findViewById(R.id.comment);
result.get(position).setDate(listItemDate.getText().toString());
result.get(position).setPlace(listItemPlace.getText().toString());
result.get(position).setComment(listItemComment.getText().toString());
return convertView;
}
class ViewHolder {
EditText listItemDate;
EditText listItemPlace;
EditText listItemComment;
}
public void refreshResultList(ArrayList<Result> result){
this.result = result;
notifyDataSetChanged();
}
}

Categories