How can i convert this activity to a fragment? [closed] - android

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to parse an rss feed and since i'm a beginner in android i cannot find a way to do this through a fragment..
This is the activity i want to convert into a fragment
public class Clients extends Activity {
private Clients local;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
local = this;
GetRSSDataTask task = new GetRSSDataTask();
task.execute("http://www.itcuties.com/feed/");
Log.d("ITCRssReader", Thread.currentThread().getName());
}
private class GetRSSDataTask extends AsyncTask<String, Void, List<RssItem> > {
#Override
protected List<RssItem> doInBackground(String... urls) {
Log.d("ITCRssReader", Thread.currentThread().getName());
try {
RssReader rssReader = new RssReader(urls[0]);
return rssReader.getItems();
} catch (Exception e) {
Log.e("ITCRssReader", e.getMessage());
}
return null;
}
#Override
protected void onPostExecute(List<RssItem> result) {
ListView itcItems = (ListView) findViewById(R.id.listView);
ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(local,android.R.layout.simple_list_item_1,result);
itcItems.setAdapter(adapter);
itcItems.setOnItemClickListener(new ListListener(result, local));
}
}
}
I already have tried to convert it but the onItemClick is getting some errors.
public void onItemClick(AdapterView parent, View view, int pos, long id) {
Intent intent = new Intent(activity, Clients.class);
intent.putExtra("description", listItems.get(pos).getLink());
activity.startActivity(intent);
}
Can someone please help me???

You should call the fragment without ui. It is needed to add ui, but not to make it visible.
public class MyFragmet extends Fragment {
public static final String TAG = "MyFragmet";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.MY_FRAGMENT_NULL_VIEW,
container, false);
local = this;
GetRSSDataTask task = new GetRSSDataTask();
task.execute("http://www.itcuties.com/feed/");
Log.d("ITCRssReader", Thread.currentThread().getName());
return view;
}
private class GetRSSDataTask extends AsyncTask<String, Void, List<RssItem> > {
#Override
protected List<RssItem> doInBackground(String... urls) {
Log.d("ITCRssReader", Thread.currentThread().getName());
try {
RssReader rssReader = new RssReader(urls[0]);
return rssReader.getItems();
} catch (Exception e) {
Log.e("ITCRssReader", e.getMessage());
}
return null;
}
#Override
protected void onPostExecute(List<RssItem> result) {
Intent intent = new Intent();
intent.setAction(TAG ); // also here you can add other information
sendBroadcast(intent);
}
}
}
and add this to activity
private BroadcastReceiver receiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
ListView itcItems = (ListView) findViewById(R.id.listView);
ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(local,android.R.layout.simple_list_item_1,result);
itcItems.setAdapter(adapter);
itcItems.setOnItemClickListener(new ListListener(result, local));
}
};
registerReceiver(receiver, new IntentFilter(MyFragmet.TAG));
FragmentManager fm = getFragmentManager();
Fragment fragment = fm.findFragmentByTag(MyFragmet.TAG);
if (fragment == null) {
getFragmentManager()
.beginTransaction()
.add(R.id.fragment, new MyFragmet(),MyFragmet.TAG)
.commit();
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}

Ok, here's my attempt:
I've replaced your ArrayAdapter with a baseadapter (finer tuned control over what happens with your items) - define a layout similiar to the list item you have in your ArrayAdapter, point the BaseAdapter at it, and set up the views within the onCreateView block.
Other than that, I think it should drop in pretty well.
public class Clients extends Fragment implents ListView.OnItemClickListener {
private Clients local;
private ListView itcItems;
private RssItemBaseAdapter adapter;
private ArrayList<Rssitem> itemList = new ArrayList<RssItem>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
local = this;
GetRSSDataTask task = new GetRSSDataTask();
task.execute("http://www.itcuties.com/feed/");
Log.d("ITCRssReader", Thread.currentThread().getName());
}
#Override
public void onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
view rootView = infalter.inflate(R.layout.activity_my,container,false);
itcItems = (ListView)findViewById(R.id.listView);
itcItems.setOnItemClickListener(this);
adapter = new RssItemBaseAdapter(getActivity(),itemList);
itcItems.setAdapter(adapter);
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i , long l){
RssItem item = adapter.getItem(i);
<Handle your Intent code here>
}
private class GetRSSDataTask extends AsyncTask<String, Void, List<RssItem> > {
#Override
protected List<RssItem> doInBackground(String... urls) {
Log.d("ITCRssReader", Thread.currentThread().getName());
try {
RssReader rssReader = new RssReader(urls[0]);
return rssReader.getItems();
} catch (Exception e) {
Log.e("ITCRssReader", e.getMessage());
}
return null;
}
#Override
protected void onPostExecute(List<RssItem> result) {
itemList = result;
adapter.swapList(itemList);
adapter.notifyDataSetChanged();
}
}
private class RssItemBaseAdapter extends BaseAdapter(){
private Context mContext;
private ArrayList<RssItem> mRssList;
public RssItemBaseAdapter(Context context, ArrayList<RssItem> obj){
mContext = context;
mRssList = obj;
}
#Override
public int getCount() {return mRssList.size(); }
#Override
public RssItem getItem(int i) {return mRssList.get(i); }
#Override
public long getItemId(int i) { return i }
#Override
public view getView(int i, View convertView, ViewGroup parent){
View rootView = convertView;
if (rootView == null){
View rootView = Inflater.from(mContext).inflate(R.layout.YOUR_SIMPLE_LAYOUT_HERE,parent,false);
}
<do your view setting here>
return rootView;
}
public ArrayList<RssItem> swapList (ArrayList<RssItem> newList){
ArrayList<RssItem> oldList = mRssList;
mRssList = newList;
return oldList;
}
}
}

Related

Fetched data from mvvm is not updated in recycler view on first visit

PreAdmissionList.java
public class PreAdmissionList extends Fragment implements View.OnClickListener, AdapterApprovalList.OnItemClickListener {
private BasicInfoViewModel basicInfoViewModel;
private AdapterApprovalList adapterApprovalList;
private RecyclerView rvApprovalList;
public PreAdmissionList() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_pre_admission_list, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.fab_add_new_admission).setOnClickListener(this);
rvApprovalList = view.findViewById(R.id.rv_approval_list);
basicInfoViewModel = new ViewModelProvider(requireActivity()).get(BasicInfoViewModel.class);
basicInfoViewModel.init();
basicInfoViewModel.getApprovalList().observe(getViewLifecycleOwner(), new Observer<List<ModelBasicInfo>>() {
#Override
public void onChanged(List<ModelBasicInfo> modelBasicInfos) {
adapterApprovalList.notifyDataSetChanged();
}
});
initRecyclerView();
}
private void initRecyclerView() {
adapterApprovalList = new AdapterApprovalList(this,basicInfoViewModel.getApprovalList().getValue());
rvApprovalList.setHasFixedSize(true);
rvApprovalList.setLayoutManager(new LinearLayoutManager(getContext()));
rvApprovalList.setAdapter(adapterApprovalList);
}
}
AdapterApprovalList.java
public class AdapterApprovalList extends RecyclerView.Adapter<AdapterApprovalList.ALViewHolder>{
private Context context;
private OnItemClickListener onItemClickListener;
private List<ModelBasicInfo> modelBasicInfoList;
public AdapterApprovalList(OnItemClickListener onItemClickListener,List<ModelBasicInfo> modelBasicInfoList) {
this.onItemClickListener = onItemClickListener;
this.modelBasicInfoList=modelBasicInfoList;
}
#NonNull
#Override
public ALViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
View view = LayoutInflater.from(context).inflate(R.layout.template_approval_list_item,parent,false);
return new ALViewHolder(view,onItemClickListener);
}
#Override
public void onBindViewHolder(#NonNull ALViewHolder holder, int position) {
ModelBasicInfo basicInfo = modelBasicInfoList.get(position);
StringBuilder fullName = new StringBuilder();
fullName.append(basicInfo.getFirstName()).append(" ");
fullName.append(basicInfo.getMiddleName()).append(" ");
fullName.append(basicInfo.getLastName()).append(" ");
holder.fullName.setText(fullName);
holder.id.setText("RKC00"+String.valueOf(basicInfo.getId()));
}
#Override
public int getItemCount() {
return modelBasicInfoList.size();
}
static class ALViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
OnItemClickListener mOnItemClickListener;
TextView fullName,id;
public ALViewHolder(#NonNull View itemView,OnItemClickListener mOnItemClickListener) {
super(itemView);
this.mOnItemClickListener = mOnItemClickListener;
fullName = itemView.findViewById(R.id.tv_text_full_name);
id = itemView.findViewById(R.id.tv_text_approval_id);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
mOnItemClickListener.onApprovalItemClick(getAbsoluteAdapterPosition());
}
}
public interface OnItemClickListener{
void onApprovalItemClick(int position);
}
}
BasicInfoViewModel.java
public class BasicInfoViewModel extends ViewModel {
private BasicInfoRepo basicInfoRepo;
private MutableLiveData<List<ModelBasicInfo>> approvalList;
public void init(){
if(approvalList != null){
return;
}
basicInfoRepo = BasicInfoRepo.getInstance();
approvalList = basicInfoRepo.getApprovalList();
}
public LiveData<List<ModelBasicInfo>> getApprovalList(){
return approvalList;
}
public void insertBasicInfo(ModelBasicInfo modelBasicInfo){
basicInfoRepo.insertData(modelBasicInfo);
}
public void updateApprovalStatus(int id){
basicInfoRepo.updateStatus(id);
}
}
BasicInfoRepo.java
public class BasicInfoRepo {
private static BasicInfoRepo instance;
static ConnectionClass connectionClass = new ConnectionClass();
private List<ModelBasicInfo> approvalList = new ArrayList<>();
public static BasicInfoRepo getInstance(){
if(instance== null){
instance = new BasicInfoRepo();
}
return instance;
}
public MutableLiveData<List<ModelBasicInfo>> getApprovalList(){
loadApprovalList();
MutableLiveData<List<ModelBasicInfo>> mList = new MutableLiveData<>();
mList.setValue(approvalList);
return mList;
}
private void loadApprovalList() {
LoadApprovalList loadApprovalList = new LoadApprovalList();
loadApprovalList.execute();
}
public void insertData(ModelBasicInfo modelBasicInfo){
InsertBasicInfo insertBasicInfo = new InsertBasicInfo();
insertBasicInfo.execute(modelBasicInfo);
}
public void updateStatus(int id){
UpdateBasicInfo updateBasicInfo = new UpdateBasicInfo();
updateBasicInfo.execute(id);
}
private static class InsertBasicInfo extends AsyncTask<ModelBasicInfo,Integer,String>{
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected String doInBackground(ModelBasicInfo... model) {
String result = null;
// Log.i("Testing db",lists[0].get(0).getFirstName());
try{
Connection connection = connectionClass.CONN();
if(connection==null){
result = "Error in connection !!!";
}else{
//Date object
Date date= new Date();
//getTime() returns current time in milliseconds
long time = date.getTime();
//Passed the milliseconds to constructor of Timestamp class
Timestamp ts = new Timestamp(time);
PreparedStatement ps = connection.prepareStatement("insert into PreAdmissionDetails values(?,?,?,?,?,?,?,?,?,?)");
ps.setString(1,model[0].getFirstName());
ps.setString(2,model[0].getMiddleName());
ps.setString(3,model[0].getLastName());
ps.setString(4,model[0].getMotherName());
ps.setDate(5, java.sql.Date.valueOf(model[0].getDateOfBirth()));
ps.setString(6,model[0].getMobileNo());
ps.setInt(7,0);
ps.setInt(8,0);
ps.setBoolean(9,false);
ps.setTimestamp(10, ts);
ps.executeUpdate();
result = "Submitted Successfully !!!";
}
}catch (Exception ex){
Log.e("sqlerror",ex.toString());
result=ex.getMessage();
}
Log.e("sqlerror","result : "+result);
return result;
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
private static class UpdateBasicInfo extends AsyncTask<Integer,Integer,String>{
#Override
protected String doInBackground(Integer... integers) {
String result = null;
try{
Connection connection = connectionClass.CONN();
if(connection==null){
result = "Error in connection !!!";
}else{
PreparedStatement ps = connection.prepareStatement("UPDATE PreAdmissionDetails SET STATUS=? WHERE id=?");
ps.setInt(1,0);
ps.setInt(2,integers[0]);
ps.executeUpdate();
result = "Updated Successfully !!!";
}
}catch (Exception ex){
Log.e("sqlerror",ex.toString());
result=ex.getMessage();
}
Log.e("sqlerror","result : "+result.toString());
return result;
}
}
private class LoadApprovalList extends AsyncTask<Void,Void,Void>{
#Override
protected Void doInBackground(Void... voids) {
String result = null;
try{
Connection connection = connectionClass.CONN();
if(connection==null){
result = "Error in connection !!!";
}else{
PreparedStatement ps = connection.prepareStatement("select * from preadmissiondetails");
ResultSet rs = ps.executeQuery();
approvalList.clear();
while (rs.next()) {
approvalList.add(new ModelBasicInfo(rs.getInt(1),
rs.getString(2),
rs.getString(3),
rs.getString(4),
rs.getString(5),
rs.getString(6),
rs.getString(7),
rs.getInt(8),
rs.getInt(9),
rs.getBoolean(10)));}
result = "Fetched Successfully !!!";
}
}catch (Exception ex){
Log.e("sqlerror",ex.toString());
result=ex.getMessage();
}
Log.e("sqlerror","result : "+result.toString());
return null;
}
}
}
Problem is here that when I open the app no items in the recycler view, Firsty I thought may be slow internet it will fetch data in after some time but it doesn't show data. But when I navigate to some other fragments and return to the preadmission list it shows data.
Your problem is here:
public MutableLiveData<List<ModelBasicInfo>> getApprovalList(){
loadApprovalList();
MutableLiveData<List<ModelBasicInfo>> mList = new MutableLiveData<>();
mList.setValue(approvalList);
return mList;
}
loadApprovalList() launches an AsyncTask, which is an asynchronous operation (in other words, it takes time to produce a result). The getApprovalList() method doesn't just halt right there and wait for loadApprovalList() to complete. It continues right along and returns an empty list the first time it executes. But by the second time it executes, approvalList now has a value because the AsyncTask has completed. So it returns the correct data that second time it executes. It executes a second time when you return to your Fragment because the init block in your ViewModel is executing a second time at that point.
The solution is to make approvalList a LiveData. That way, when the AsyncTask updates approvalList, your ViewModel can observe the change. Your ViewModel should observe approvalList in your Repository, just like how your Fragment is observing the getApprovalList() method in your ViewModel.

How to update an item on a recyclerview after excuting AsyncTask doInBackground?

I'm creating a chat feature for an application and it works super fine. But I would like to show the user that message has been sent or it still wating for the server's response.
Fields:
List<ChatMessage> chatMessages;
ChatAdapter chatAdapter;
RecyclerView chatRecyclerView;
ImageButton submitMessageBtn;
this how I send a message on my ChatActivity class:
public void submitMessage(final String messageType, final byte[] message){
final ChatMessageResponse messageObject = new ChatMessageResponse();
new AsyncTask<Void, Void, Void>() {
#Override
protected void onPreExecute() {
super.onPreExecute();
messageObject.setMessage( message);
messageObject.setYours(true);
messageObject.setUserNickname(getNickname());
messageObject.setCreationDate(DateTime.now().withZone(DateTimeZone.UTC));
messageObject.setType(messageType);
AddMessage(messageObject);
}
#Override
protected Void doInBackground(Void... voids) {
try {
chatClient.chat().sendMessage(eventId, messageType, message);
runOnUiThread(new Runnable() {
#Override
public void run() {
// Update message on the list after has been sent to server
}
});
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}.execute();
}
public void AddMessage(ChatMessage message)
{
chatMessages.add(message);
chatAdapter.notifyDataSetChanged();
chatRecyclerView.scrollToPosition(chatMessages.size() -1);
}
When message is immediatly added to the adapter it should look like this:
my ChatAdapter class is setup like this:
public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ChatViewHolder> {
private static final int VIEW_TYPE_MESSAGE_THIS_USER = 0;
private static final int VIEW_TYPE_MESSAGE_OTHER_USER = 1;
private final Activity activity;
public List<ChatMessage> chats=new ArrayList<>();
ArrayList<String> usercolor=new ArrayList<>();
Context mContext;
View view;
public ChatAdapter(List<ChatMessage> chats, Context mContext, Activity activity) {
this.chats = chats;
this.mContext = mContext;
this.activity = activity;
}
#Override
public ChatViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
mContext = parent.getContext();
if (viewType == VIEW_TYPE_MESSAGE_OTHER_USER) {
view = View.inflate(mContext, R.layout.message_item_left, null);
} else if (viewType == VIEW_TYPE_MESSAGE_THIS_USER){
view = View.inflate(mContext, R.layout.message_item, null);
}
return new ChatViewHolder(view,(View.OnLongClickListener)activity);
}
#Override
public void onBindViewHolder(final ChatViewHolder holder, int position){
final ChatMessageResponse m = (ChatMessageResponse) chats.get(position);
if (getItemViewType(position) == VIEW_TYPE_MESSAGE_OTHER_USER){
holder.bindToView1(m);
} else if (getItemViewType(position) == VIEW_TYPE_MESSAGE_THIS_USER)
{
holder.bindToView(m);
}
}
#Override
public int getItemCount() {
return chats.size();
}
#Override
public int getItemViewType(int position) {
return chats.get(position).isYours() ? VIEW_TYPE_MESSAGE_THIS_USER : VIEW_TYPE_MESSAGE_OTHER_USER;
}
}
When the server's response is positive the views in the ChatViewHolder (that I don't show the code because is too long) should change visibility state
Someone told me to get a referece for the view and change it on the activity's asynctask or create a Callback listener for my adapter.
But I have no Idea how to do either one of then any help is appreciated.
Are you familiar with the use of "Callbacks" or "Interfaces"? You can create an interface and implement it in your activity. Pass the callback by parameters in the "AsyncTask" and use it there.
//Interface class
/**
* Created by gmora
*/
public interface IProcess {
void updateAdapter(String result);
}
On Activity:
public class YourActivity extends AppCompatActivity {
private IProcess mProcess;
private Adapter mRecyclerAdapter;
private RecyclerView mRecyclerView;
private List<ChatMessage> chats; //update chats on activity and refresh your adapter
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mProcess = new IProceso() {
#Override
public void updateAdapter(String pException) {
//update chats ... and update mAdater.notifyDataChange()...
// or mRecyclerView.setAdapter(new Adpater.... with new list chats)..
}
};
mRecyclerView = find....
// etc....
mRecyclerAdapter = new RecyclerAdapter( chats, ...);
mRecyclerView.setAdapter(mRecyclerAdapter);
}
}
Finally on AsyncTask... create a external class from AsyncTask please!
/**
* Created by gmora.
*/
public class YourAsyncTaskClass extends AsyncTask<String, Void, String > {
private IProcess iProcess;
public StarSearchPrinterTask(IProcess pIProcess) {
this.iProcess= pIProcess;
}
#Override
protected void onPreExecute() {
//loading... its optional
}
#Override
protected String doInBackground(String... interfaceType) {
// execute webservice or api and get results..
return results;
}
#Override
protected void onPostExecute(String results) {
mIProceso.updateAdapter(results);
}
}

how to send ArrayList(Bitmap) from asyncTask to Fragment and use it in Arrayadapter

I want to send a list of bitmap i retreived from mysql database using asyncTask to the fragment Fragment_ListView.class, in this fragment class i want to set the adapter of the listView with the bitmap token from asyncTask but i don't know how to do that.
Async Task
#Override
protected void onPostExecute(ArrayList<Bitmap> bitmapArrayList) {
super.onPostExecute(bitmapArrayList);
loading.dismiss();
// now after getting images from server , i want to send this bitmapArrayList
// to Fragment_ListView where i set the adapter of the
}
#Override
protected ArrayList<Bitmap> doInBackground(String... params) {
imageList = new ArrayList();
String add1 = "http://192.168.1.11/save/load_image_from_db.php?id=1";
String add2 = "http://192.168.1.11/save/load_image_from_db.php?id=2";
String add3 = "http://192.168.1.11/save/load_image_from_db.php?id=3";
URL url;
Bitmap image = null;
String[] adds = {add1, add2, add3};
for (int i = 0; i < adds.length; i++) {
try {
url = new URL(adds[i]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
image = BitmapFactory.decodeStream(connection.getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
imageList.add(image);
image = null;
}
return imageList;
OnCreate of MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listfrg = new Fragment_ListView();
getFragmentManager().beginTransaction().add(R.id.frml, listfrg).commit();
}
Fragment_ListView :
public class Fragment_ListView extends Fragment {
ListView mListView;
static ArrayList<Bitmap> bitmaps;
static MySimpleArrayAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frglist, container, false);
mListView = (ListView) view.findViewById(R.id.listView);
bitmaps = new ArrayList<>();
adapter = new MySimpleArrayAdapter(getActivity(), bitmaps);
mListView.setAdapter(adapter);
return view;
}
Something like this,
Create a new interface file
public interface BitMapArrayCallBack {
abstract void bitmapArray(ArrayList<Bitmap> bitmaps);
}
Then in AsyncTask ... i don't know your class name so i will assume the class ServerRequest
public class ServerRequest {
Context context;
public ServerRequest(Context context) {
this.context = context;
}
public void doAsyncTask(BitMapArrayCallBack bitmapCallback) {
new MyAsyncTask(bitmapCallback).execute();
}
public class MyAsyncTask extends AsyncTask<Void, Void, Void> {
private BitMapArrayCallBack bitmapCallback;
public MyAsyncTask(BitMapArrayCallBack bitmapCallback) {
this.bitmapCallback = bitmapCallback;
}
//call do in background etc..
#Override
protected void onPostExecute(ArrayList<Bitmap> bitmapArrayList) {
super.onPostExecute(bitmapArrayList);
loading.dismiss();
bitmapCallback.bitmapArray(bitmapArrayList);
// This will hold and return your arraylist
}
}
}
Now you must call your asynctask in fragment listview
public class Fragment_ListView extends Fragment {
ListView mListView;
static MySimpleArrayAdapter adapter;
private ServerRequest serverRequest;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frglist, container, false);
mListView = (ListView) view.findViewById(R.id.listView);
serverRequest = new ServerRequest(getActivity);
serverRequest.doAsyncTask(new BitMapArrayCallBack {
#Override
void bitMapArray(ArrayList<Bitmap> bitmaps) {
// in here you have access to the bitmaps array
adapter = new MySimpleArrayAdapter(getActivity(), bitmaps);
mListView.setAdapter(adapter);
}
})
return view;
}

Update a ListView in background?

I have a Fragment that contain a ListView with Adapters. I want when replace this Fragment the listview continue changing on background. To do it I create a method with a TimerTask inside a AsyncTask, because I want the ListView change to each 10 seconds. If the Fragment that contain the ListView is visible without replace its works fine, but if I make replace isn't works and throws an exception NullPointerException.
How can I solve it ?
I'm trying this.
Fragment
public class JogosAbertosFrag extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener {
private ImageButton btPerfil;
private Intent intentPerfil;
private ListView lvJogosFinalizados;
private ListView lvJogosAndamento;
protected ProgressDialog progressDialog;
private JogosListAdapter jogosListAdapterAndamento, jogosListAdapterFechado;
private TextView tvPontuacao;
private List<Batalha> listBatalhaAberto, listBatalhaFechado;
//clock
private Timer timer;
private TimerTask timerTask;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((CustomDrawerLayout)getActivity()).getSupportActionBar().show();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.jogosabertos, container, false);
lvJogosFinalizados = (ListView)rootView.findViewById(R.id.lvJogosFinalizados);
lvJogosFinalizados.setOnItemClickListener(this);
lvJogosAndamento = (ListView)rootView.findViewById(R.id.lvJogosAndamento);
lvJogosAndamento.setOnItemClickListener(this);
tvPontuacao = (TextView)rootView.findViewById(R.id.tvPontuacao);
tvPontuacao.setText(BatalhaConfigs.USUARIO_PONTUACAO);
btPerfil = (ImageButton)rootView.findViewById(R.id.btPerfil);
btPerfil.setOnClickListener(this);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
clockTask();
}
/** */
private void getAllBattles(){
ApplicationController app = new BatalhaDAO().getAllBattles(new BatalhaAdapter(){
#Override
public void getAllBattlesOpened(List<Batalha> list) {
if(!list.isEmpty()){
listBatalhaAberto = list;
if(jogosListAdapterAndamento == null){
jogosListAdapterAndamento = new JogosListAdapter(getView().getContext(), listBatalhaAberto);
lvJogosAndamento.setAdapter(jogosListAdapterAndamento);
}else{
jogosListAdapterAndamento.changeList(listBatalhaAberto);
}
}
}
#Override
public void getAllBattlesClosed(List<Batalha> list) {
if(!list.isEmpty()){
listBatalhaFechado = list;
if(jogosListAdapterFechado == null){
jogosListAdapterFechado = new JogosListAdapter(getView().getContext(), listBatalhaFechado);
lvJogosFinalizados.setAdapter(jogosListAdapterFechado);
}else{
jogosListAdapterFechado.changeList(listBatalhaFechado);
}
}
}
});
CustomVolleySingleton.getInstance(getView().getContext()).addToRequestQueue(app);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentTransaction ft;
Fragment frag;
if(parent == lvJogosAndamento){
Batalha batalha = listBatalhaAberto.get(position);
Bundle params = new Bundle();
params.putSerializable("infoBatalha", batalha);
frag = new JogarComOponenteFrag();
frag.setArguments(params);
ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fl, frag);
ft.addToBackStack("back");
ft.commit();
}else if(parent == lvJogosFinalizados){
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
/** timer para atualizar o adapter */
private void clockTask(){
new AsyncTask<String, Void, String>() {
#Override
protected String doInBackground(String... params) {
timer = new Timer();
timerTask = new TimerTask() {
#Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
tvPontuacao.setText(BatalhaConfigs.USUARIO_PONTUACAO);
getAllBattles();
}
});
}
};
timer.scheduleAtFixedRate(timerTask, 100, 10000);
return "execute";
}
}.execute("execute");
}
#Override
public void onStop() {
super.onStop();
CustomVolleySingleton.getInstance(getView().getContext()).cancelPendingRequests(CustomVolleySingleton.TAG);
}
}
ListAdapter
public class JogosListAdapter extends BaseAdapter {
private List<Batalha> lista;
private Context context;
public JogosListAdapter(Context context, List<Batalha> list){
this.context = context;
this.lista = list;
}
#Override
public int getCount() {
return lista.size();
}
#Override
public Object getItem(int position) {
return lista.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public void changeList(List<Batalha> lista){
this.lista = lista;
notifyDataSetChanged();
}
#Override
public View getView(int position, View view, ViewGroup parent) {
View layout;
if (view == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.jogos_list_adapter, parent, false);
}else{
layout = view;
}
return layout;
}
Exception
FATAL EXCEPTION: main
java.lang.NullPointerException
at br.com.mypackage.myapp.frags.JogosAbertosFrag.getAllBattles(JogosAbertosFrag.java:113)
at br.com.mypackage.myapp.frags.JogosAbertosFrag.access$12(JogosAbertosFrag.java:84)
at br.com.mypackage.myapp.frags.JogosAbertosFrag$2$1$1.run(JogosAbertosFrag.java:170)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
Exception Lines
113 has -> CustomVolleySingleton.getInstance(getView().getContext()).addToRequestQueue(app);
84 has -> private void getAllBattles(){}
170 close -> private void clockTask(){
First of All, you need to assign the Anonymous Inner Type AsyncTask class to a class variable. Before doing that you need to create a nested AsyncTask class within your Activity.
Your extended AsyncTask class should look something like this :
public class MyContinousAsyncTask extends AsyncTask<String, Void, String>() {
#Override
protected String doInBackground(String... params) {
timer = new Timer();
timerTask = new TimerTask() {
#Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
tvPontuacao.setText(BatalhaConfigs.USUARIO_PONTUACAO);
getAllBattles();
}
});
}
};
timer.scheduleAtFixedRate(timerTask, 100, 10000);
return "execute";
}
}
Then you need to declare a class variable of this Class in your Activity:
private MyContinousAsyncTask myContinouslyRunningAsyncTask;
now after doing the above, just modify your clockTask() method like this:
/** timer para atualizar o adapter */
private void clockTask(){
myContinouslyRunningAsyncTask = new MyContinousAsyncTask();
myContinouslyRunningAsyncTask.execute("execute");
}
now you should stop this AsyncTask on the onPause() Event of your Fragment, other wise you will be getting NPE because of accessing the de-allocated UI Components.
The code should look like this:
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if(myContinouslyRunningAsyncTask != null)
{
myContinouslyRunningAsyncTask.cancel();
}
if(timer != null)
{
timer.cancel();
timer.purge();
}
}
To make your code further efficient, you should not call the clockTask(); in onActivityCreated(..) method, but in onResume() method, like this:
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
clockTask();
}
I hope this helps.

Using a variable in two different methods

Am trying to use jsoup library in my android application and i want to get the value of str1 in the description class and use it in MyAdapter class as items.add(new Item(str1, R.drawable.flag_one)); i can't figure it out, any help is highly appreciated
public class HomeFragment extends Fragment {
// URL Address
String url = "http://www.livescore.com";
public HomeFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_home, container, false);
getOverFlowMenu();
GridView gridView = (GridView)rootView.findViewById(R.id.gridview);
gridView.setAdapter(new MyAdapter(rootView.getContext()));
new Description();
return rootView;
}
private void getOverFlowMenu() {
}
//Description async task
class Description extends AsyncTask<Void, Void, Void>{
String desc;
#Override
protected Void doInBackground(Void... voids) {
try{
// Connect to the web site
Document document = Jsoup.connect(url).get();
Element content = document
.body().getElementsByClass("kubrick-info__title").first();
// Locate the content attribute
desc = content.text();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result){
// Set description into TextView
String str1 = desc;
}
}
private class MyAdapter extends BaseAdapter
{
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
public MyAdapter(Context context)
{
inflater = LayoutInflater.from(context);
items.add(new Item(str1, R.drawable.flag_one));
items.add(new Item("teams", R.drawable.flag_two));
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int i)
{
return items.get(i);
}
#Override
public long getItemId(int i)
{
return items.get(i).drawableId;
}
}
Can you declare static String str1 = "" at the top, right under public class HomeFragment extends Fragment? Then in your onPostExecute method:
#Override
protected void onPostExecute(Void result){
// Set description into TextView
str1 = desc;
}
The idea is that if it the String is declared at the top, it will be visible throughout the entire HomeFragment class.

Categories

Resources