0

I am trying to load the data in database to the List view. And that ListView is in the Framelayout.

This is my layout file,

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="60dp"
    android:gravity="bottom" 
    android:orientation="horizontal">

    <ListView
        android:id="@+id/list_customers"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal" >

    </ListView>

    <TextView
        android:id="@+id/txtsimple_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TextView>
</LinearLayout>

</FrameLayout>

This is my adapter class,

 public class ListCustomersAdapter extends BaseAdapter {

public Context ctx;
public String[] customers;
LayoutInflater mInflater;

public ListCustomersAdapter(Context c,
        String[] Customers ){
    super();
    mInflater = (LayoutInflater) c
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.ctx=c;
    this.customers=Customers;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return customers.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View localView;
    if (convertView == null) {
        localView = mInflater
                .inflate(R.layout.activity_main, null);
    } else {
        localView = convertView;
    }

    TextView txtCustomername=(TextView)localView.findViewById(R.id.txtsimple_list);
    txtCustomername.setText(customers[position]);
    txtCustomername.setLayoutParams(new LinearLayout.LayoutParams(
               LinearLayout.LayoutParams.WRAP_CONTENT,
               LinearLayout.LayoutParams.WRAP_CONTENT));
    return txtCustomername;
}
}

This is my activity class where I am setting the adapter to listView,

 public class MainActivity extends Activity {

ListView lsCustomers;
ListCustomersAdapter adapter;
public Context context = MainActivity.this;
ArrayList<String> results;
String[] Customers;
Button btnaddcustomers;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    results = new ArrayList<String>();
    btnaddcustomers=(Button)findViewById(R.id.btnadd);

    Customers = new String[results.size()];
    for (int i = 0; i < results.size(); i++) {
        Customers[i] = results.get(i);
    }
    adapter = new ListCustomersAdapter(context, Customers);
    lsCustomers = (ListView) findViewById(R.id.list_customers);
    Log.d("","Error1: "); //I am getting error after this line
    lsCustomers.setAdapter(adapter);
    }catch(Exception e){
        Toast.makeText(context, "No record found", Toast.LENGTH_LONG);
    }
    DB.close();

    btnaddcustomers.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MainActivity.this, AddCustomers.class);
            startActivity(intent);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void addCustomers(View paramView) {

    Intent intent = new Intent(MainActivity.this, AddCustomers.class);
    startActivity(intent);
}
 }

When I run the app, I am getting the exception,

06-27 11:03:55.734: E/AndroidRuntime(11560): FATAL EXCEPTION: main
06-27 11:03:55.734: E/AndroidRuntime(11560): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.widget.ListView.measureScrapChild(ListView.java:1170)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1253)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.widget.ListView.onMeasure(ListView.java:1162)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.view.View.measure(View.java:8313)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.view.View.measure(View.java:8313)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.view.View.measure(View.java:8313)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.view.View.measure(View.java:8313)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.view.View.measure(View.java:8313)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.view.View.measure(View.java:8313)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.view.ViewRoot.performTraversals(ViewRoot.java:845)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.os.Looper.loop(Looper.java:130)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.app.ActivityThread.main(ActivityThread.java:3687)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at java.lang.reflect.Method.invokeNative(Native Method)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at java.lang.reflect.Method.invoke(Method.java:507)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at dalvik.system.NativeStart.main(Native Method)

Please correct my code. Where I am doing wrong? I am getting irritated with this exception. And please specify why I am getting this error.

Any help is appreciated!! Thanks!!

1
  • hi i am not sure but your adapter problem...please use custom row layout and try again.. Commented Jun 27, 2013 at 5:51

1 Answer 1

4

The problem is that you are using the same layout for the Adapter as well as for Activity.

See, You set listview in activity_main.xml and textview in which you want to set the value from the database.

I suggest you to refer this example about how to fetch value from database and show in listview.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.