So I created a custom ListView with 2 objects in the list. For example, the first on the list is Cats and the second is Dogs. Then when one is tapped, it will navigate to a different activity screen. So for example, when the user taps "Dogs", it will go to a different screen with a new list with names on the list such as "Corgi", "Pug", "Husky", etc. Then when a dog breed is tap, it will forward to a website. For example, when the user taps "Corgi", it will forward the user to Corgi's Wiki page.
How will I go about creating the third part of the app?
My code is the following:
MainActivity:
public class MainActivity extends ListActivity implements OnItemClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] data = { "Dogs", "Cats" };
int[] icons = { R.drawable.dogz, R.drawable.catz };
// Provide the cursor for the list view.
setListAdapter(new CustomListAdapter(this, data, icons));
/* setOnItemClickListener() Register a callback to be invoked when an item
* in this AdapterView has been clicked.
*/
getListView().setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(parent.getContext(), ChildActivity.class);
// Add extended data to the intent.
intent.putExtra("POSITION", position);
/*
* Launch a new activity. You will not receive any information about when
* the activity exits. This implementation overrides the base version,
* providing information about the activity performing the launch.
*/
startActivity(intent);
}
}
ChildActivity:
public class ChildActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[][] data = {
{ "Corgi", "Pugs", "Husky" },
{ "Siamese", "Persian", "Maine Coon" } };
int[][] icons = {
{ R.drawable.d, R.drawable.e, R.drawable.f },
{ R.drawable.a, R.drawable.b, R.drawable.c }, };
Intent intent = getIntent();
int position = intent.getIntExtra("POSITION", 0);
// Provide the cursor for the list view.
setListAdapter(new CustomListAdapter(this, data[position],
icons[position]));
}
}
CustomListAdapter:
public class CustomListAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private final int[] icons;
public CustomListAdapter(Context context, String[] values, int[] icons) {
super(context, R.layout.row_layout, values);
this.context = context;
this.values = values;
this.icons = icons;
}
@Override
// Get a View that displays the data at the specified position in the data set.
public View getView(int position, View convertView, ViewGroup parent) {
/*
* Instantiates a layout XML file into its corresponding View objects.
* It is never used directly. Instead, use getSystemService(String) to
* retrieve a standard LayoutInflater instance that is already hooked up to
* the current context and correctly configured for the device you are running on.
*/
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
/*
* Inflate a new view hierarchy from the specified xml resource.
* Throws InflateException if there is an error.
*/
View rowView = inflater.inflate(R.layout.row_layout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.txtStatus);
textView.setText(values[position]);
Drawable draw = context.getResources().getDrawable(icons[position]);
Bitmap bitmap = ((BitmapDrawable) draw).getBitmap();
int h = bitmap.getHeight();
int w = bitmap.getWidth();
Drawable newDraw = new BitmapDrawable(context.getResources(),
Bitmap.createScaledBitmap(bitmap, 40 * w / h, 40, true));
/*
* Sets the Drawables (if any) to appear to the left of, above, to the right of,
* and below the text. Use 0 if you do not want a Drawable there.
* The Drawables' bounds will be set to their intrinsic bounds.
*/
textView.setCompoundDrawablesWithIntrinsicBounds(newDraw, null, null,
null);
return rowView;
}
}
I know I would have to create a third activity but I do not know how to go about it without using the .xml. Thank in advance.