I have a dialog fragment listView,on this list is a image and a textview.
I would like to click each item on the list to start a new activity! tried an onItemClickListener but but cant seem to start a new activity,the current onItemClickListener displays toast text when clicked.
I have tried removing the toast and adding an intent to no avail.
public class MainActivity extends FragmentActivity {
Button showBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
final PlayersFragment p = new PlayersFragment();
showBtn = (Button) findViewById(R.id.button1);
showBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
showBtn.setVisibility(View.GONE);
transaction.replace(R.id.fragment_container, p);
transaction.addToBackStack(null);
transaction.commit();
}
});
}
@Override
public void onBackPressed() {
if (showBtn != null) {
showBtn.setVisibility(View.VISIBLE);
}
super.onBackPressed();
}
}
public class PlayersFragment extends android.support.v4.app.DialogFragment {
ListView lv;
String[] players = {"image1", "image2", "image3", "image4", "image5", "image6", "image7",
"image8", "image9", "image10", "image11", "image12", "image13", "image14"};
int[] images = {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4,
R.drawable.image5, R.drawable.image6, R.drawable.image7, R.drawable.image8, R.drawable.image9, R.drawable.image10,
R.drawable.image11, R.drawable.image12, R.drawable.image13, R.drawable.image14,};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialog, container, false);
//initialize listview
lv = (ListView) rootView.findViewById(R.id.listView1);
//create adapter obj and set list view to it
Adapter adapter = new Adapter(getActivity(), players, images);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int pos, long id) {
Toast.makeText(getActivity(), players[pos], Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
you can use onItemClicklistener for your listview :
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
if(pos == 0)
{
Intent intent = new Intent(getActivity() , Activity2.class);
startActivity(intent);
}
else if(pos == 1)
{
Intent intent = new Intent(getActivity() , Activity3.class);
startActivity(intent);
} and so on .......
}
});