Ik im probably not going about this the best way , but I have no idea how to go about this the right way given my current setup . Pretty much I have a fragment tabhost that has just about all my content including a newsfeed, from the newsfeed users can click another users picture to open their profile. Up until now I was taking the easy way out and just using a intent which opened a whole new instance of the tabhost and all the fragments, i sent the profile id over via a intent extra and my problem was solved, but ive since learned this isnt a great solution . So ive been trying to fix up my app by taking the advice on this thread:
Communication between Fragments
my problem is bc my onClick listeners for the profile images is in my LazyAdapter class which extends BaseAdapter, whats the best way to switch out the fragments and pass over the proper profile id to the new fragment given my situation?
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView name = (TextView)vi.findViewById(R.id.name); // title
TextView message = (TextView)vi.findViewById(R.id.message); // artist name
TextView created = (TextView)vi.findViewById(R.id.created); // duration
SmartImageView thumb_image = (SmartImageView) vi.findViewById(R.id.list_image);
HashMap<String, String> update = new HashMap<String, String>();
update = data.get(position);
// Setting all values in listview
name.setText(update.get("name"));
message.setText(update.get("message"));
created.setText(update.get("created"));
thumb_image.setImageUrl(update.get("thumb_img"));
name.setOnClickListener(new myOnClickListener(position));
thumb_image.setOnClickListener(new myOnClickListener(position));
return vi;
}
public class myOnClickListener implements OnClickListener{
private int position;
private String clicked_uid;
public myOnClickListener(int position){
this.position=position;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
HashMap<String, String> update = new HashMap<String, String>();
update = data.get(position);
Log.i("Update Position:", update.toString());
clicked_uid=update.get("uid");
Log.d("Clicked UID:", clicked_uid+"");
Intent i = new Intent(activity.getApplicationContext() , TabHostFragmentActivity.class);
i.putExtra("profile_uid", clicked_uid);
activity.startActivity(i);
activity.finish();
}
}
}
Related
Im trying to implement SharedPreferences but, since this is all so new to me, Im confused on how to actually do this. I have an activity that contains Two EditTexts and a Button; those EditTtexts are subject and grade, and the button sends it to a fragment activity, which contains a Recyclerview with its cardview etc. I have no trouble getting the input and displaying it in the Recyclerview list. When I try applying the Shared Preferences to save the custom data from the recyclerview, I have no idea where I should input the code.
I tried several tutorials, and I used gson to set a saveData, but I havent been able to call the list, making my app crash.
Activity with the Button
package com.example.repomax;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Objects;
public class Binary extends AppCompatDialogFragment {
private PersonAdapter adapter;
private static final String TAG = "Binary Dialog";
public Binary(PersonAdapter adapter) {
this.adapter = adapter;
}
private void onClick(View v) {
if (classtup.getText().toString().isEmpty() || Sectiones.getText().toString().isEmpty()) {
Toast.makeText(getContext(), "Llene todos los campos", Toast.LENGTH_LONG).show();
} else {
ApplicationClass.materias.add(new Lasmaterias(classtup.getText().toString().trim(),
Sectiones.getText().toString().trim()));
adapter.addMaterias(new Lasmaterias(classtup.getText().toString().trim(),
Sectiones.getText().toString().trim()));
Toast.makeText(getContext(), "Clase Registrada", Toast.LENGTH_SHORT).show();
classtup.setText(null);
Sectiones.setText(null);
saveData();
dismiss();
}
}
public void saveData
public interface OnInputSelected {
void SendInput(String input);
}
public OnInputSelected monInputSelected;
//Widgets
private
RecyclerView.Adapter myAdapter;
TextView Title;
EditText classtup, Sectiones;
Button addud;
Planes planes;
FragmentManager fragmentManager;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_binary, container, false);
myAdapter = new PersonAdapter(getContext(), ApplicationClass.materias);
classtup = view.findViewById(R.id.classtup);
Sectiones = view.findViewById(R.id.Sectiones);
fragmentManager = getParentFragmentManager();
planes = (Planes) fragmentManager.findFragmentById(R.id.Planes);
addud = view.findViewById(R.id.addud);
addud.setOnClickListener(this::onClick);
return view;
}
}
FragmentActivity with the RecyclerView:
package com.example.repomax;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.gson.Gson;
import java.util.ArrayList;
public class Planes extends Fragment {
private static final String TAG = "Planes fragment";
RecyclerView recyclerView;
RecyclerView.Adapter myAdapter;
RecyclerView.LayoutManager layoutManager;
View view;
Button fab;
public Planes() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_planes, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = view.findViewById(R.id.list);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this.getActivity());
recyclerView.setLayoutManager(layoutManager);
myAdapter = new PersonAdapter(getContext(), ApplicationClass.materias);
recyclerView.setAdapter(myAdapter);
fab = view.findViewById(R.id.fab);
fab.setOnClickListener(v -> {
Log.d(TAG, "onClick: Abriendo Ventanilla");
Binary dialog = new Binary((PersonAdapter) myAdapter);
dialog.setTargetFragment(Planes.this, 1);
dialog.show(getParentFragmentManager(), "Binary");
});
}
}
And finally the adapter:
package com.example.repomax;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.ViewHolder> {
private ArrayList<Lasmaterias> materias;
private Context context;
private PersonAdapter adapter;
public PersonAdapter(Context context, ArrayList<Lasmaterias> list) {
this.context = context;
this.materias = list;
}
public void addMaterias(Lasmaterias lasmaterias) {
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView tvName;
TextView tvGroup;
public ViewHolder(#NonNull View itemView) {
super(itemView);
tvName = itemView.findViewById(R.id.tvName);
tvGroup = itemView.findViewById(R.id.TvPass);
itemView.setOnClickListener(v -> {
});
}
}
#NonNull
#Override
public PersonAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.class_item, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull PersonAdapter.ViewHolder viewHolder, int position) {
viewHolder.itemView.setTag(materias.get(position));
viewHolder.tvName.setText(materias.get(position).getName());
viewHolder.tvGroup.setText(materias.get(position).getTelNr());
}
#Override
public int getItemCount() {
return materias.size();
}
}
I've been trying to import a very simple project I've started on a differnt laptop but for some reson the project now is full of errors.
Eclipse does not recognize imports being made and thus refuse to recognize simple classes as ActionBarActivity and methods like onCreate.
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
Button bEnter=(Button) getActivity().findViewById(R.id.bEnter);
Button bSignup=(Button) getActivity().findViewById(R.id.bSignup);
bEnter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String toastText="It's Working!!!";
int toastDuration=Toast.LENGTH_SHORT;
Toast toast=Toast.makeText(getActivity(), toastText, toastDuration);
toast.show();
}
});
}
}
}
What am I missing here??
What errors are you getting ?
Trick1:
If the appcompat_v7 project(library) is causing the problem, remove it from the project properties, then perform the following steps.
1) Right click on the main.
2) Hover over Android Tools.
3) Click 'Add Support Library'.
It will download the required library and the clean the project. It should work.
if doesn't work, let me know.
Trick2: Update to the latest Revisions ( Tools and SDKs using SDK manager)
Happy Coding.
I am bran new to this but I have successfully created a functional button.
I am attempting to pass user input (editText1 on activity_main) to Recipe (my second activity) The first code is my functional button. What am I doing wrong in the second round of code in my attempt at passing information to be displayed on the second activity layout?
package com.example.andrew;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b=(Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v){
startActivity(new Intent(MainActivity.this, Recipe.class));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Second round of code: My attempt at passing information. First activity
package com.example.andrew;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Edit Text et = (Edit Text) findViewById(R.id.editText1);
String theText = et.getText().toString();
Intent i = new Intent(this, Recipe.class);//Recipe is my second class
i.putExtra("text_label", theText);//what is "text_label"? Where should it be?
Button b=(Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v){
startActivity(new Intent(MainActivity.this, Recipe.class));
}
});
}
}
And in my second activity I have:
public class Recipe extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipe_layout);
Intent i = getIntent();
uriStringi = i.getStringExtra("text_label");//is text_label on actv. 1?
startActivity(i);
}
You are adding data in the intent but while starting the activity you are not using it. Instead creating new intent.
Solution:
In your current Activity, create a new Intent:
Intent i = new Intent(this, Recipe.class);
i.putExtra("text_label", theText);
startActivity(i); // Write this in onClick()
Then in the Recipe, retrieve those values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("text_label");
}
i am trying to create simple automation tool for testing.I have followed a simple tutorials
on net and created a RCP with view on eclipse. now i have tried to include simple GEF
component on the view it throws me error saying " Could not create the view: Plug-in "GEFTutorial" was unable to instantiate class "geftutorial.View"."
here is my source code
particularly when i uncomment creation of
private ScrollingGraphicalViewer viewer = new ScrollingGraphicalViewer();
private RootEditPart rootEditPart = new ScalableFreeformRootEditPart();
private EditPartFactory editPartFactory = new SimpleGEFEditPartFactory();
all the above statements on the view.my view appears back
here is my source code for view.java
package geftutorial;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.gef.*;
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;
public class View extends ViewPart {
public static final String ID = "GEFTutorial.view";
//Use a standard Viewer for the Draw2d canvas
private ScrollingGraphicalViewer viewer = new ScrollingGraphicalViewer();
//Use standard RootEditPart as holder for all other edit parts
private RootEditPart rootEditPart = new ScalableFreeformRootEditPart();
//Custom made EditPartFactory, will automatically be called to create
//edit
// parts for model elements
private EditPartFactory editPartFactory = new SimpleGEFEditPartFactory();
//The model
private SuperWidget model;
//private TableViewer viewer;
/**
* The content provider class is responsible for providing objects to the
* view. It can wrap existing objects in adapters or simply return objects
* as-is. These objects may be sensitive to the current input of the view,
* or ignore it and always show the same content (like Task List, for
* example).
*/
class ViewContentProvider implements IStructuredContentProvider {
public void inputChanged(Viewer v, Object oldInput, Object newInput) {
}
public void dispose() {
}
public Object[] getElements(Object parent) {
if (parent instanceof Object[]) {
return (Object[]) parent;
}
return new Object[0];
}
}
class ViewLabelProvider extends LabelProvider implements
ITableLabelProvider {
public String getColumnText(Object obj, int index) {
return getText(obj);
}
public Image getColumnImage(Object obj, int index) {
return getImage(obj);
}
public Image getImage(Object obj) {
return PlatformUI.getWorkbench().getSharedImages().getImage(
ISharedImages.IMG_OBJ_ELEMENT);
}
}
/**
* This is a callback that will allow us to create the viewer and initialize
* it.
*/
public void createPartControl(Composite parent) {
/*viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
| SWT.V_SCROLL);
viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new ViewLabelProvider());
// Provide the input to the ContentProvider
viewer.setInput(new String[] {"One", "Two", "Three"});
*/
//Create a dummy model
model = new SuperWidget("Model");
model.createDummyModel();
//Initialize the viewer, 'parent' is the
// enclosing RCP windowframe
viewer.createControl(parent);
viewer.setRootEditPart(rootEditPart);
viewer.setEditPartFactory(editPartFactory);
//Inject the model into the viewer, the viewer will
// traverse the model automatically
viewer.setContents(model);
//Set the view's background to white
viewer.getControl().setBackground(new Color(null, 255,255,255));
}
/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
viewer.getControl().setFocus();
}
}
Can someone give me a clue about this? i am new to RCP and GEF :(
I'm also just learning GEF, but from what I have seen gef editors are not ViewPart (views) but editors, extending EditPart.
Check my ongoing GEF tutorial here. Hope it helps.
You can also access other GEF tutorial from the eclipse website.
hi i have two classes in android and in one class i have write an array and i want to access it in the main class but the error is give me that "force closed" here is my code
package com.semanticnotion.DAO;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class DAO extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WordsDAO DAO = new WordsDAO(new String[] "Arte","Arquitectura","Familia","Moda","Cotilleos","Cine","Libros","Historia","Pintura","Musica","Tendencies","Modernimso","Pop art","Masteialismo","realities","filosofia","moda","fotografia","religion"});
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), WordsDAO.class);
startActivity(myIntent);
}
});
}
}
and the second class code is
package com.semanticnotion.DAO;
public class WordsDAO
{
String[] words = new String[] "Arte","Arquitectura","Familia","Moda","Cotilleos","Cine","Libros","Historia","Pintura","Musica","Tendencies","Modernimso","Pop art","Masteialismo","realities","filosofia","moda","fotografia","religion"};
public WordsDAO(String[] words )
{
this.words=words;
}
}
please any one tell what well be the error in this code thaks
First of all: the constructor in your second class would not be used. The way to pass parameters to another activity is to use Intent.putExtra in the code calling the other activity and in your other activity use
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
String value = extras.getString("keyName");
}
to get the data in onCreate.
That said, I guess the problem arises from your second class not providing an explicit parameterless constructor.