自定义ListView与数据库

时间:2013-06-21 15:56:36

标签: android database android-listview

这是我的Android代码:

public class Fragment1 extends SherlockFragment {
    ListView lista;
    ArrayList<String> items = new ArrayList<String>();
    private List<Listaadaptera> thelista = new ArrayList<Listaadaptera>();
    View rootView;
    ListView list;
    TextView title, price;

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu, inflater);
        MenuInflater blowup = this.getSherlockActivity()
                .getSupportMenuInflater();
        blowup.inflate(R.menu.menuxml, menu);
        return;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.pricelist, container, false);
        ActionBar ab = this.getSherlockActivity().getSupportActionBar();
        ab.setTitle("");
        title = (TextView) rootView.findViewById(R.id.title);
        price = (TextView) rootView.findViewById(R.id.details);

        list = (ListView) rootView.findViewById(R.id.list);
        SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(
                this.getSherlockActivity(), R.array.phones_array,
                android.R.layout.simple_spinner_dropdown_item);
        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        ab.setListNavigationCallbacks(mSpinnerAdapter, null);
        StrictMode.enableDefaults();
        getData();
        return rootView;
}
getdata();
*//Connecting to the database...

        try {
            JSONArray jArray = new JSONArray(result);
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json = jArray.getJSONObject(i);
                thelista.add(new Listaadaptera(json.getString("PhoneName"),
                        json.getString("PhonePrice"), R.drawable.lebanon,
                        R.drawable.rating));
            }

        } catch (Exception e) {
            Log.e("lag_tag", "ERROR PARSING DATA" + e.toString());
            Toast.makeText(getSherlockActivity(),
                    "Couldn't Connect To The Internet", Toast.LENGTH_LONG)
                    .show();
        }

    }

}

pricelist xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip" >

    <LinearLayout
        android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dip"
        android:background="@drawable/image_bg"
        android:padding="3dip" >

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="50dip"
            android:layout_height="50dip" />
    </LinearLayout>


    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="Smartphone Name"
        android:textColor="#040404"
        android:textSize="15dip"
        android:textStyle="bold"
        android:typeface="sans" />


    <TextView
        android:id="@+id/details"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_marginTop="1dip"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="Smartphone Price"
        android:textColor="#343434"
        android:textSize="10dip" />


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/rating" />

</RelativeLayout>

我在此代码中的目标是获取在数据库中注册的所有详细信息,并将其详细说明为此服装布局。 在表中我有2个字段,PhoneName和PhonePrice 如何让我的应用程序能够获取在这些字段中找到的所有详细信息并在listview中详细说明? 非常感谢您的帮助!谢谢:D

2 个答案:

答案 0 :(得分:1)

尝试使用此代码

         Cursor select = db.rawQuery("Select *from DBName", null);
         select.moveToFirst();
         int count = c.getCount();   
         String[] pname= new String[count];
         String[] pprice= new String[count];
         c.moveToFirst();
         for(int i = 0;i < count;i++){
                pname[i] = c.getString(c.getColumnIndex("PhoneName"));
                pprice[i] = c.getString(c.getColumnIndex("PhonePrice"));
                c.moveToNext();
            }
         ListView list = (ListView) findViewById(R.id.menulist);
         list.setAdapter(new listadapter(this,pname,pprice));

“listadapter”应该有2个PhoneName和Phone Address值。并使用持有人,这样您的列表视图就不会遇到像重复值这样的问题。

这个代码适合我。

答案 1 :(得分:0)

使用ArrayAdapter将您从数据库获取的值映射到您将用于在ListView中显示数据的行xml