Refreshing Android ListView data inside OnClickListener - android

I've been working on a personal project in Android and ran into a strange situation regarding an Activity which uses a ListView. The basics of the problem are that I have a list of items, each which have 2 buttons, edit and delete. Right now I'm working on implementing the Delete button, which works functionally, but doesn't update the ListView correctly. Instead, it puts what was just deleted on top of the list. It of course refreshes whenever I renavigate to that activity.
Right now the Delete button is detected inside the a custom BaseAdapter, and when I call the notifyDataSetChanged, the situation described above happens instead of removing the now deleted item. How can I correctly update the list inside the adapter class?
I realize that there have been some questions about this, but I haven't been able to integrate them, and solutions I think may work don't really explain how they work; I'm using this project to understand Android app development more, so I would prefer answers with some level of explanation, although any help is of course appreciated!
Thanks!
Here's the relevent code. Note that this is an unfinished project, so there are some unused/incomplete things inside it. Please ignore these.
EditItemsActivity:
package com.example.mybudget;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
public class EditItemsActivity extends Activity implements OnGestureListener{
private DatabaseHandler db;
private List<DataPoint> dpList;
private EditItemsAdapter adapter;
private ListView lv;
private GestureDetector gestureDetector;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_items);
// Show the Up button in the action bar.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
getActionBar().setDisplayHomeAsUpEnabled(true);
db = new DatabaseHandler(this);
dpList = db.allDataThisMonth();
lv = (ListView) findViewById(R.id.edititems);
adapter = new EditItemsAdapter(this, R.id.edititems, dpList);
lv.setAdapter(adapter);
gestureDetector = new GestureDetector(getBaseContext(), this);
// buttonDelete.setVisibility(View.GONE);
}
public void refreshList()
{
adapter.notifyDataSetChanged();
}
#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_edit_items, menu);
return true;
}
#Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
public void onDelete()
{
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY)
{
// Log.d("Swipe", "" + velocityX + ", " + velocityY);
// if(velocityX > 200 && velocityY < 50 && velocityY > -50)
// {
// buttonEdit.setVisibility(View.GONE);
// buttonDelete.setVisibility(View.VISIBLE);
// }
// else if(velocityX < -200 && velocityY < 50 && velocityY > -50)
// {
// buttonDelete.setVisibility(View.GONE);
// buttonEdit.setVisibility(View.VISIBLE);
// }
return false;
}
#Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
}
And here's the adapter class:
package com.example.mybudget;
import java.text.NumberFormat;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
public class EditItemsAdapter extends BaseAdapter implements OnClickListener{
private List<DataPoint> dpList;
private Activity activity;
private DatabaseHandler db;
public EditItemsAdapter(Activity a)
{
activity = a;
}
public EditItemsAdapter(Activity a, int textViewResourceId, List<DataPoint> dpList)
{
super();
this.dpList = dpList;
activity = a;
db = new DatabaseHandler(activity);
}
public static class ViewHolder
{
public TextView item1;
public TextView item2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
//ViewHolder holder;
NumberFormat format = NumberFormat.getCurrencyInstance();
if (v == null)
{
// LayoutInflater vi =
// (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater vi = activity.getLayoutInflater();
v = vi.inflate(R.layout.edit_grid_items, null);
// holder = new ViewHolder();
// holder.item1 = (TextView) v.findViewById(R.id.edit_item_name);
// holder.item2 = (TextView) v.findViewById(R.id.edit_item_cost);
// v.setTag(holder);
TextView tv1 = (TextView)v.findViewById(R.id.edit_item_name);
TextView tv2 = (TextView)v.findViewById(R.id.edit_item_cost);
Button edit = (Button)v.findViewById(R.id.edit_item_button);
Button delete = (Button)v.findViewById(R.id.delete_item_button);
final DataPoint dp = dpList.get(position);
tv1.setText(dp.getName());
tv2.setText(Float.toString(dp.getCost()));
delete.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
db.deleteRowByKey(dp);
((EditItemsActivity) activity).refreshList();
}
});
}
// else
// holder = (ViewHolder)v.getTag();
// if(dp != null)
// {
// holder.item1.setText(dp.getName());
// holder.item2.setText(format.format(dp.getCost()));
// }
return v;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return dpList.size();
}
#Override
public DataPoint getItem(int position) {
// TODO Auto-generated method stub
return dpList.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return dpList.size();
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
EDIT:
The solution involved the inflation explanation that Adam provided, but required a complete repopulation of dpList, the
dpList = db.allDataThisMonth();
that Anup suggested.

In your getView method of your adapter, you're checking if (convertView == null) and if it's null you're inflating a new view. If it's not null, you're just returning the non-null view that was supplied.
The convertView supplied to the getView method is a cached view that has already been displayed. You're supposed to re-use this (if it's a valid view - you may have multiple different views in your list), rather than inflating a new one. You're forgetting to update the content of it for the corresponding position.
So, how to fix it? Simply close your if (v == null) after inflation:
if (v == null)
{
LayoutInflater vi = activity.getLayoutInflater();
v = vi.inflate(R.layout.edit_grid_items, null);
}
Edit: As Anup points out, you also need to update your dpList variable or it will keep returning the same values for the given position. You can do this in your click listener:
delete.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
db.deleteRowByKey(dp);
dpList.remove((Integer)v.getTag());
((EditItemsActivity) activity).refreshList();
}
});
// Required so we know which index to remove from our dpList.
delete.setTag(position);

From your code, I can see that your adapter is populated by dplist. When you delete a row using db.deleteRowByKey(dp);you are updating the database but you are not updating your dplist.
You need to repopulate your dplist to match the database and only then will notifyDataSetChanged() work as expected.
A simply way to do this would be to change your refreshList() function to:
public void refreshList()
{
//reload dpList so that it can sync up with the database
dpList = db.allDataThisMonth();
//now notify adapter that the data set has changed so that it can update itself.
adapter.notifyDataSetChanged();
}

Related

How to fetch data from Api into listview?

When i am trying to call the method "refresh" of main activity from another Api class,the method was called and also it shows some fatal errors.And it didn't change the adapter values.Can anyone give any idea to clear that.?
package com.example.hotspot;
import com.example.hotspot.HotspotApi;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
import android.widget.TextView;
public class HotSpot extends Activity {
TextView textview;
ListView listview;
HotspotAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hot_spot);
textview = (TextView) findViewById(R.id.textView1);
listview = (ListView) findViewById(R.id.listView1);
adapter = new HotspotAdapter(this);
listview.setAdapter(adapter);
new HotspotApi(adapter).execute();
}
public void refresh() {
System.out.println("refresh() is called");
adapter.notifyDataSetChanged();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hot_spot, menu);
return true;
}
}
hotspot.java
package com.example.hotspot;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.hotspot.HotspotModel;
import com.example.hotspot.HotspotAdapter;
import android.os.AsyncTask;
public class HotspotApi extends AsyncTask<Void, Integer, Void> implements
Icommon {
public Boolean IsServerErr = false;
private JSONArray response_array;
String url = "some url";
HotspotAdapter adapter;
HotSpot hot;
public HotspotApi(HotspotAdapter adapter) {
this.adapter = adapter;
}
#Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
getresult();
return null;
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
hot=new HotSpot();
hot.refresh();
super.onPostExecute(result);
}
void getresult() {
InternetManager manager = new InternetManager(url);
String category_jsonresponse = manager.URLRequest();
if (!manager.IsServerConn) {
IsServerErr = true;
}
if (category_jsonresponse != null) {
System.out.println("Hotspot_jsonresponse" + category_jsonresponse);
try {
response_array = new JSONArray(category_jsonresponse);
for (int i = 1; i < response_array.length(); i++) {
JSONObject image_object = response_array.getJSONObject(i);
HotspotModel h = new HotspotModel();
h.setId(image_object.getString("id") == null ? ""
: image_object.getString("id"));
h.setContent(image_object.getString("content") == null ? ""
: image_object.getString("content"));
h.setImg(image_object.getString("img") == null ? ""
: image_object.getString("img"));
h.setName(image_object.getString("name") == null ? ""
: image_object.getString("name"));
arraylist.add(h);
}
System.out.println("HotspotModelsize() is " + arraylist.size());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
HotspotAdapter.java
package com.example.hotspot;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class HotspotAdapter extends BaseAdapter implements Icommon{
private TextView textview;
private View view;
ImageView imageview;
private LayoutInflater inflater;
public HotspotAdapter(Context context ){
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return arraylist.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arraylist.get(arg0);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
if (arg1 == null) {
view = inflater.inflate(R.layout.custom_layout, null);
} else {
view = arg1;
}
textview = (TextView) view.findViewById(R.id.txt_content);
textview.setText(arraylist.get(arg0).getName());
return view;
}
}
In your HotSpotApi class you are creating a new HotSpot activity, this seems wrong. I guess that you are getting json data from internet and load it into a listview.
Solution:
In HotspotApi change following instead of calling activity method:
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
adapter.notfiyDatasetChanged();
}
Hope this will help you.
hot=new HotSpot(); ?? you cannot use like that! HotSpot is an activity, should be called by Framework for example, activitymanager. Or use startActivity() to show a activity.
Refresh method (adapter.notifyDataSetChanged();) will result in refresh of UI. However, hot = new HotSpot() will not call onCreated() method,which means the UI is not created. So it definitely results in the fatal error.
I'd never see anyone call an Activity with new operator.
You should reference the common process about how use a activity and adapter.

How to start two different Intents in alistView.setOnItemClickListener(new OnItemClickListener() {}

I am new to android and a little bit confused. i have a listView with image and text. Where, if I click on a Image it should start an activity, and if I click on text another activity.
Cod:
in
onCrete(){
listView = getListView();}
myBaseAdapterItemActivity = new MyBaseAdapterItemActivity(
ItemActivity.this, placeNameList);
setListAdapter(myBaseAdapterItemActivity);
myBaseAdapterItemActivity.notifyDataSetChanged();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view,
int position, long id) {
// One Activity I can start without any problem
// In xml File I set for image clicable to true.
// What I want to do is like this
if(view.getId() == R.id.imageId)
{
Intent intent = new Intent(this, ImageActivity.class);
startActivity(intent);
}
else if(view.getID == R.id.textId)
{
Intent intent = new Intent(this, TextActivity.class);
startActivity(intent);
}
}}
And whenever I click on Image it does not not either in textView.
Any Idea
It has two solutions:
1) Instead on writing onItemClickListener for list you can do findviewbyid the textview and imageview in your custom adapter in getview method and then set onclick listeners on both of them.
2) You can use getChildAt method.... and check which child is your imageview and which is your textview. This is a work around so not much guaranteed.
Try with the below code.
Your adapter should be like below code. then your text and image click will create new activity.
Hi the code should be like below Hope this helps you.
package com.example.listwithclick;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
ListView listView1;
Activity activity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity=this;
listView1=(ListView)findViewById(R.id.listView1);
listView1.setAdapter(new MyAddapter(MainActivity.this));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class MyAddapter extends BaseAdapter {
Context rContext;
private LayoutInflater rInflater;
public MyAddapter(Context c) {
rInflater = LayoutInflater.from(c);
rContext = c;
}
public MyAddapter(Activity imagebinding) {
// TODO Auto-generated constructor stub
activity = imagebinding;
rContext = imagebinding;
rInflater = LayoutInflater.from(imagebinding);
rContext = imagebinding;
rInflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 10;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
convertView = rInflater.inflate(R.layout.child, null);
final MyDat mydat = new MyDat();
mydat.textview = (TextView) convertView.findViewById(R.id.textView1);
mydat.textview.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(rContext, "text", 1000).show();
Intent image= new Intent(rContext,TextActivity.class);
startActivity(image);
}
});
mydat.imageView1=(ImageView)convertView.findViewById(R.id.imageView1);
mydat.imageView1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(rContext, "image ", 1000).show();
Intent image= new Intent(rContext,ImageActivity.class);
startActivity(image);
}
});
return convertView;
}
class MyDat {
TextView textview;
ImageView imageView1;
}
}
}
In your MyBaseAdapterItemActivity, setOnClickListener((OnCLickListener)mContext) to the ImageVIew and TextView.
In your activity, extends OnClickListener.
write your startActivity(Intent) in the OnClick(View v) depending on v.getId()
In list item xml, for set android:onClick="onFirstLinkClick" and similarly for the image view also,
and the use following method in your activity
public void onFirstLinkClick(View V) {
}

How to retrieve listview using BaseAdapter?

I have created android apps to retrieving Listview using BaseAdapter from ArrayList but not displayed anything in Listview. Description of my apps is that I want retrieve listview using BaseAdapter. when my app is run on emulator nothing is displayed on screen when clicked on menu button on my emulator only add button displayed on screen after clicked on add launch the new activity in that activity i have created two editText. after submit, return to prev activity and display the listview.
Please can anybody help me to find out this error
Following is the Adapter class
package com.oj2.exlistview;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class TimeTrackAdapter extends BaseAdapter {
ArrayList<TimeRecord> times = new ArrayList<TimeRecord>();
public Context cntxt;
public TimeTrackAdapter(Context cnt,ArrayList<TimeRecord> list2) {
super();
cntxt = cnt;
times = list2;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return new TimeTracker().getList1().size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return getItem(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
if (convertView == null){
System.out.println("in getView");
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.time_list_item, parent,false);
TimeRecord time = times.get(position);
TextView timeTextView = (TextView) convertView.findViewById(R.id.timeView);
timeTextView.setText(time.getTimes());
TextView noteTextView = (TextView) convertView.findViewById(R.id.noteView);
noteTextView.setText(time.getNotes());
}
return convertView;
}
}
Following is the Activity class
package com.oj2.exlistview;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;
public class TimeTracker extends Activity{
TimeTrackAdapter timeTrackAdapter;
ListView listView;
TimeRecord timerecord;
public ArrayList<TimeRecord> list1 = new ArrayList<TimeRecord>();
public static final int TIME_ENTRY_REQUEST_CODE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
System.out.println("in onCreate()");
}
public ArrayList<TimeRecord> getList1() {
return list1;
}
public void setList1(ArrayList<TimeRecord> list1) {
this.list1 = list1;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
System.out.println("in onCreateOptionsMenu");
MenuInflater mI = getMenuInflater();
mI.inflate(R.menu.time_list_menu,menu);
return true;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
System.out.println("in onMenuItemSelected");
if (item.getItemId()==R.id.add_time_menu_item) {
Intent intent = new Intent(this, AddTimeActivity.class);
startActivityForResult(intent, TIME_ENTRY_REQUEST_CODE);
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
System.out.println("in onActivityResult");
if (requestCode==TIME_ENTRY_REQUEST_CODE) {
if (resultCode==RESULT_OK) {
String Time = data.getStringExtra("time");
String Note = data.getStringExtra("note");
list1.add(new TimeRecord(Time, Note));
timeTrackAdapter = new TimeTrackAdapter(this,list1);
listView = (ListView) findViewById(R.id.timeListView);
listView.setAdapter(timeTrackAdapter);
listView.getAdapter();
/* timeTrackAdapter.addTimeRecord(new TimeRecord(Time, Note));*/
timeTrackAdapter.notifyDataSetChanged();
}
}
}
}
Following is the AddTimeActivity
package com.oj2.exlistview;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class AddTimeActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_time);
}
public void onCancle(View view) {
finish();
}
public void onSave(View view) {
Intent intent = getIntent();
EditText TimeView = (EditText) findViewById(R.id.editText1);
intent.putExtra("time", TimeView.getText().toString());
EditText Noteview = (EditText)findViewById(R.id.editText2);
intent.putExtra("note", Noteview.getText().toString());
this.setResult(RESULT_OK, intent);
finish();
}
}
Thanking You
First, don't create any Activity object in your Adapter. Activity objects are created by Android itself, and they are used to display Views. It has nothing to do with adapters.
The layout you want to display your data in must contain a <ListView ... /> tag, with an android:id="#+id/whatEverId" attribute.
Then what you have to do is to retrieve the ListView object in your TimeTracker activity using its id :
ListView lv = (ListView) findViewById(R.id.whatEverId);
Then create your adapter and tell your ListView that the views you want it to display will be created by your custom adapter :
TimeTrackerAdapter adapter = new TimeTrackerAdapter(this, myListOfData);
lv.setAdapter(adapter);
myListOfData is your ArrayList containing the data. The rest is explicit and should do the job.

How to make app like Flipboard?

I want to make an application similar like Flipboard on android like this. I am using gridview for showing Gallery images, but it's not applicable. How can I achieve this? I also want to apply page curl and flip.because page curl and flip.
I have to implement curl effect in flipboard please anyone guide me.
you can achieve it by using ViewFlipper or viewPager APIs.
ViewPager API is supported above ICS. but, for lover version you can use android.support-v4 library.
for View Flipper you can use animation for flipping.
ViewFlipper source code::
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;
public class ViewFlipperActivity extends Activity implements android.view.GestureDetector.OnGestureListener {
/** Called when the activity is first created. */
private int[] imageID = {
R.drawable.a01, R.drawable.a02,
R.drawable.a03, R.drawable.a04,
R.drawable.a05, R.drawable.a06,
R.drawable.a07, R.drawable.a08,
R.drawable.a09, R.drawable.a010,
R.drawable.a011
};
private ViewFlipper viewFlipper = null;
private GestureDetector gestureDetector = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);
// gestureDetector Object is used to detect gesture events
gestureDetector = new GestureDetector(this);
for (int i = 0; i < imageID.length; i++)
{
ImageView image = new ImageView(this);
image.setImageResource(imageID[i]);
image.setScaleType(ImageView.ScaleType.FIT_XY);
viewFlipper.addView(image, new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}
}
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
if (arg0.getX() - arg1.getX() > 120)
{
this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_in));
this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_out));
this.viewFlipper.showNext();
return true;
}
else if (arg0.getX() - arg1.getX() < -120)
{
this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_in));
this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_out));
this.viewFlipper.showPrevious();
return true;
}
return true;
}
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
}
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
return this.gestureDetector.onTouchEvent(event);
}
}
View Flipper XML layout:
<ViewFlipper
android:id="#+id/viewflipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
ViewPager dource code::
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ViewPager viewPager;
private static int NUM_AWESOME_VIEWS = 20;
private Context cxt;
private pageradapter adapter;;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cxt = this;
adapter = new pageradapter();
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class pageradapter extends PagerAdapter{
#Override
public int getCount() {
return NUM_AWESOME_VIEWS;
}
/**
* Create the page for the given position. The adapter is responsible
* for adding the view to the container given here, although it only
* must ensure this is done by the time it returns from
* {#link #finishUpdate()}.
*
* #param container The containing View in which the page will be shown.
* #param position The page position to be instantiated.
* #return Returns an Object representing the new page. This does not
* need to be a View, but can be some other container of the page.
*/
#Override
public Object instantiateItem(View collection, int position) {
ImageView iv = new ImageView(cxt);
iv.setBackgroundResource(R.drawable.a01);
((ViewPager) collection).addView(iv,0);
return iv;
}
/**
* Remove a page for the given position. The adapter is responsible
* for removing the view from its container, although it only must ensure
* this is done by the time it returns from {#link #finishUpdate()}.
*
* #param container The containing View from which the page will be removed.
* #param position The page position to be removed.
* #param object The same object that was returned by
* {#link #instantiateItem(View, int)}.
*/
#Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((ImageView) view);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view==((ImageView)object);
}
/**
* Called when the a change in the shown pages has been completed. At this
* point you must ensure that all of the pages have actually been added or
* removed from the container as appropriate.
* #param container The containing View which is displaying this adapter's
* page views.
*/
#Override
public void finishUpdate(View arg0) {}
#Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {}
#Override
public Parcelable saveState() {
return null;
}
#Override
public void startUpdate(View arg0) {
}
}
}
viewPager XML layout::
<RelativeLayout 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.support.v4.view.ViewPager
android:id="#+android:id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
Not sure about the layout but this page flipping animation tutorial might help:
http://openaphid.github.com/blog/2012/05/21/how-to-implement-flipboard-animation-on-android/
If someone uses library from accepted answer and needs to flip pages without swipe gesture (for example to implement next and previous buttons) can use methods which I did for myself. I spend a while to figure out how does the library works so I think it can be helpful.
add this to FlipCards.java
protected void flipToNextCard()
{
if(accumulatedAngle >= (maxIndex-1)*180 || state != STATE_INIT)
return;
controller.showFlipAnimation();
forward = true;
setState(STATE_AUTO_ROTATE);
controller.getSurfaceView().requestRender();
}
protected void flipToPreviousCard()
{
if(accumulatedAngle <= 0 || state != STATE_INIT)
return;
controller.showFlipAnimation();
forward = false;
swapCards();
frontCards.resetWithIndex(backCards.getIndex() - 1);
controller.flippedToView(getPageIndexFromAngle(accumulatedAngle-1), false);
setState(STATE_AUTO_ROTATE);
controller.getSurfaceView().requestRender();
}
and this to FlipViewController.java
public void flipToNextView()
{
cards.flipToNextCard();
}
public void flipToPreviousView()
{
cards.flipToPreviousCard();
}

Android: My ListView Rows can't be selected on touch mode after adding a new row to the data

I have an Activity and a Thread. The thread handles the data (later that data will be grabbed from Internet activity, for now, it just automatically adds a new row each 10 seconds). The thing is, after a new row being add, I can't touch the items anymore, to regain focus, I must press the up or down arrow on my hardware keyboard, or the menu button.
Of course I first thought to re-set the .setFocusableOnTouchMode to true, but this didn't seem to solve my problem. Or at least, I'm not setting it on the right place. Anyway this is my code:
The Activity:
$
package com.ejemplolisbox;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class EL extends Activity {
/**
* #see android.app.Activity#onCreate(Bundle)
*/
public Refresh actualizar;
// The thread
public static ListView g;
public static EfficientAdapter instance ;
// The adapter (is a custom made adapter, I didn't do it myself, just grabbed it from the Internet)
public static String[] abbreviations = { "Item0",
"Item1", "Item2"};
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
g = (ListView) findViewById(R.id.lv_country);
g.setFocusable(true);
g.setFocusableInTouchMode(true);
instance = new EfficientAdapter(this);
// The adapter is now set to this instance
g.setAdapter(instance);
actualizar = new Refresh();
actualizar.start();
//I start the thread
g.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View arg0, boolean arg1) {
// TODO Auto-generated method stub
// Code should be here (??)
}
});
g.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView a, View v, int position,
long id) {
//use position to get clicked position
//your code goes here
}
});
}
public static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return abbreviations.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.rowlayout, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView
.findViewById(R.id.TextView01);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text1.setText(abbreviations[position]);
return convertView;
}
static class ViewHolder {
TextView text1;
}
}
}
OK now the Thread.
package com.ejemplolisbox;
public class Refresh extends Thread{
public void run(){
while(true){
String[] ex=EL.abbreviations;
String[] ne=new String[ex.length+1];
for(int i=0;i<ex.length;i++){
ne[i]=ex[i];
}
ne[ex.length]="newItem"+ex.length;
EL.abbreviations=ne;
try{
EL.instance.notifyDataSetChanged();
EL.g.setFocusableInTouchMode(true);
}
catch(Exception e){
int i = 0;
EL.g.setFocusableInTouchMode(true);
}
EL.g.setFocusableInTouchMode(true);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Well, thanks on advance, any solution will be appretiated
You can't directly modify UI elements from a thread that isn't the UI thread. If you want to modify a UI element from a different thread, you have to call the function runOnUiThread. That means you'll have to pass a refernce to the activity to the Refresh class in it's constructor so you can do the following call on your thread:
activity.runOnUiThread(new Runnable(){
public void run(){
EL.g.setFocusableInTouchMode(true);
}
});
That said, if I were you I'd scrap this and just use an AsyncTaskLoader instead. It's much easier and the class was specifically created for asynchronously loading up elements in things like a listview. The way you have the code written right now is not going to work very well and is prone to all kinds of error. In android, a general rule of thumb is to not use the raw Thread class unless you abosultely have to. Use either the AsyncTaskLoader, AsyncTask, or IntentService.
Final note, if you're developing for API levels less than 10, you can still access the AsyncTaskLoader class via the android Support Package.

Categories

Resources