mardi 6 octobre 2015

Listview with Custom ArrayAdapter SQLite

So I'm trying to create a custom ListView with a custom ArrayAdapter from SQLite in my custom ArrayAdapter the data will not display on ListView...ie(the ListView will be shown but the TextView text is not shown in that ListView. Below is my code

Subcategory.java

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subcategory_layout);
listView= (ListView) findViewById(R.id.listView2);

//Intent i = getIntent();

dbHelper = new SqlLiteDbHelper(this);
try {
    dbHelper.openDataBase();
} catch (SQLException e) {
    e.printStackTrace();
}
sqLiteDatabase=dbHelper.getReadableDatabase();
cursor=dbHelper.getsubcategory(sqLiteDatabase);
subcategoryAdapter = new SubcategoryAdapter(getApplicationContext(),R.layout.sublist_item);
listView.setAdapter(subcategoryAdapter);
if (cursor.moveToFirst())
{
    do {
        String subcategory;
        subcategory=cursor.getString(0);
        foodSupply= new FoodSupply(subcategory);
        subcategoryAdapter.add(foodSupply);

    }while (cursor.moveToNext());
}

SubcategoryAdapter

public class SubcategoryAdapter extends ArrayAdapter {
List list = new ArrayList();
public SubcategoryAdapter(Context context, int resource) {
super(context, resource);
}
@Override
public void add (Object object){
super.add(object);
list.add(object);
}
static class LayoutHandler {
    TextView SUBCATEGORY;
}

@Override
public int getCount () {
return list.size();
}

@Override
public Object getItem ( int position){
return list.get(position);
}

@Override
public View getView ( int position, View convertView, ViewGroup parent){

View row = convertView;
LayoutHandler layoutHandler;
if (row == null) {
    LayoutInflater layoutInflater = (LayoutInflater)   this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    row = layoutInflater.inflate(R.layout.sublist_item, parent, false);
    layoutHandler = new LayoutHandler();
    layoutHandler.SUBCATEGORY = (TextView) row.findViewById(R.id.textView2);
    row.setTag(layoutHandler);
} else {
    layoutHandler = (LayoutHandler) row.getTag();
}
FoodSupply foodSupply = (FoodSupply) this.getItem(position);
layoutHandler.SUBCATEGORY.setText(foodSupply.getSubcategory());
return row;

}

Subcategory_layout

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView2"
    ></ListView>

Sublist_item

<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView2"
    android:layout_gravity="center_horizontal"
    android:textColor="#000000"
    />

Aucun commentaire:

Enregistrer un commentaire