Skip to content Skip to sidebar Skip to footer

Casting List To Listview That Was Generated From Sqlite

Quick Info I am following one of the example videos on Lynda.com titled App Development with Local Storage. I have the exercise files but I am attempting to follow using my own set

Solution 1:

Thanks to @Divyesh I found a solution to my problem.


Creating a custom ArrayAdapter did the trick:

  1. Create an additional layout view to include required widgets. 3 TextViews were created in my case under data_view.xml
  2. A list parser was created to extract the data from each row that is found inside the database and constructed as DataItem into listFromDB.
  3. The custom adapter was then defined which extends ArrayAdapter and assign the values to the TextViews after the data_view.xml has been inflated.
  4. Lastly, the new custom adapter was used in MainActivity and items from the list were assigned to the appropriate TextViews

The following is a sample of the steps mentioned above to which I am going to refactor later on.

data_view.xml

<TextView
    android:id="@+id/accountName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Account Name" />
<TextView
    android:id="@+id/description"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Description" />
<TextView
    android:id="@+id/balance"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Balance" />

ListParser

publicclassListParser {
    publicString accountName;
    publicString description;
    publicString balance;

    publicListParser(String accountName, String description, String balance) {
        this.accountName = accountName;
        this.description = description;
        this.balance = balance;
    }
}

Custom Adapter

publicclassAccountAdapterextendsArrayAdapter<ListParser> {
    publicAccountAdapter(Context context, ArrayList<ListParser> users) {
        super(context, 0, users);
    }

    publicstaticclassViewHolder {
        TextView accountName;
        TextView description;
        TextView balance;

        publicViewHolder(View convertView) {
             accountName = (TextView) convertView.findViewById(R.id.accountName);
             description = (TextView) convertView.findViewById(R.id.description);
             balance = (TextView) convertView.findViewById(R.id.balance);
        }
    }

    @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this positionListParseruser= getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.account_view, parent, false);
            viewHolder = newViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }

        // Lookup view for data populationTextViewaccountName= viewHolder.accountName;
        TextViewdescription= viewHolder.description;
        TextViewbalance= viewHolder.balance;
        // Populate the data into the template view using the data object
        accountName.setText(user.accountName);
        description.setText(user.description);
        balance.setText(user.balance);
        // Return the completed view to render on screenreturn convertView;
    }
}

MainActivity (Single Row)

ArrayList<ListParser> arrayOfListParsers = new ArrayList<ListParser>();
AccountAdapter adapter = new AccountAdapter(this, arrayOfListParsers);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);

ListParser newUser = new ListParser(String.valueOf(listFromDB.get(0).getaccountName()), String.valueOf(listFromDB.get(0).getDescription()), String.valueOf(listFromDB.get(0).getBalance()));
adapter.add(newUser);

More work needs to be done to the MainActivity Class, but this was just a test run.

Additional Credit: https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView

Post a Comment for "Casting List To Listview That Was Generated From Sqlite"