Android : Convert ListActivity to FragmentActivity

on Wednesday, October 8, 2014


I am a beginner in android mobile app development. I have a problem in converting a ListActivity to FragmantActivity. This is my code for ListActivity:



public class MainActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<HighLightItem> items = HighLightItem.populateItems();
MyAdapter adapter = new MyAdapter(this, items);
setListAdapter(adapter);
}


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

}


and this is my HighLightItem class containing the arraylist:



public class HighLightItem {

String itemName;
int imageResourceRef;
String shortDescription;

public static ArrayList<HighLightItem> populateItems() {
ArrayList<HighLightItem> items = new ArrayList<HighLightItem>();

for (int x = 0; x < 10; x++) {

HighLightItem item = new HighLightItem();

switch (x) {
case 0: {
item.itemName = "Samsung Galaxy S5";
item.imageResourceRef = R.drawable.galaxy_s;
item.shortDescription = "Latest flagship device from Samsung.";
}
break;

case 1: {
item.itemName = "HTC One (M8)";
item.imageResourceRef = R.drawable.htc_one_m8;
item.shortDescription = "Latest flagship device from HTC.";
}
break;

case 2: {
item.itemName = "LG G3";
item.imageResourceRef = R.drawable.lg_g3;
item.shortDescription = "Latest flagship device from LG.";
}
break;

case 3: {
item.itemName = "LG G Watch";
item.imageResourceRef = R.drawable.lg_wear;
item.shortDescription = "Latest smartwatch from LG.";
}
break;

case 4: {
item.itemName = "Moto X";
item.imageResourceRef = R.drawable.moto_x;
item.shortDescription = "Latest smartphone from Motorola.";
}
break;

case 5: {
item.itemName = "Motorola 360";
item.imageResourceRef = R.drawable.motorola_360;
item.shortDescription = "Latest smartwatch from Motorola.";
}
break;

case 6: {
item.itemName = "Nexus 4 (Phone)";
item.imageResourceRef = R.drawable.nexus_4;
item.shortDescription = "Latest Nexus smartphone from LG and Google.";
}
break;

case 7: {
item.itemName = "Nexus 7 (Tablet)";
item.imageResourceRef = R.drawable.nexus_7;
item.shortDescription = "Latest Nexus tablet from Asus and Google.";
}
break;

case 8: {
item.itemName = "Nexus One";
item.imageResourceRef = R.drawable.nexus_one;
item.shortDescription = "One of the famous Nexus smartphone from HTC and Google.";
}
break;

case 9: {
item.itemName = "Xiaomi mi3";
item.imageResourceRef = R.drawable.xiaomi_mi3;
item.shortDescription = "Known for its Bang-for-the-buck trait!";
}
break;

}

items.add(item);
}

return items;
}

}


I tried to copy the contents of my ListActivity to a new class which now extends FragmentActivity. But I've got an error in this part:



setListAdapter(adapter);


I tried to change it to FragmentList.setListAdapter(adapter) but it was still an error. What is wrong with my code? I want to make this ListActivity to FragmentActivity without creating another xml layout (just via code). Do I need to override onCreateView(String name, Context context, AttributeSet attrs) ? If yes, what will I implement inside the onCreateView?


0 comments:

Post a Comment