android Call ListActivity from activity class - android-listview

I have layouts main activity and another list activity. But when i try to call list activity i am getting crash. Please help me to start list activity (Note: List activity has EditText and button to add text to existing listview in list Activity)
All i wanted to add text listview which is already exist in listactivity.
Many Thanks!!
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view){
EditText txt=(EditText)findViewById(R.id.txtPass);
if (txt.getText().length()==0){
Toast.makeText(this, "Please enter password to continue!!", Toast.LENGTH_SHORT).show();
txt.getText().clear();
return;
}
if(txt.getText().toString().contains("Test")){
Button btn;
btn=(Button)findViewById(R.id.btnSubmit);
Intent intent=new Intent(this,Passwordlist.class);
startActivity(intent);
}
}

First, make some change to your code:
Button btn;
EditText txt
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Do this stuff here and not in onClick. Your views should be defined in //onCreate() and before the onCLick()
txt=(EditText)findViewById(R.id.txtPass);
btn=(Button)findViewById(R.id.btnSubmit);
}
public void onClick(View view){
Intent intent=new Intent(YourActivity.this,Passwordlist.class);
if (txt.getText().toString().matches("")){
Toast.makeText(this, "Please enter password to continue!!", Toast.LENGTH_SHORT).show();
txt.getText().clear();
}
else if(txt.getText().toString().contains("Test")){
startActivity(intent);
}
}
See if this works.

Related

Facebook SDK LoginButton

Good afternoon,
I'm here with a problem.
I managed to get the sdk facebook and can see the icon to log in, just that when I click on it it of the error. The hash key this correct, what could it be?
private LoginButton loginButton;
private CallbackManager mCallbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();
setContentView(R.layout.gondola);
loginButton = (LoginButton)findViewById(R.id.login_button);
loginButton.setReadPermissions("user_friends");
loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
Toast.makeText(getApplicationContext(), "LogIN",
Toast.LENGTH_SHORT).show();
}
#Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "ERRO1",
Toast.LENGTH_SHORT).show();
}
#Override
public void onError(FacebookException exception) {
Toast.makeText(getApplicationContext(), "ERRO2",
Toast.LENGTH_SHORT).show();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager.onActivityResult(requestCode, requestCode, data);
}

Type preference activity is dispatched

protected void onCreate (Bundle savedInstancestate) {
super.onCreate(savedInstancestate);
addPreferencesFromResources(R.xml.prefs);
}
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
addPreferencesFromResource (R.xml.YourFileName);
}

Start an instance of activity

How can i start an instance of activity.
My custom activity has a constructor take some paramters.
public class CustomActivity extends Activity {
public CustomActivity(String someParameters) {
}
}
And i create an instance of it. How to start it.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomActivity activity = new CustomActivity("TEST");
// how to start to the above activity from this activity
}

getting parse initialization error while unit testing activity with Robolectrics framework and eclipse

I am unit testing activity with Robolectric framework and getting the following error of parse initialization error.
MyActivity.java
public class WelcomeActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// hide action bar
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.hide();
//Checking Current User
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null)
{
Intent i=new Intent(WelcomeActivity.this, ViewPagerStyleLandingActivity.class);
startActivity(i);
WelcomeActivity.this.finish();
}
setContentView(R.layout.actvity_welcome);
SharedPreferences pref = getSharedPreferences(Constants.SIGNUP_CHECK_PREF, MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean(Constants.PREF_KEY_FIRST_RUN, false);
editor.commit();
Button loginbut=(Button) findViewById(R.id.login);
Button registerbut=(Button) findViewById(R.id.register);
loginbut.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i=new Intent(WelcomeActivity.this,LoginActivity.class);
startActivity(i);
}
});
registerbut.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i=new Intent(WelcomeActivity.this,RegisterActivity.class);
startActivity(i);
}
});
}
}
and have made the test case as
#RunWith(RobolectricTestRunner.class)
public class WelcomeActivityTest {
private WelcomeActivity activity;
private Button loginbut;
#Before
public void setup() {
activity = Robolectric.buildActivity(WelcomeActivity.class).create().get();
loginbut = (Button) activity.findViewById(R.id.button);`}
#Test
public void checkActivityNotNull() throws Exception {
assertNotNull(activity);
}
#Test
public void shouldHaveButtonThatSaysAudit() throws Exception{
assertThat((String) loginbut.getText(), equalTo("Login"));
}
#Test
public void pressingLaunchButtonForSecondActivity() throws Exception {
String resultValue = "Testing Text";
assertNotNull(loginbut);
loginbut.performClick();
Intent startedIntent = shadowOf(activity).getNextStartedActivity();
assertThat(resultValue, equalTo(startedIntent.getStringExtra("result")));
}
private ShadowContextWrapper shadowOf(WelcomeActivity activity2) {
return null;
}
#After
public void tearDown() throws Exception {
// Nothing Here
}
}
now getting the error
when running with jUnit

refresh listView by clicking outside of it

I have a viewpager with an adapter that extends FragmentStatePagerAdapter, this has to show some fragment that always repeats the same layout (in whic there is a listView) for every page ... now the problem is how can I update the listView that is currently visible by clicking on a button that is outside of the Fragment? I can refresh only the ones that are in the not visible pages,but not the ones that are displayed or adjacent...
One possible solution is to reset the adapter, but it doesn't seems to be right one, because it' s slow... could you please help me?
public class MainActivity extends FragmentActivity implements OnClickListener{
#Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.layout1);
pagerAdapter = new AdapterPager(getSupportFragmentManager());
ctx=this;
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(pagerAdapter);
pagerTab=(PagerTabStrip) findViewById(R.id.pager_tab_strip);
mViewPager.setCurrentItem(paginaCentrale);
modifica=(ImageView)findViewById(R.id.modifica);
modifica.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
// TODO onClick
switch (arg0.getId()){
case R.id.modifica:
//here I would like to call the method caricaLista() of the fragment
break;
}
}
public static class AdapterPager extends FragmentStatePagerAdapter {
public AdapterPager(FragmentManager fm) {
super(fm);
}
#Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return super.getItemPosition(object);
}
#Override
public Fragment getItem(int i) {
Fragment fragment;
Bundle args = new Bundle();
args.putInt("position", i);
fragment=new Fragment1();
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
return 1000;}
#Override
public CharSequence getPageTitle(int i) {
String title=String.valueOf(i);
return title;
}
}
public static class Fragment1 extends Fragment implements OnItemClickListener{
int fragmentPosition;
ListView lv;
List<Lista> lista= new ArrayList<Lista>();
public Fragment1(){
}
public static Fragment1 newInstance(){
return new Fragment1();
}
#Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
caricaLista("")}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle
savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1, container, false);
lv=(ListView) rootView.findViewById(R.id.listViewCompiti);
Bundle args = getArguments();
int position= args.getInt("position");
fragmentPosition=position;
lv.setOnItemClickListener(this);
homeListAdapter= new HomeListAdapterWithCache(ctx,R.layout.list_item_home,lista);
lv.setAdapter(homeListAdapter);
return rootView;
}
You have to store the Current Fragment into your MainActivity , and change every time the page changes.
So you call the method of the current fragments.
Check out how your MainActivity should look like:
public class MainActivity extends FragmentActivity implements OnClickListener{
&Fragment mCurrentFragment;*
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.layout1);
pagerAdapter = new AdapterPager(getSupportFragmentManager());
ctx=this;
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(pagerAdapter);
pagerTab=(PagerTabStrip) findViewById(R.id.pager_tab_strip);
mViewPager.setCurrentItem(paginaCentrale);
modifica=(ImageView)findViewById(R.id.modifica);
modifica.setOnClickListener(this);
*mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
mCurrentFragment = pagerAdapter.getItem(position);
}*
});
#Override
public void onClick(View arg0) {
// TODO onClick
switch (arg0.getId()){
case R.id.modifica:
*mCurrentFragment.caricaLista();*
break;
}
}
}