1

这是我的安卓代码:

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 如何使我的应用程序能够获取在这些字段中找到的所有详细信息并在列表视图中详细说明它们?您的帮助将不胜感激!感谢:D

4

2 回答 2

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 个值。并使用 holder,这样您的列表视图就不会遇到重复值之类的问题。

这个代码对我有用。

于 2013-06-22T02:53:30.003 回答
0

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

于 2013-06-21T16:07:17.487 回答