이름, 가격, 설명이있는 제품 목록 (SQLite)을 표시해야합니다. 제품은 범주별로 그룹화됩니다. 내부의 모든 정보가있는 SQLite 테이블에서 제품을 가져옵니다. 테이블은 "preturi"라고 불리우며 "Categorie", "ProductName", "Price", "Description"필드가 있습니다.확장 가능 목록 복잡한 레이아웃 용 어댑터
그래서 내가 얻고 자하는 것은 ExpandableListView입니다 (아래 레이아웃 참조) :
"Category one"
"Product1", 25.22, "FirstProduct"
"Product2", 21.13, "SecondProduct"
"Product3", 16.24, "ThirdProduct"
"Category two"
"Product4", 5.72, "FourthProduct"
...
...
지금까지는 그룹/목록 만 가져 왔지만 Category/ProductName 만 표시되었습니다. 제품의 다른 두 가지 속성 (가격 및 설명)을 표시 할 수 있어야합니다. 이 두 개의 추가 필드를 보내고 팽창시키기 위해 코드를 어떻게 변경해야합니까? 내가
내 SQLiteTable에서 데이터와 함께 "lblPret"와 "lblDescription"레이블을 채우려 그래서 기본적으로list_group.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp"
android:background="#000000">
<TextView
android:id="@+id/lblListHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textSize="17dp"
android:text="Category here"
android:textColor="#f9f93d" />
</LinearLayout>
다음
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="200dp"
android:layout_height="65dip"
android:orientation="vertical" >
<TextView
android:id="@+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dip"
android:paddingTop="5dp"
android:text="Product name"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
<TextView
android:id="@+id/lblPret"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12dip"
android:paddingTop="2dp"
android:text="Price"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="65dip"
android:orientation="vertical" >
<TextView
android:id="@+id/lblDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12dip"
android:paddingTop="5dp"
android:text="Description"
android:paddingBottom="5dp"
android:paddingLeft="10dp" />
</LinearLayout>
</LinearLayout>
:
지금까지, 나는 다음과 같은 코드를 사용
확장형 목록을 표시하는 활동 (PreturiActivity)의 레이아웃은 다음과 같습니다.
다음과 같이 10activity_preturi.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="ro.mypack.myapppack.myapp.app.PreturiActivity">
<ExpandableListView
android:id="@+id/lvExp"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</RelativeLayout>
활동 클래스 코드는 다음과 같습니다
public class PreturiActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preturi);
expListView = (ExpandableListView) findViewById(R.id.lvExp); // getting the listview
prepareListData(); // preparing list data
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
expListView.setAdapter(listAdapter); // setting list adapter
}
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
String catego = "";
Integer i = 0;
final SQLiteDatabase db = new myDbHelper(getApplicationContext()).getWritableDatabase();
Cursor cy = db.rawQuery("SELECT DISTINCT categorie FROM preturi group by categorie", null);
if (cy != null) {
if (cy.moveToFirst()) {
do {
catego = cy.getString(cy.getColumnIndex("categorie"));
listDataHeader.add(catego);
Cursor cz = db.rawQuery("SELECT * FROM preturi ", null);
if (cz != null) {
if (cz.moveToFirst()) {
List<String> continut = new ArrayList<String>();
do {
catego = cz.getString(cz.getColumnIndex("den_produs"));
myprice = cz.getString(cz.getColumnIndex("price"));
mydescr = cz.getString(cz.getColumnIndex("description"));
continut.add(catego); // here I should be able to add more than just "catego" (product name)
} while (cz.moveToNext());
listDataChild.put(listDataHeader.get(i), continut);
}
}
i++;
}while (cy.moveToNext());
}
}
내가 뭔가 다른 어디에서 더 자세한 정보를 넣을 수 있습니다와 "HashMap<String, List<String>> listDataChild;"
및 List<String> continut = new ArrayList<String>();
를 교체해야 확신합니다. 그것이 무엇이고 어떻게 처리 할 수 있습니까?
@Override
public View getChildView(int groupPosition, final int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
TextView txtListChild_pret = (TextView) convertView.findViewById(R.id.lblPret);
txtListChild.setText(childText);
txtListChild_pret.setText(childText);
return convertView;
}
도와주세요 :
내가 사용하는 어댑터의 핵심은 이것이다 (당신이 그것을 볼 필요가없는 나는 여기에 어댑터 코드를 붙여하지 않지만, 그것은 꽤 표준입니다)
감사합니다
코드를 좀 알려주시겠습니까?나는 개념을 이해한다 (나는 그것 또한 생각했다). 그러나 나는 그것을 구현하는 데 약간의 어려움이있다. – user1137313
@ user1137313 신중하게 대답을 읽고 그것에 대해 연구해라. 생각을위한 음식 : getter/setter 메소드로 클래스를 정의하고, 이름, 세부 사항 및 가격으로 오브젝트를 준비하고이를 ArrayList에 넣어야합니다. 이 arraylist를 위의 예제를 참조하여 getChildView를 오버라이드해야 할 사용자 정의 어댑터에 전달하십시오. –
감사합니다. 나는 어댑터에서 같은 종류의 접근법을 사용한다. ArrayList 만 사용한다. 자습서가 설명하고 어댑터가 SQLite로 작성되지 않았으므로 우리가 말한대로 연구하고 있습니다. 그러나 조금 더 기대하고 있습니다 ... 나는 서둘러 큰 시간을 보내고 있습니다. ( – user1137313