0

我想將數據庫中的數據加載到List視圖中。並且該ListView位於Framelayout中。

這是我的佈局文件,

<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> 

這是我的適配器類,

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

這是我的活動課我在哪裏設置適配器的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); 
} 
} 

當我運行應用程序時,我收到異常,

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) 

請更正我的代碼。我在哪裏做錯了?這個例外,我感到惱火。並請說明爲什麼我收到此錯誤。

任何幫助表示讚賞!謝謝!!

+0

嗨,我不知道,但您的適配器問題...請使用自定義行佈局,然後再試一次.. –

回答

4

問題是,您正在使用相同的佈局的適配器以及活動。

請參閱,您在中設置了activity_main.xmltextview其中您要設置數據庫中的值。

我建議你參考this關於如何從數據庫獲取值並在listview中顯示的例子。

相關問題