android - 带数据库的自定义ListView

标签 android database android-listview

这是我的安卓代码:

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

    }

}

价目表 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

最佳答案

尝试使用这些代码

         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。并使用 holder 这样你的 ListView 就不会遇到像重复值这样的问题。

此代码适合我。

关于android - 带数据库的自定义ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17239509/

相关文章:

database - ORM 是否适用于非 CRUD 数据库?

database - InterSystems Caché - 如何访问 Caché Studio?

android - java.io.IOException : BufferedInputStream is closed 异常

java - 使用 ContentResolver 更新联系人组时更新不起作用

android - 更改粗体 html 文本的文本颜色

c# - 什么是存储大量对象的好方法,这些对象将被持久地修改/搜索?

android - OnvideoClicklistener 问题转到 YouTube 主页

android 在 ListView 中更改行项目 View 相关的一个 fragment

android - 使用 Intent 传递数据会导致应用程序崩溃

android - 在android中自定义操作栏首先显示默认操作栏