Reputation: 13
Here is my Java:
public class Cantos extends AppCompatActivity {
ListView lv;
Context context;
ArrayList cantoList;
public static String[] cantos = {"1: Abre Tu Oido", "2: A Cristo Quiero", "3: Acerquese Mi Clamor", "4: A Cristo Yo Alabare",
"5: Acude Dios", "6: Adelante", "7: A Dios Canto", "8: Adios Para Siempre", "9: Ahora Senor", "10: A Jesucristo Ven",
"11: Alabad A Dios"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cantos);
initTypeface();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
toolbar.setNavigationIcon(getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Cantos.this, MainMenu.class);
startActivity(i);
}
});
}
private void initTypeface() {
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/AftaSerifThin-Regular.otf");
TextView text = (TextView) findViewById(R.id.toolbarCantos);
text.setTypeface(myTypeface);
}
}
Here is my other Java with BaseAdapter (Custom ListView):
public class CustomAdapter extends BaseAdapter {
String[] result;
Context context;
private static LayoutInflater inflater = null;
public CustomAdapter(Cantos cantos, String[] cantos1) {
result = cantos1;
context = cantos;
inflater = (LayoutInflater)context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return result.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder
{
TextView tv;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.cantos_list, null);
holder.tv = (TextView) rowView.findViewById(R.id.textCanto);
holder.tv.setText(result[position]);
Typeface myFont = Typeface.createFromAsset(context.getAssets(), "fonts/AftaSerifThin-Regular.otf");
holder.tv.setTypeface(myFont);
return rowView;
}
}
How can I make one certain item send me to another certain activity? Thank you in advance! P.S sorry if I spam too much! I am nearly done learning some of the basics! I beg you cope with me!
Upvotes: 0
Views: 49
Reputation: 403
First set adapter for your listview. then perform onItem click event.
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
displayAnotherListView(position);
}
});
public void displayAnotherListView(int position) {
switch (position) {
case 0: {
Intent i=new Intent(YourActivity.this,Activity_one.class);
startActivity(ii);
break;
}
case 1: {
Intent one=new Intent(YourActivity.this,Activity_two.class);
startActivity(one);
break;
}
case 2: {
Intent two = new Intent(YourActivity.this, Activity_three.class);
startActivity(two);
break;
}
}
}
you can do this for each item, but if you want to pass same intent for all no need to make method finding position, just pass intent on item click.
Upvotes: 0
Reputation: 21
If you want to call another activity on selection of item in list view you can call OnClick
listener to textview
in custom adapter
Code:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.cantos_list, null);
holder.tv = (TextView) rowView.findViewById(R.id.textCanto);
holder.tv.setText(result[position]);
holder.tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(context,NewAcivity.class);
((Activity) context).startActivity(intent);
}
});
Typeface myFont = Typeface.createFromAsset(context.getAssets(), "fonts/AftaSerifThin-Regular.otf");
holder.tv.setTypeface(myFont);
return rowView;
}
Upvotes: 1
Reputation: 81
Use intent.putExtra("key", value) & getExtra() to send the data from one activity to another activity
//try this
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent;
intent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(intent)
}
});
Upvotes: 0