2014-02-09 3 views
0

У меня есть этот фрагмент, который раздувает действие с помощью кнопок. Как заставить кнопки перейти к другому действию или фрагменту? кнопку 1 предполагается перейти к деятельности карты и кнопки 2а listfragmentКнопки в фрагменте андроида

EDIT ** Все, что я хочу, это одна деятельность с 2-мя кнопками и одной из кнопок перейти к фрагменту

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 


public class MainActivity extends Activity { 
static Context context; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //context = getApplicationContext(); 
     Button test= (Button)findViewById(R.id.testbtn); 
     test.setOnClickListener(new View.OnClickListener() 
     { 
       public void onClick(View v) 
       { 
        Intent intent = new Intent(MainActivity.this, MyLocation.class);//this works 
        startActivity(intent); 

       } 
    }); 

     Button proximityAlert= (Button)findViewById(R.id.alertbtn); 
     proximityAlert.setOnClickListener(new View.OnClickListener() 
     { 
       public void onClick(View v) 
       { 
        Intent intent = new Intent(MainActivity.this, mfragment.class);//this doesn't work, this class is a fragment list 
        startActivity(intent); 

       } 
    }); 


    } 

    @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; 
    } 

} 

mfragment class

import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 
import android.support.v4.app.ListFragment; 

public class mfragment extends ListFragment { 
    String[] cities = { 
      "hello", 


    }; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //setting the adapter to the layout 
     setListAdapter(new ArrayAdapter<String>(getActivity(), 
       android.R.layout.simple_list_item_1, 
       cities)); 

    } 

    public void onListItemClick(ListView parent, View v, int position, long id) { 
     //displays a toest of the selected item in the list 
     //using an array to and using the position of the selected item 
     Toast.makeText(getActivity(), 
      "You have selected " + cities[position], 
      Toast.LENGTH_SHORT).show(); 
     String selectedCity = cities[position]; 

     /*//cheks if the detailfragment is in the current activity 
     DetailFragment detailFragment = (DetailFragment) 
      getFragmentManager().findFragmentById(R.id.detailFragment); 


     //---if the detail fragment is not in the current activity as myself--- 
     if (detailFragment != null && detailFragment.isInLayout()) { 
      //---the detail fragment is in the same activity as the master--- 
      detailFragment.setSelectedCity(selectedCity); 
      //calls the method set selected city and sends a string with the selcted city 
     } else { 
     */ 
      //---the detail fragment is in its own activity--- 
      //this is only needed if is in portrait mode 
      //starts a new activity and it pass arguments to know 
      //which city got selected 
      Intent intent = new Intent(getActivity(), DetailActivity.class); 
      intent.putExtra("city", selectedCity); 
      startActivity(intent); 
     //} 
    } 
} 

ответ

0

Вам нужно указать кнопки в правильных макетах. Если вы хотите, чтобы в действии была одна кнопка, вам нужно указать ее в XML-файле макета. Затем вы будете использовать его из кода действия. Аналогично, вам нужно указать кнопку, которую вы хотите разместить в фрагменте в файле макета xml-файла.

Вы не можете смешивать макеты с активностью и фрагментом, каждый из которых функционирует независимо друг от друга и не имеет прямого доступа друг к другу.

+0

Проверьте править PLS – user3022474

+0

То, что я сказал, все еще стоит. Если вы хотите кнопку на фрагменте, вам нужно определить его там. – Szymon

+1

Получил работу, спасибо – user3022474

Смежные вопросы