I am creating an android application in which there is a scenario where i have to add new items to the grid i am using an layout inflater to add new items to the grid the items gets added to the database sucessfully but the grid view is not refreshed once the item is added i have used notify dataset changed but it is not working can anyone tell me what i have to change in the existing code
Activity for the gridview:
listet = databaseHandlerOtherchgs.getAllproducttitle();
listet.add(new OtherChargesType("Plucking", bitMapData));
listet.add(new OtherChargesType("Loading", bitMaploading));
listet.add(new OtherChargesType("Add New", bitMapaddnew));
listcharges = new ArrayList<CustomizedCharge>());
adapter = new OtherChargesGridAdpater(SinglePageTransaction.this, listet);
gv.setAdapter(adapter);
gv.setExpanded(true);
GridAdapter:
public class OtherChargesGridAdpater extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<OtherChargesType> otherchargestypes;
DatabaseHandlerOtherChgs databaseHandlerOtherchgs;
public OtherChargesGridAdpater(Activity activity, List<OtherChargesType> otherchargestypes) {
this.activity = activity;
this.otherchargestypes = otherchargestypes;
}
#Override
public int getCount() {
return otherchargestypes.size();
}
#Override
public Object getItem(int i) {
return otherchargestypes.get(i);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertview, ViewGroup parent) {
if (inflater == null) {
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (convertview == null) {
convertview = inflater.inflate(R.layout.row_othercharges_griditem, null);
}
OtherChargesType m = otherchargestypes.get(position);
byte[] outImage = m.getImage();
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
final Bitmap theImage = BitmapFactory.decodeStream(imageStream);
ImageView otherimages = (ImageView) convertview.findViewById(R.id.imageView1);
final TextView typename = (TextView) convertview.findViewById(R.id.textView1);
databaseHandlerOtherchgs=new DatabaseHandlerOtherChgs(activity);
// getting movie data for the row
typename.setText(m.getTypename());
otherimages.setImageBitmap(theImage);
notifyDataSetChanged();
return convertview;
}
public void updatedata(){
DatabaseHandlerOtherChgs databaseHandlerOtherChgs =new DatabaseHandlerOtherChgs(activity);
databaseHandlerOtherChgs.getAllproducttitle();
this.notifyDataSetChanged();
}
}
Use This code in Activity.. This code store your data.
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putString("MSG", "This is my message to be reloaded");
super.onSaveInstanceState(outState);
}
In Oncreate Method put this code
if (savedInstanceState != null) {
String str= savedInstanceState.getString("MSG");
Toast.makeText(this, str, Toast.LENGTH_LONG).show();
}
And for refresh your activity use follow code
finish();
startActivity(getIntent());
Make Changes as per your requirement.
Related
Im new in realm db. I completed add and get data in realm db. But, I couldn't sort(ascending and descending).Im my code it display items in listview. Listview contains 5 list and each list contains 4 field(name, age, skill and date). if I sort(ascending) name, need to ascending in 5 list.My code is not work
I post my code here,
private void Ascending_order() {
realm.beginTransaction();
RealmResults<Employee> result = realm.where(Employee.class)
.sort("name", Sort.ASCENDING).findAll();
realm.copyFromRealm(result);
realm.commitTransaction();
employeedetailadapter.notifyDataSetChanged();
}
Adapter class:
public class EmployeeDetailAdapter extends BaseAdapter {
private ArrayList<Employee>employeeDetaillists = new ArrayList<>();
private Context c;
private LayoutInflater inflater;
private OnItemClick mCallback;
private SimpleDateFormat df = new SimpleDateFormat("dd/mm/yyyy");
public EmployeeDetailAdapter(Context c,ArrayList<Employee> employeeDetaillists, OnItemClick listener) {
this.employeeDetaillists = employeeDetaillists;
this.c= c;
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mCallback = listener;
}
#Override
public int getCount() {
return employeeDetaillists.size();
}
#Override
public Object getItem(int position) {
return employeeDetaillists.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
Holder holder;
if (v==null){
v= (View) inflater.inflate(R.layout.list_single_item,null);
holder = new Holder();
holder.tvPersonName = (TextView) v.findViewById(R.id.tvPersonName);
holder.tvPersonAge = (TextView) v.findViewById(R.id.tvPersonAge);
holder.tvPersonSkill = (TextView) v.findViewById(R.id.tvPersonSkill);
holder.ivEditPesonDetail=(ImageView)v.findViewById(R.id.ivEditPesonDetail);
holder.tvPersondate=(TextView)v.findViewById(R.id.tvPersondate);
holder.ivDeletePerson=(ImageView)v.findViewById(R.id.ivDeletePerson);
v.setTag(holder);
}else{
holder = (Holder) v.getTag();
}
holder.tvPersonName.setText(employeeDetaillists.get(position).getName());
holder.tvPersonAge.setText(employeeDetaillists.get(position).getAge());
holder.tvPersonSkill.setText(employeeDetaillists.get(position).getSkill());
String strDate = df.format(employeeDetaillists.get(position).getSdate());
holder.tvPersondate.setText(strDate);
holder.ivDeletePerson.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Delete(employeeDetaillists.get(position).getName(),position);
}
});
return v;
}
private void Delete(String name, int position) {
mCallback.onClickdelete(name, position);
}
public void updateData(RealmResults<Employee> result) {
}
class Holder {
TextView tvPersonName, tvPersonAge, tvPersonSkill,tvPersondate;
ImageView ivDeletePerson, ivEditPesonDetail;
}
}
Your code does't change db. You just get sorted items but don't use them.
realm.copyFromRealm(result); // this line does nothing
realm.commitTransaction(); // this one too, because you change nothing
employeedetailadapter.notifyDataSetChanged(); // you data is the same, so this line also useless here
To see your data sorted you should use RealmResults in your adapter. With this approach your list always will sorted, even after adding new items. But note: your adapter should extends RealmRecyclerViewAdapter.
You should run this code before creating adapter and use result inside adapter:
RealmResults<Employee> result = realm.where(Employee.class)
.sort("name", Sort.ASCENDING).findAll();
Also you can try manually update data of your adapter.
private void Ascending_order() {
RealmResults<Employee> result = realm.where(Employee.class)
.sort("name", Sort.ASCENDING).findAll();
employeedetailadapter.updateData(result); // update data inside adapter before calling `notifyDataSetChanged`
employeedetailadapter.notifyDataSetChanged();
}
You need to create updateData method yourself:
public void updateData(RealmResults<Employee> result) {
employeeDetaillists = new ArrayList<Employee>(result);
}
First of all, while getting data from Realm you don't need to write it in Transaction. Write Transaction is required only when you are adding data in realm or updating any realm object.
And about your problem, To get sorted data from realm, You can do it like this
RealmResults<Employee> result = realm.where(Employee.class).sort("name", Sort.ASCENDING).findAll();
Now the data you got is sorted, If you still see wrong order in your ListView then there could be some issue in your Adapter. If you share your adapter code, then I can help further :)
Updated:
Adapter Class
public class EmployeeDetailAdapter extends BaseAdapter {
private RealmResults<Employee> employeeDetaillists;
private Context c;
private LayoutInflater inflater;
private OnItemClick mCallback;
private SimpleDateFormat df = new SimpleDateFormat("dd/mm/yyyy");
public EmployeeDetailAdapter(Context c,RealmResults<Employee> employeeDetaillists, OnItemClick listener) {
this.employeeDetaillists = employeeDetaillists;
this.c= c;
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mCallback = listener;
}
#Override
public int getCount() {
return employeeDetaillists.size();
}
#Override
public Object getItem(int position) {
return employeeDetaillists.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
Holder holder;
if (v==null){
v= (View) inflater.inflate(R.layout.list_single_item,null);
holder = new Holder();
holder.tvPersonName = (TextView) v.findViewById(R.id.tvPersonName);
holder.tvPersonAge = (TextView) v.findViewById(R.id.tvPersonAge);
holder.tvPersonSkill = (TextView) v.findViewById(R.id.tvPersonSkill);
holder.ivEditPesonDetail=(ImageView)v.findViewById(R.id.ivEditPesonDetail);
holder.tvPersondate=(TextView)v.findViewById(R.id.tvPersondate);
holder.ivDeletePerson=(ImageView)v.findViewById(R.id.ivDeletePerson);
v.setTag(holder);
}else{
holder = (Holder) v.getTag();
}
holder.tvPersonName.setText(employeeDetaillists.get(position).getName());
holder.tvPersonAge.setText(employeeDetaillists.get(position).getAge());
holder.tvPersonSkill.setText(employeeDetaillists.get(position).getSkill());
String strDate = df.format(employeeDetaillists.get(position).getSdate());
holder.tvPersondate.setText(strDate);
holder.ivDeletePerson.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Delete(employeeDetaillists.get(position).getName(),position);
}
});
return v;
}
private void Delete(String name, int position) {
mCallback.onClickdelete(name, position);
}
public void updateData(RealmResults<Employee> result) {
}
class Holder {
TextView tvPersonName, tvPersonAge, tvPersonSkill,tvPersondate;
ImageView ivDeletePerson, ivEditPesonDetail;
}
}
In your Activity please change following function
private void Ascending_order() {
result = realm.where(Employee.class)
.sort("name", Sort.ASCENDING).findAll();
employeedetailadapter.notifyDataSetChanged();
}
"result" list should be declared on class level and should be passed to Adapter's constructor as well.
Like
class Activity {
RealmResults<Employee> result;
EmployeeDetailAdapter employeedetailadapter;
//// Other Code
public onCreate(Bundle b) {
result = realm.where(Employee.class).findAll();
employeedetailadapter = new EmployeeDetailAdapter(this, result, listener);
// Other code
}
}
I'm using view pager with swiping tab layouts. And i'm displaying list view of data using custom adapter. And also onclick of list view i have a list view detail activity where I'm displaying data in more detail. In these detail activity i'm performing some changes to the data(some post method). after that I create an instance of customAdapter class and call notifyDataSetChanged() in order to refresh list view. My problem over here is the list view some times refreshes quickly and some times there is a delay of some seconds.
So, Can somebody suggest me proper usage of list view and what changes needs to be done in order to refresh list view whenever a post method is performed.
My code Fragment class:
private void showJsonData(String response) {
try {
String serviceID = LoggedInUserStore.getLoggedInServiceId(getContext());
List<Complaint> userList = new ArrayList<>(); //ArrayList of type user(POJO CLASS)
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
if (serviceID.equals(jsonArray.getJSONObject(i).getString("ServiceID"))) {
if (jsonArray.getJSONObject(i).getString("CallStatusID").equalsIgnoreCase("1")) {
userList.add(0, Complaint.fromJson(jsonArray.getJSONObject(i))); //
}
}
}
assignAdapter = new AssignAdapter(getActivity(), userList);
listView.setAdapter(assignAdapter);
listView.invalidateViews();
assignAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
My custom adapter class
public class AssignAdapter extends BaseAdapter implements Filterable {
List<Complaint> ticket = new ArrayList<>();
private Context context;
String ticketNo, complaint, raiseDate;
Complaint user;
List<Complaint> temporaryList = new ArrayList<>();
/*String status, priority;*/
public AssignAdapter(Context context, List<Complaint> ticket) {
this.context = context;
this.ticket = ticket;
this.temporaryList = ticket;
}
#Override
public int getCount() {
return temporaryList.size();
}
#Override
public Object getItem(int position) {
return temporaryList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public class viewHolderItem {
TextView ticketNumberText, complaintNameText, raisedDateText;
}
//Set the layout for the fragment and return it.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
viewHolderItem holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.custom_list_view, null, true);
holder = new viewHolderItem();
holder.ticketNumberText = (TextView) convertView.findViewById(R.id.ticketIdSupervisor);
holder.complaintNameText = (TextView) convertView.findViewById(R.id.complaintNameSupervisor);
convertView.setTag(holder);
} else {
holder = (viewHolderItem) convertView.getTag();
}
user = temporaryList.get(position);
if (user != null) {
//Get the Ticket Number
Typeface custom_font = Typeface.createFromAsset(context.getAssets(), "fonts/DroidSerif.ttf");
ticketNo = temporaryList.get(position).getTicketNumber();
holder.ticketNumberText.setText(ticketNo);
holder.ticketNumberText.setTag("ticketNumber");
holder.ticketNumberText.setTypeface(custom_font);
//Get the complaint Name
complaint = temporaryList.get(position).getComplaintDetails();
holder.complaintNameText.setText(complaint);
holder.complaintNameText.setTag("complaint");
holder.complaintNameText.setTypeface(custom_font);
}
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(context.getApplicationContext(), ComplaintDetailsSupervisor.class);
i.putExtra("COMPLAINT NAME", temporaryList.get(position).getComplaintDetails());
i.putExtra("RAISED DATE", temporaryList.get(position).getRaisedDate().substring(0, 10));
context.startActivity(i);
}
});
notifyDataSetChanged();
return convertView;
}
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
}
My List view detail activity class
dialogButtonOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
assignComplaint();
al.remove(position);
AssignAdapter assignAdapter = new AssignAdapter(getApplicationContext(), al);
assignAdapter.notifyDataSetChanged();
ComplaintDetailsSupervisor.this.finish();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
dialogButtonNo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
});
}
public void assignComplaint() throws JSONException {
//my custom method...
}
In the list view detail activity class i'm doing this
al.remove(position);
AssignAdapter assignAdapter = new AssignAdapter(getApplicationContext(), al);
assignAdapter.notifyDataSetChanged();
ComplaintDetailsSupervisor.this.finish();
Removing the position of list view and immediately calling adapter. This works fine but I don't know why sometimes it does not refreshes..May be when list view has a single item it does not refreshes immediately.
You are creating a new adapter and calling notifyDatasetChanged on it but have not called setAdapter with the new adapter as a parameter, hence why your list ist not refreshed.
You need to call
setAdapter(assignAdapter)
or reuse your existing assignAdapter and then call notifyDatasetChanged() on it.
Am trying to update my listview on every selection of the spinner. but its not working. Instead of getting new data, listview is repeating the same values.
I am unable to find out what is my mistake.
here is my avtivity code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
setContentView(R.layout.activity_performance_details);
PerfList = new ArrayList<PerformanceListItem>();
months = (Spinner) findViewById(R.id.load_month);
listview_performance = (ListView) findViewById(R.id.performance_details_list);
sadapter = new PerformanceAdapter(PerformanceDetails.this, PerfList);
months.setOnItemSelectedListener(this);
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner a=(Spinner)parent;
if(a.getId() == R.id.load_month) {
monthid =1+(int)months.getSelectedItemPosition();
Toast.makeText(getApplicationContext(),""+monthid,Toast.LENGTH_LONG).show();
new setAsyncTask_performance().execute();
}
}
after selecting spinner data it is sent to server and from server its relevant data is fetched and sent back to the list view. now when i first time select the spinner it show the data accordingly. But on second selection it will include the previous data without updating the listview
Adapter Code:
public class PerformanceAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private Context context;
private List<PerformanceListItem> performanceList;
public PerformanceAdapter(Activity activity, List<PerformanceListItem> PList) {
this.activity = activity;
this.performanceList = PList;
}
#Override
public int getCount() {
return performanceList.size();
}
#Override
public Object getItem(int position) {
return performanceList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null) {
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (convertView == null) {
convertView = inflater.inflate(R.layout.performance_itemlist, null);
}
Animation slideUp = AnimationUtils.loadAnimation(activity, R.anim.slide_up);
TextView staffName = (TextView) convertView.findViewById(R.id.perf_staffName);
TextView staffDesignation = (TextView) convertView.findViewById(R.id.perf_Design);
TextView staffPerformance = (TextView) convertView.findViewById(R.id.perf_performance);
PerformanceListItem plist = performanceList.get(position);
staffName.setText(plist.getpStaffName());
staffDesignation.setText(plist.getpDesignation());
staffPerformance.setText(plist.getpPerformance());
slideUp.setDuration(500);
convertView.startAnimation(slideUp);
slideUp = null;
return convertView;
}
}
and this is my performance list to get and set data
PerformanceListItems code:
public class PerformanceListItem {
private String pSid;
private String pStaffName;
private String pDesignation;
private String pPerformance;
private String pList;
public PerformanceListItem(){
}
public PerformanceListItem(String pList){
this.pList = pList;
}
public String getpSid(){
return pSid;
}
public void setpSid(String pSid){
this.pSid = pSid;
}
public String getpStaffName(){
return pStaffName;
}
public void setpStaffName(String pStaffName){
this.pStaffName = pStaffName;
}
public String getpDesignation(){
return pDesignation;
}
public void setpDesignation(String pDesignation){
this.pDesignation = pDesignation;
}
public String getpPerformance(){
return pPerformance;
}
public void setpPerformance(String pPerformance){
this.pPerformance = pPerformance;
}
}
After debugging the entire code i found that my JSONObject is not updating with new value
any help would be appreciable.
Update the data of your adapter when you execute this
new setAsyncTask_performance().execute();
If you want to show only the new data just remove all your listview items then update the data and set the adapter again.
dont set adapter in oncreate. Set your adapter in Asynctask Post execute. and set your array inside doinbackground along with getting data task.
I'm new to android. I'm trying to get my list view to update, I've tried everything...from calling notifydatasetchanged on the ui thread to just simply recreating my list adapter but for whatever reason when I update, no matter which method I use I have to scroll to see the changes. By this I mean that the data updates (say 13:01 changes to 13:02 in the list), it will update, but to see the change I have to scroll so that 13:01 goes off screen and then move back and it will have updated visually.
Why is this? (I can't post code right now as I'm on my phone but if required I will post later.)
EDIT: Here's the relevant code...sorry it took so long I haven't been at my computer for a couple of days.
Relevant parts of ListFragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.match_fragment, container, false);
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
MatchAdapter adapter = (MatchAdapter) this.getListAdapter();
if(futureMatches)
adapter = new MatchAdapter (this.getActivity(), ((MainActivity)this.getActivity()).getMatches(), futureMatches);
else
adapter = new MatchAdapter (this.getActivity(), ((MainActivity)this.getActivity()).getPastMatches(), futureMatches);
setListAdapter(adapter);
}
public void refresh()
{
MatchAdapter adapter;
//Update array in mainactivity
if(futureMatches)
MainActivity.refreshMatches((MainActivity) getActivity());
else
MainActivity.refreshPastMatches((MainActivity) getActivity());
//put updated entries in the adapter
if(futureMatches)
adapter = new MatchAdapter (getActivity(), ((MainActivity)getActivity()).getMatches(), futureMatches);
else
adapter = new MatchAdapter (getActivity(), ((MainActivity)getActivity()).getPastMatches(), futureMatches);
setListAdapter(adapter);
updateList();
}
public void updateList(){
this.getActivity().runOnUiThread(new Runnable() {
public void run() {
((BaseAdapter) getListAdapter()).notifyDataSetChanged();
getListView().refreshDrawableState();
getListView().invalidate();
}
});
}
public void onViewStateRestored(Bundle savedInstanceState)
{
super.onViewStateRestored(savedInstanceState);
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
refresh();
}
My adapter class:
public class MatchAdapter extends BaseAdapter
{
private final Activity context;
private LayoutInflater inflater;
private boolean time = false;
private boolean futureMatchAdapter = true;
private ArrayList<String> matchList;
public MatchAdapter(Context cont, ArrayList<String> matches, boolean isFutureMatchAdapter)
{
matchList = matches;
futureMatchAdapter = isFutureMatchAdapter;
context = (Activity) cont;
inflater = LayoutInflater.from(context);
}
public int getCount()
{
return MatchAdapter.size();
}
#Override
public Object getItem(int position)
{
return MatchAdapter.get(position);
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
String curPos = "";
curPos = MatchAdapter.get(position);
//times, future matches and past matches are handled differently
if(curPos.contains("Last updated:"))
time = true;
else
time = false;
if (convertView == null)
{
holder = new ViewHolder();
if(time)
{
convertView = inflater.inflate(R.layout.time_item, null);
holder.title = (TextView) convertView.findViewById(R.id.item_time);
}
else
{
if(futureMatchAdapter)
{
convertView = inflater.inflate(R.layout.feed_item, null);
holder.title = (TextView) convertView.findViewById(R.id.item_title);
}
else
{
convertView = inflater.inflate(R.layout.past_feed_item, null);
holder.title = (TextView) convertView.findViewById(R.id.item_title_past);
}
}
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
if(futureMatchAdapter)
holder.title.setText(matchList.get(position));
else
{
String matchString = matchList.get(position);
String alwaysVisible = matchString.replace("<", "vs");
alwaysVisible = alwaysVisible.replace(">", "vs");
if(!time)
alwaysVisible = alwaysVisible.substring(0, alwaysVisible.length() - 1);
holder.title.setText(alwaysVisible);
if(matchString.contains(">"))
{
String winner = matchString.substring(0, matchString.indexOf(">")) + "won!";
alwaysVisible = alwaysVisible.concat(winner);
}
else if(matchString.contains("<"))
{
String winner = matchString.substring(matchString.indexOf("<") + 2, matchString.indexOf("\n")) + " won!";
alwaysVisible = alwaysVisible.concat(winner);
}
holder.title.setOnClickListener(new pastMatchesOnclickListener(alwaysVisible)
{
public void onClick(View v)
{
((TextView) v).setText(matchWinner);
}
});
}
return convertView;
}
static class ViewHolder
{
TextView title;
TextView time;
}
}
did you try to update your list using the adapter?
dataadapter.clear();
dataadapter.addAll(allDataResult);
To show and update content in a ListView you should:
Create or find your ListView
Create your ListAdapter
Add your ListAdapter to your ListView
Add data to your ListAdapter
Call notifyDataSetChanged() on your ListAdapter.
Example:
ListView listView = (ListView) findViewById(R.id.listview);
MyListAdapter myListAdapter = new MyListAdapter();
listView.setAdapter(myListAdapter);
myListAdapter.add("Hello");
myListAdapter.add("Hi");
myListAdapter.notifyDataSetChanged();
Note: if you're using a subclass of ArrayAdapter the notifyDataSetChanged() will be called for you when you use the methods add(), addAll(), etc.
I'm trying to make a ListFragment. I looked the Api Demo (FragmentLayout). it works on a simple example and now i want to apply it to my existing project.
Here is my code. I create inner classes (RecipeList & RecipeDetail) as in the Api Demo.
public class InfoActivity extends MenuActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info_fragment_layout);
// ...
}
public static class RecipeList extends ListFragment {
private int mCurrentSelectedItemIndex = -1;
private boolean mIsTablet = false;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
accountData = new ArrayList<Account>();
new AccountSyncTask() {
#Override
public void onPostExecute(
final ArrayList<ArrayList<String>> result) {
// For each retrieved account
Bd.insert(retrievedAccount);
accountData.add(retrievedAccount);
}
accountListAdapter = new AccountListAdapter(
InfoActivity.this, R.layout.accountlist_detail,
accountData);
accountListAdapter = new AccountListAdapter(
activityContext, R.layout.accountlist_detail,
accountData);
setListAdapter(accountListAdapter);
}
}.execute(sessionName, null, "getAllObjectOnServer",
String.valueOf(nbRow));
if (savedInstanceState != null) {
mCurrentSelectedItemIndex = savedInstanceState.getInt(
"currentListIndex", -1);
}
// This is a tablet if this view exists
View recipeDetails = getActivity()
.findViewById(R.id.recipe_details);
mIsTablet = recipeDetails != null
&& recipeDetails.getVisibility() == View.VISIBLE;
if (mIsTablet) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
if (mIsTablet && mCurrentSelectedItemIndex != -1) {
showRecipeDetails();
}
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
mCurrentSelectedItemIndex = position;
showRecipeDetails();
}
private void showRecipeDetails() {
if (mIsTablet) {
// Set the list item as checked
getListView().setItemChecked(mCurrentSelectedItemIndex, true);
// Get the fragment instance
RecipeDetail details = (RecipeDetail) getFragmentManager()
.findFragmentById(R.id.recipe_details);
// Is the current visible recipe the same as the clicked? If so,
// there is no need to update
if (details == null
|| details.getRecipeIndex() != mCurrentSelectedItemIndex) {
details = RecipeDetail
.newInstance(mCurrentSelectedItemIndex);
FragmentTransaction ft = getFragmentManager()
.beginTransaction();
ft.replace(R.id.recipe_details, details);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
} else {
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("currentListIndex", mCurrentSelectedItemIndex);
}
}
public static class RecipeDetail extends Fragment {
private int mRecipeIndex;
public static RecipeDetail newInstance(int recipeIndex) {
// Create a new fragment instance
RecipeDetail detail = new RecipeDetail();
// Set the recipe index
detail.setRecipeIndex(recipeIndex);
return detail;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
View v = inflater
.inflate(R.layout.recipe_details, container, false);
//..
return v;
}
public int getRecipeIndex() {
return mRecipeIndex;
}
public void setRecipeIndex(int index) {
mRecipeIndex = index;
}
}
I have a custom ArrayAdapter (my items in the ListFragment contain 4 textViews and a clickable imageButton).
AccountListAdapter :
public class AccountListAdapter extends ArrayAdapter<Account> {
private final Context context;
private final int layoutResourceId;
private final ArrayList<Account> data;
public AccountListAdapter(Context context, int layoutResourceId,
ArrayList<Account> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
AccountHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
holder = new AccountHolder();
convertView.setClickable(true);
convertView.setFocusable(true);
holder.txtName = (TextView) convertView.findViewById(R.id.nom);
holder.txtId = (TextView) convertView.findViewById(R.id.id);
convertView.setTag(holder);
} else {
holder = (AccountHolder) convertView.getTag();
}
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("click", "index = " + position);
}
});
holder.txtName.setText(data.get(position).getName());
holder.txtId.setText(data.get(position).getId());
convertView.setBackgroundResource(R.drawable.list_selector);
ImageButton img = (ImageButton) convertView.findViewById(R.id.check);
img.setTag(position);
return convertView;
}
static class AccountHolder {
TextView txtName;
TextView txtId;
}
}
Problem :
When i click on an Item of the listFragment,
public void onListItemClick(ListView l, View v, int position, long id) {
mCurrentSelectedItemIndex = position;
Log.i("click", "here";
showRecipeDetails();
}
is not called but the listener on an item defined in AccountListAdapter works
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("click", "index = " + position);
}
});
Why is onListitemClick never called ?
Another question : is it a proper way to consume a web service in another thread in the onActivityCreated function of my ListFragment (in order to populate the list) ?
Thx in advance
EDIT = For the moment i made "showRecipeDetails" static and call it in the listener in my custom ArrayAdapter.
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
RecipeList.showRecipeDetails(position);
}}
I'm not satisfied with my solution, i'm interessed to any other solution
OnItemClickListeners must first be associated with the ListView you want to record clicks for. In your onActivityCreated(..) method, place getListView().setOnItemClickListener(this) somewhere and put implements OnItemClickListener after public static class RecipeList extends ListFragment.