gpt4 book ai didi

android - 带数据库的自定义ListView

转载 作者:行者123 更新时间:2023-11-30 03:32:20 26 4
gpt4 key购买 nike

这是我的安卓代码:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com