List view not working in android - java

I am trying to make an app of notifications for my college project.
I am trying to create a Customized Adapter for a listview , but the activity containing the ListView does not show anything. I think I have done something wrong in the getView() method in CustomAdapter.java.
CustomAdapter class is used to created the child views for the the listview. listviechild.xml defines the layout for single row in the listview tith xml file - listactivity.xml
package com.example.client_nic;
import java.util.ArrayList;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class CustomAdapter extends BaseAdapter {
public Context context =null;
public ArrayList<String> nam = null;
public ArrayList<String> date= null;
public ArrayList<String> time = null;
LayoutInflater inflater =null;
public CustomAdapter(Context context, ArrayList<String> nam,
ArrayList<String> date, ArrayList<String> time) {
super();
nam = new ArrayList<String>();
date = new ArrayList<String>();
time = new ArrayList<String>();
this.context = context;
this.nam = nam;
this.date = date;
this.time = time;
inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return nam.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public View getView(int pos, View view, ViewGroup arg2) {
// TODO Auto-generated method stub
Log.e("name",nam.get(pos));
Log.e("date",date.get(pos));
Log.e("time", time.get(pos));
view = inflater.inflate(R.layout.listviewchild, arg2,false);
if(view.isActivated()){
Toast.makeText(context, "yes",Toast.LENGTH_SHORT).show();
}
TextView nametv = (TextView)view.findViewById(R.id.textView1);
TextView datetv = (TextView)view.findViewById(R.id.textView2);
TextView timetv = (TextView)view.findViewById(R.id.textView3);
nametv.setText(nam.get(pos));
datetv.setText(date.get(pos));
timetv.setText(time.get(pos));
return view;
}
}
listviewchild.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="30dp" />
<TextView android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="20dp"
android:layout_below="#+id/textView1"
/>
<TextView android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="20dp"
android:layout_marginLeft="30dp"
android:layout_toRightOf="#+id/textView2"
android:layout_below="#+id/textView1"
/>
</RelativeLayout>
listactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>

Try the following:
package com.example.client_nic;
import java.util.ArrayList;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class CustomAdapter extends BaseAdapter {
public Context context =null;
public ArrayList<String> nam = null;
public ArrayList<String> date = null;
public ArrayList<String> time = null;
LayoutInflater inflater = null;
public CustomAdapter(Context context, ArrayList<String> nam,
ArrayList<String> date, ArrayList<String> time) {
nam = new ArrayList<String>();
date = new ArrayList<String>();
time = new ArrayList<String>();
this.context = context;
this.nam = nam;
this.date = date;
this.time = time;
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return nam.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public View getView(int pos, View view, ViewGroup arg2) {
// TODO Auto-generated method stub
Log.e("name",nam.get(pos));
Log.e("date",date.get(pos));
Log.e("time", time.get(pos));
view = inflater.inflate(R.layout.listviewchild, arg2, false);
if(view.isActivated()){
Toast.makeText(context, "yes",Toast.LENGTH_SHORT).show();
}
TextView nametv = (TextView)view.findViewById(R.id.textView1);
TextView datetv = (TextView)view.findViewById(R.id.textView2);
TextView timetv = (TextView)view.findViewById(R.id.textView3);
nametv.setText(nam.get(pos));
datetv.setText(date.get(pos));
timetv.setText(time.get(pos));
return view;
}
}
This should properly inflate your views and work. If there are other problems, you may be passing empty data to to the Adapter.
Couple of side notes
1) You are passing three ArrayLists to fill the data for your ListView. Why not just pass a single array list with custom Java objects? The fields for the java objects would be name, data, time.
i.e:
public class MyObject{
public String name;
public String date;
public String time;
MyObject(String name, String date, String time){
this.name = name;
this.date = date;
this.time = time;
}
//define getters and setters...
}
and:
public CustomAdapter(Context context, ArrayList<MyObject> objs) {
...
...
}
2) You are inflating the view each time. Instead, use the ViewHolder Pattern to hold the views data rather than re-drawing this and looking up resources each time in your application. Android makes use of recycling views so you want to maximize on this and save resources and reduce delays from loading views each time (lots of overhead!). I will not provide an example on this for you, but look up that link to find out how to do this yourself!

By looking at your code I think that the instance varibles of class CustomAdapter (ArrayList s) nam, date and time are not getting filled with the contents of the corresponding ArrayList passed to the constructor due to which the Activity containing ListView is displaying nothing.
Replace the constructor with the following code
public CustomAdapter(Context context, ArrayList<String> nam,
ArrayList<String> date, ArrayList<String> time) {
super();
this.context = context;
this.nam.addAll(nam);
this.date.addAll(date);
this.time.addAll(time);
inflater =LayoutInflater.from(context);
}
I think this will solve your problem.

Related

How to fix EditText part from ListView inside fragment?

Wanna make to search with using EditText when I click it from ListView. I made it with some youtube videos or blogs. Mixed up those and of course, There's error. What should fix? I put my every codes to understand it. Need you guys help a lot.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Playbook">
<ListView
android:id="#+id/listView2"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_baseline_search_24"
android:id="#+id/searchImage" />
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_toRightOf="#id/searchImage"
android:id="#+id/editTextFilter"/>
<TextView
android:id="#+id/termName"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editTextFilter"
style="#style/TextAppearance.AppCompat.Title"
android:text="Term"/>
</RelativeLayout>
This is layout part.
package com.example.gridiron;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
/**
* A simple {#link Fragment} subclass.
* Use the {#link Playbook#newInstance} factory method to
* create an instance of this fragment.
*/
public class Playbook extends Fragment {
ArrayList<PlaybookList> arrayList2;
ListView listView2;
private static PlaybookListAdapter playbookListAdapter;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public Playbook() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment Playbook.
*/
// TODO: Rename and change types and number of parameters
public static Playbook newInstance(String param1, String param2) {
Playbook fragment = new Playbook();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_playbook, container, false);
ListView listView2 = (ListView) view.findViewById(R.id.listView2);
EditText editTextFilter = (EditText) view.findViewById(R.id.editTextFilter);
ArrayList<PlaybookList> arrayList2 = new ArrayList<>();
arrayList2.add(new PlaybookList("Quarterback", "https://namu.wiki/w/%EC%BF%BC%ED%84%B0%EB%B0%B1"));
arrayList2.add(new PlaybookList("Runningback", "https://namu.wiki/w/%EB%9F%AC%EB%8B%9D%EB%B0%B1"));
PlaybookListAdapter playbookListAdapter = new PlaybookListAdapter(getActivity(), R.layout.list_row2, arrayList2);
listView2.setAdapter(playbookListAdapter);
editTextFilter.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable edit) {
String filterText = edit.toString();
if (filterText.length() > 0) {
listView2.setFilterText(filterText);
} else {
listView2.clearTextFilter();
}
}
});
listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.valueOf(arrayList2.get(position).getTermURL())));
getActivity().startActivity(intent);
}
});
return view;
}
}
package com.example.gridiron;
public class PlaybookList {
String TermName;
String TermURL;
public PlaybookList(String termName, String termURL) {
TermName = termName;
TermURL = termURL;
}
public String getTermName() {
return TermName;
}
public void setTermName(String termName) {
TermName = termName;
}
public String getTermURL() {
return TermURL;
}
public void setTermURL(String termURL) {
TermURL = termURL;
}
}
package com.example.gridiron;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class PlaybookListAdapter extends ArrayAdapter<PlaybookList> {
private Context mContext;
private int mResource;
public PlaybookListAdapter(#NonNull Context context, int resource, #NonNull ArrayList<PlaybookList> objects) {
super(context, resource, objects);
this.mContext = context;
this.mResource = resource;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(mResource, parent, false);
TextView termName = convertView.findViewById(R.id.termName);
termName.setText(getItem(position).getTermName());
return convertView;
}
}
And last 3 codes are for class. It's really hard to make it. Please, Help me!
There is no ListView in your layout code. You didn't include the error but I'm pretty sure it's NullPointerException on this line:
listView2.setAdapter(playbookListAdapter);

ListView with multiple layouts - how to keep each row in a given layout

I have the following layouts:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textGoesHere"
android:drawableRight="#drawable/row_receive"
android:text="hello alert" />
</RelativeLayout>
and
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textGoesHere"
android:drawableLeft="#drawable/row_send"
android:text="hello alert" />
</LinearLayout>
I am trying to create a ListView that displays these two layouts. Here is my java code, which features the main class, a Message class that stores data from an EditText that the user fills out and which is supposed to populate the ListView, and finally, the custom Adapter class:
package com.example.androidlabs;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ChatRoomActivity extends AppCompatActivity {
List<String> listElements = new ArrayList<String>();
ListView theListView;
Button sendButton;
Button receiveButton;
EditText chatText;
Adapter adapter;
Message message = new Message();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_room);
theListView = (ListView) findViewById(R.id.theListView);
sendButton = (Button) findViewById(R.id.sendButton);
chatText = (EditText) findViewById(R.id.chatText);
receiveButton = (Button) findViewById(R.id.receiveButton);
theListView.setAdapter(adapter = new Adapter());
// sendButton.setOnClickListener(click -> listElements.add("new element"));
sendButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
message.setMessage(chatText.getText().toString());
message.setType(0);
listElements.add(chatText.getText().toString());
adapter.notifyDataSetChanged();
}
});
receiveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
message.setMessage(chatText.getText().toString());
message.setType(1);
listElements.add(chatText.getText().toString());
adapter.notifyDataSetChanged();
}
});
}
public class Message {
private String text;
private int type;
public void setMessage(String text) { this.text = text; }
public String getMessage() { return text; }
public void setType(int type) { this.type = type; }
public int getType() {return type; }
}
public class Adapter extends BaseAdapter {
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getItemViewType(int position) {
if (message.getType() == 0) { return 0; }
else { return 1; }
}
#Override
public int getCount() {
return listElements.size();
}
#Override
public Object getItem(int position) {
return listElements.get(position);
}
#Override
public long getItemId(int position) {
return (long) position;
}
#Override
public View getView(int position, View old, ViewGroup parent) {
View newView = old;
LayoutInflater inflater = getLayoutInflater();
int listViewItemType = this.getItemViewType(position);
//make a new row
if (newView == null) {
if (listViewItemType == 0) {
newView = inflater.inflate(R.layout.row_send_layout, parent, false);
}
else {
newView = inflater.inflate(R.layout.row_receive_layout, parent, false);
}
}
//set what the text should be for this row:
TextView tView = newView.findViewById(R.id.textGoesHere);
tView.setText( getItem(position).toString() );
//return it to be put in the table
return newView;
}
}
}
I want the list view to look like this:
The ListView I want
But what ends up happening is it changes all of the layouts of the ListView depending on the button the user clicks on:
The ListView I get if I click on "Send"
The ListView I get if I click on "Recieve"
What can I adjust in my code to get the desired outcome? I'm assuming it's something in my getView() method, but I can't figure out what that is.
You are constantly using the same reference to Message:
Message message = new Message();
So when a new message gets sent, it also overrides the old message completely. You saved your text by storing it in the ArrayList above, so when the layout gets refreshed, it just loads the texts from there, but the message type is read from the single message over and over again...
To fix it, you need the Message to be in an Array/ArrayList as well. When doing that, I'd also remove the listElements-Arraylist, as it just stores the same messages the message itself stores as well... Storing the same data twice is a bad practice, as this can lead to problems when changing only one of them.
So instead of calling the setMessage and setType method in your onClickListeners, create a new instance of Message, populate it with data and store it in an ArrayList. Then, in your getViewand getItemViewType functions of the adapter, get the required message from the position-parameter passed in.

Expandable Listview OnClickListeners

I am creating an app in android studios that requires the user to select a child item from an expandable listview on one activity and for it to be displayed on another activity. I'm new to java and android studios and still dont understand how to do this really. If anyone has any sample code or can help that would be great, thanks!
You should use the setOnChildClickListener method from the ExpandableListView class to create a ClickListener for the list elements and then use a Intent to open the new activity. The call to set the listener should be set after you've set the adapter for the ListView.
Here there's a tutorial for using a ExpandableListView and it shows how to set a ClickListener for the elements.
And here there's the Android developer's documentation which shows how to create and open a new Activity from a button's click.
**You should use the setOnChildClickListener method from the ExpandableListView class to create a ClickListener for the list elements and then use a Intent to open the new activity. The call to set the listener should be set after you've set the adapter for the ListView and create the xml file for child and parent.child will be Expandable TextView **
**layout file**
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.elite_android.explistview1.MainActivity">
<RelativeLayout
android:id="#+id/main_number_picker_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="17dp">
<NumberPicker
android:id="#+id/main_number_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Next" />
<TextView
android:id="#+id/main_txt_patient_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/main_number_picker"
android:text="Select number of patient"
android:textColor="#color/colorAccent"
android:layout_marginLeft="80dp"/>
</RelativeLayout>
<ExpandableListView
android:id="#+id/exp_Listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/main_number_picker_layout">
</ExpandableListView>
</RelativeLayout>
**Main Activity.java file**
package com.example.elite_android.explistview1;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.view.View.OnFocusChangeListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ListView;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.NumberPicker;
public class MainActivity extends AppCompatActivity {
ExpandableListView expandableListView;
private MyAdapter mAdapter;
ExpandableListView expand;
NumberPicker numberPicker;
private List<String> headerItems;
private ArrayList childDetails;
private HashMap<String, List<String>> childList;
private Context ctx;
EditText editText1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get reference to the EditText
editText1 = (EditText) findViewById(R.id.child_item);
// //set the onFocusChange listener
// // editText1.setOnFocusChangeListener(editText1.getOnFocusChangeListener());
//
//// //get reference to EditText
//// editText2 = (EditText) findViewById(R.id.sequence);
//// // set the on focusChange listner
//// editText2.setOnFocusChangeListener(editText2.getOnFocusChangeListener());
//
// //Generate list View from ArrayList;
//
//
// EditText editText = (EditText) findViewById(R.id.child_item);
// editText.requestFocus();
// InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
//
//
//// editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
// // #Override
// // public void onFocusChange(View v, boolean hasFocus) {
// // //editText.setOnFocusChangeListener();
// // return;
// // }
// //});
//
String[] numbers = new String[10];
for (int count = 0; count < 10; count++)
numbers[count] = String.valueOf(count);
numberPicker = (NumberPicker) findViewById(R.id.main_number_picker);
numberPicker.setMaxValue(numbers.length);
numberPicker.setMinValue(1);
numberPicker.setDisplayedValues(numbers);
numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
generateListItems(newVal);
loadRecycleView();
}
});
}
private void generateListItems(int childCount) {
if (headerItems != null)
headerItems.clear();
else
headerItems = new ArrayList<>();
if (childList != null)
childList.clear();
else
childList = new HashMap();
if (childDetails == null) {
childDetails = new ArrayList();
childDetails.add("Name");
childDetails.add("Age");
childDetails.add("Gender");
}
// Put header items
for (int count = 0; count < childCount; count++) {
headerItems.add(" Parent " + count);
childList.put(headerItems.get(count), childDetails);
}
}
private void loadRecycleView() {
if (expandableListView == null) {
expandableListView = (ExpandableListView) findViewById(R.id.exp_Listview);
mAdapter = new MyAdapter(this, headerItems, childList);
expandableListView.setAdapter(mAdapter);
} else {
expandableListView.invalidate();
mAdapter.setHeaderData(headerItems);
mAdapter.setListData(childList);
mAdapter.notifyDataSetChanged();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
}
**Adapter class.java**
package com.example.elite_android.explistview1;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.EditText;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.view.View.OnFocusChangeListener;
/**
* Created by ELITE-ANDROID on 28-02-2017.
*/
// this is provides all needs methods implementing an Expandable List VIew
public class MyAdapter extends BaseExpandableListAdapter {
// We need some Variables here so therer are some Variables here
private List<String> header_titles; // This is for Representing the HeadLine(Array list)
private HashMap<String, List<String>> child_titles; // defin the HashMap for the child item how to Represent the Parent Handing so Hash Map, need some Variables
private Context ctx;
private NumberPicker numberPicker;
EditText editText;
View view;
private static LayoutInflater inflater = null;
//for initalized for all Variables we need some Constructor
public MyAdapter(Context context, List<String> header_titles, HashMap<String, List<String>> child_titles) {
super();
this.ctx = context;
this.child_titles = child_titles;
this.header_titles = header_titles;
}
public void refreshHeader_titles(List<String> events) {
this.header_titles.clear();
this.header_titles.addAll(header_titles);
notifyDataSetChanged();
}
;
#Override
// From this Override method How to Return how many elements are the Group count like parent
public int getGroupCount() {
return header_titles.size();
}
#Override
//here the number of child items are in each heading. There are three Heading - PAtien Name ,Age, Gender
public int getChildrenCount(int groupPosition) {
// Log.d("xxx", )
return child_titles.get(header_titles.get(groupPosition)).size(); //how to Return the size of HashMap
}
#Override
public Object getGroup(int groupPosition) {
return header_titles.get(groupPosition);
}
#Override
// here how to retuen child items on the particular headings and Positions.
public Object getChild(int groupPosition, int childPosition) {
return child_titles.get(header_titles.get(groupPosition)).get(childPosition);
}
#Override
//return the groupo position
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
// Here return the Group View
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
// ListViewHolder viewHolder;
// if (view == null) {
// viewHolder = new ListViewHolder();
// LayoutInflater inflater = context.getLayoutInflater();
// view = inflater.inflate(R.layout.listitems, null, true);
// viewHolder.itmName = (TextView) view.findViewById(R.id.Item_name);
// viewHolder.itmPrice = (EditText) view.findViewById(R.id.Item_price);
// view.setTag(viewHolder);
// } else {
// viewHolder = (ListViewHolder) view.getTag();
//// loadSavedValues();
// }
// Here how to get the Heading Title here Decalered String Variables, now how to get title of heading from getGroup methods so simple call Backed Methods.
String title = (String) this.getGroup(groupPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.prent, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.heading_item);
textView.setText(title); // for Heading bold style ,title
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String title = (String) this.getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child, null);
}
String cellData = child_titles.get(header_titles.get(groupPosition)).get(childPosition);
EditText childItem = (EditText) convertView.findViewById(R.id.child_item);
childItem.setHint(cellData);
childItem.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// Log.d("xxxx", "Has focus " + hasFocus);
if (!hasFocus) {
// int itemIndex = View.getId();
// String enteredName = ((EditText)v).getText().toString();
// selttems.put(itemIndex, enteredName);
} else {
}
return;
}
});
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public void setListData(HashMap<String, List<String>> lData) {
child_titles = lData;
}
public void setHeaderData(List<String> hData) {
header_titles = hData;
}
}
**child.Xml file**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#ffff">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/child_item"
android:textSize="25dp"
android:textStyle="bold"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"/>
<!--<EditText-->
<!--android:id="#+id/sequence"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_alignParentLeft="true"-->
<!--android:layout_alignParentTop="true"-->
<!--android:paddingLeft="35sp"-->
<!--android:layout_marginRight="10dp"-->
<!--android:textAppearance="?android:attr/textAppearanceMedium" />-->
</LinearLayout>
<!--This Layout File Represent the Child Items.
This Field Represent the Child Item IN the List VIew -->
**parent.Xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical">
<TextView
android:id="#+id/heading_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:textColor="#color/colorAccent"
android:textSize="25dp" />
</LinearLayout>
<!--This Layout file for Heading -Lines.-->

How can a click Listener in a custom ListAdapter modify the lists data?

I'm trying to make a relatively simple filesystem browser that displays directories and lets you select them. I have a list fragment which uses a custom adapter to display the directories as a list. I have created a clickListener for each entry in the list. I need to get it so that when a list entry is clicked, the entire list view refreshes. As this click listener is defined within my adapter though, how can it signal up the stack somehow to the list fragment, in order to tell it to refresh the data that the adapter uses. Help much appreciated.
Here is my code:
PickerFragment.java:
package com.grid.picker;
import android.content.Intent;
import android.os.Bundle;
//import android.support.v4.app.Fragment;
import android.os.Environment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
import static android.content.Intent.getIntent;
public class PickerFragment extends ListFragment
{
private String currentFilesystemPath = ""; // This path does not include the root path
private int numberOfFolders = 0;
private List<Folder> folders = new ArrayList<Folder>();
public PickerFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
private void tempPopulate()
{
folders.add(new Folder("TestFolder", "/test/path"));
}
private void populateFolders()
{
// Create a new File at the current path
File currentFolder = new File(Environment.getExternalStorageDirectory().getPath()+currentFilesystemPath);
Utility.debugOutput("populateFolders() currentFolder: " +currentFolder);
// Lets get the list of folders
String[] directories = currentFolder.list(new FilenameFilter() {
#Override
public boolean accept(File current, String name) {
return new File(current, name).isDirectory();
}
});
// Build up the list of folders with folder objects
for(int i=0; i<directories.length;i++)
{
//Utility.debugOutput("Folder:" +directories[i].toString());
folders.add(new Folder(directories[i].toString(), currentFolder.getPath()+directories[i].toString()));
}
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
// Temporary folder population
//tempPopulate();
populateFolders();
/*
// Set an adapter for this list fragment to use
setListAdapter(new ArrayAdapter<Folder>(getActivity(),
android.R.layout.simple_list_item_activated_1, folders));
*/
// We want to use our custom adapter. We pass in this activity, the layout to use, and an array of folders
// We give it a new Folder object just so it knows what type of objects are in the array...??
FolderAdapter adapter = new FolderAdapter(this.getActivity(), R.layout.listview_item_row, folders.toArray(new Folder[0]));
// Use our custom adapter for the list
setListAdapter(adapter);
}
}
FolderAdapter.java:
package com.grid.picker;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class FolderAdapter extends ArrayAdapter<Folder>
{
Folder[] folders = null;
Context context;
int layoutResourceId;
// Constructor
// Param 1: Reference of the activity
// Param 2: The resource id of the layout file we want to use for displaying each ListView item
public FolderAdapter(Context context, int layoutResourceId, Folder[] data)
{
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.folders = data;
}
// This is a function that the click listener can call to start the list update process
public void updateList(String name)
{
Utility.debugOutput("updateList() rx: " +name);
this.notifyDataSetChanged();
}
// Presumably getView is called for every row in the list ?
// position is the index of the list item in the list
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
// Create a new view to play with
View row = convertView;
final int rowID = position;
FolderHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
// Inflate (parse) the layout XML
row = inflater.inflate(layoutResourceId, parent, false);
//Instantiate a new static object for folder icons (static for speed (caching)
holder = new FolderHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
row.setTag(holder);
// Set clickListener
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Utility.debugOutput("Row ID clicked: " +rowID);
Utility.debugOutput("Folder array:" +folders[rowID].folderName);
updateList(folders[rowID].folderName);
//Toast.makeText(context,"you clicked item: "+rowID, Toast.LENGTH_LONG.show();
//code you want to execute on click of list item...
}
});
}
else
{
holder = (FolderHolder)row.getTag();
}
Folder folder = folders[position];
holder.txtTitle.setText(folder.folderName);
holder.txtTitle.setText(folder.folderName);
holder.imgIcon.setImageResource(folder.icon);
/*
// Add click listener to the view
View.OnClickListener clickListener = new View.OnClickListener() {
public void onClick(View v) {
Utility.debugOutput("Something clicked???!");
// This call requires API level 15 minimum...
v.callOnClick();
}
}; */
//row.setOnClickListener(clickListener);
return row;
}
// This class will be used as a cache for the folder images
static class FolderHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}
Folder.java:
package com.grid.picker;
public class Folder
{
// Each instance of this class will be an entry in the folder list
public String folderName;
public String fullPath;
public boolean hasChildren = false; // False by default
public static final int icon = R.drawable.folder;
// Constructor
public Folder(String newName, String newFullPath)
{
folderName = newName;
fullPath = newFullPath;
}
}
listview_item_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<ImageView android:id="#+id/imgIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<TextView android:id="#+id/txtTitle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:textStyle="bold"
android:textSize="22dp"
android:textColor="#000000"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
</LinearLayout>
Eventually I am going to need to put a checkbox on each folder displayed in the list, but for now I need to get it so the folders shown in the list can be clicked on to refresh the display and update the list. Many thanks.
use this on your click listener
// Set clickListener
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Utility.debugOutput("Row ID clicked: " +rowID);
Utility.debugOutput("Folder array:" +folders[rowID].folderName);
notifyDataSetInvalidated();
}
});

Uri argument and LoaderCallbacks

I have four class :
MainActivity.java - MyAdapter.java - MyDataBase.java - MyListActivity.java
now, what is Uri in the MyListActiviy ? How can I fill it ?
when I Click "showlist_btn" button in MainActivity, a list without content is displayed ! why ?
MainActivity:
package com.example.ex22;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText edittext ;
Button add_btn ;
Button showlist_btn ;
MyDataBase mydatabase ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydatabase = new MyDataBase(MainActivity.this);
edittext = (EditText)findViewById(R.id.edittext);
showlist_btn = (Button)findViewById(R.id.showlist_btn);
add_btn = (Button)findViewById(R.id.add_btn);
add_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str = edittext.getText().toString();
mydatabase.addName(str);
edittext.setText("");
}
});
showlist_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, MyListActivity.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
MyAdapter:
package com.example.ex22;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
List<String> mylist ;
LayoutInflater inflater ;
public MyAdapter(Context context){
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mylist.size();
}
void getList(List<String> list){
mylist = list ;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewholder ;
if(convertView == null){
convertView = inflater.inflate(R.layout.row_for_list, null);
viewholder = new ViewHolder();
viewholder.text = (TextView)convertView.findViewById(R.id.textview_forrow);
convertView.setTag(viewholder);
}else {
viewholder = (ViewHolder)convertView.getTag();
}
viewholder.text.setText(mylist.get(position));
return convertView;
}
static class ViewHolder{
TextView text ;
}
}
MyDataBase:
package com.example.ex22;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDataBase extends SQLiteOpenHelper {
private final static String DB_NAME = "mydb" ;
private final static String TABLE_NAME = "mytable" ;
//private static final String NAME = "name" ;
private final static String NAME_COLOUM = "_name" ;
private static final String CREATE_TABLE = "CREATE TABLE "+ TABLE_NAME + "("
+ NAME_COLOUM + " TEXT" + ")";
public MyDataBase(Context context){
super(context,DB_NAME,null,1);
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
}
public void addName(String str){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NAME_COLOUM, str);
db.insert(TABLE_NAME, null, values);
db.close();
}
public List<String> getAllNames(){
List<String> list = new ArrayList<String>() ;
SQLiteDatabase db = this.getWritableDatabase();
Cursor c = db.rawQuery("SELECT * FROM "+ TABLE_NAME, null);
if(c.moveToFirst()){
do{String str = c.getString(0);
list.add(str);
}while(c.moveToNext());
}
return list ;
}
public String getCulomName(){
return this.NAME_COLOUM;
}
}
MyListActivity:
package com.example.ex22;
import android.app.ListActivity;
import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;
public class MyListActivity extends ListActivity
implements LoaderCallbacks<Cursor>{
SimpleCursorAdapter simplecursorradapter ;
MyDataBase mydb ;
CursorLoader cursorloader ;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setContentView(R.layout.show_list);
mydb = new MyDataBase(this);
String[] from = new String[] { mydb.getCulomName() };
int[] to = new int[]{
R.id.forlist
};
simplecursorradapter = new SimpleCursorAdapter(this, android.R.layout.simple_expandable_list_item_2, null,
from , to ,0);
setListAdapter(simplecursorradapter);
LoaderManager loadermanager = getLoaderManager();
loadermanager.initLoader(0, null, this);
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// TODO Auto-generated method stub
String[] projection = {mydb.getCulomName()};
final String SCHEME = "content";
// The provider's authority
final String AUTHORITY = "com.example.ex22";
final Uri src = Uri.parse(SCHEME + "://" + AUTHORITY);
Uri uri = Uri.withAppendedPath(src, "mytable");
cursorloader = new CursorLoader(this, uri, projection, null, null, null);
return cursorloader;
}
#Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
// TODO Auto-generated method stub
simplecursorradapter.swapCursor(arg1);
}
#Override
public void onLoaderReset(Loader<Cursor> arg0) {
// TODO Auto-generated method stub
simplecursorradapter.swapCursor(null);
}
}
activity_main :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<EditText
android:id="#+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Write Your Name..."
/>
<Button
android:id="#+id/add_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add Name"
/>
<Button
android:id="#+id/showlist_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Show Name List"
/>
</LinearLayout>
forlist:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/forlist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
row_for_list:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textview_forrow"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="40sp"
/>
</LinearLayout>
show_list:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

Categories

Resources