Thursday, 12 September 2013

Extending ListActivity android.R.id.list

Extending ListActivity android.R.id.list

I am creating a Calculator. When calculations are made, I need them
displayed, then clickable so I can delete individual figures. I followed
this tutorial, and got this far. The only problem is when I run it, I get
a FC and my logcat says this:
Caused by: java.lang.RuntimeException: Your content must have a ListView
whose id attribute is 'android.R.id.list'
All of my research say to change my id to android.R.id.list. I tried this,
but still getting the same error.
Here is my code:
calculator.java
public class Slab extends ListActivity {
//declare a bunch of variables here.
ArrayList<Calculations> newList = null;
SimpleAdapter adapter = null;
private CustomListAdapter newAdapter=null;
private int i = 0;
public ArrayList<LauncherActivity.ListItem> myItems = new
ArrayList<LauncherActivity.ListItem>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initialize();
validate();
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//do my calculations here
//display the figures
newList = new ArrayList<Calculations>();
Calculations info = new Calculations();
info.SetType("Slab figure " + figureCount);
info.SetFigure(wide + " wide " + long + " long " +
thick + " thick");
info.SetFigureAmount(CubicYd+"");
newList.add(info);
if(newList!=null && newList.size() > 0){
newAdapter.notifyDataSetChanged();
newAdapter.add(newList.get(0));
i++;
}
newAdapter.notifyDataSetChanged();
}
}
});
}
private void initialize() {
newList = new ArrayList<Calculations>();
newAdapter = new CustomListAdapter(this, R.layout.list_item, newList);
setListAdapter(this.newAdapter);
//other irrelevant declarations
}
}
CustomAdapter.java
public class CustomListAdapter extends ArrayAdapter<Calculations> {
private Context appContext = null;
private ArrayList<Calculations> items = null;
public CustomListAdapter(Context context, int textViewResourceId,
ArrayList<Calculations>items){
super(context, textViewResourceId, items);
this.appContext = context;
this.items=items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null) {
LayoutInflater vi =
(LayoutInflater)appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item, null);
}
Calculations c = items.get(position);
if (c != null){
TextView type = (TextView) v.findViewById(R.id.tvType);
TextView figure = (TextView) v.findViewById(R.id.tvFullFigure);
TextView amount = (TextView) v.findViewById(R.id.tvAmount);
type.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(appContext, "This item was clicked",
Toast.LENGTH_LONG).show();
}
});
if(type!=null) {
type.setText(c.getType());
}
if(figure!=null){
figure.setText(c.getFigure());
}
if(amount!=null){
amount.setText(c.getFigureAmount());
}
}
return v;
}
}
Calculations.java
public class Calculations {
private String type = "";
private String figure = "";
private String figureTotal = "";
public void SetType(String type){
this.type = type;
}
public String getType(){
return this.type;
}
public void SetFigure(String figure) {
this.figure = figure;
}
public String getFigure(){
return this.figure;
}
public void SetFigureAmount(String figureTotal){
this.figureTotal = figureTotal;
}
public String getFigureAmount(){
return this.figureTotal;
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textSize="20dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/tvType"
android:layout_gravity="left|center_vertical" />
<TextView
android:textSize="15dp"
android:textStyle="bold"
android:layout_below="@+id/tvType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/tvFullFigure"
android:layout_gravity="right|center_vertical" />
<TextView
android:textSize="15dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/tvAmount"
android:layout_gravity="left|center_vertical"
android:layout_below="@+id/tvType"
android:layout_alignParentRight="true" />
</RelativeLayout>
acitivty_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="15dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:background="@drawable/concret_back"
>
/////**********there is a bunch of edit text boxes, buttons, and text
views taking up the upper half of the screen *********////////
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/Buttonslayout"
android:layout_above="@+id/tvTotal"
>
<ListView
android:id="@+id/android.R.id.list"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
</ScrollView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:paddingTop="5dp"
android:text=""
android:textStyle="bold"
android:id="@+id/tvTotal"
android:paddingBottom="15dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:textColor="#FFFFFF"
android:textSize="20dp"/>
</RelativeLayout>
I have had this problem with another app I have done and never found a
solution. So I though I would ask why do I keep getting the Caused by:
java.lang.RuntimeException: Your content must have a ListView whose id
attribute is 'android.R.id.list' error?
Thanks for any input.

No comments:

Post a Comment