Ok, I have spent days going through the forums and have come up with no solution.
I am trying to retrieve all records from a database and show them in a listView
public class list_loans extends Activity {
ListView loanList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_loans);
//Instantiate Database helper
DataBaseHelper myDbHelper = new DataBaseHelper(this);
ArrayList<personObject> myArray = new ArrayList<>();
//Open writable database
SQLiteDatabase db = myDbHelper.getWritableDatabase();
Cursor resultSet = db.rawQuery("SELECT * FROM borrower", null);
//TODO: Print out single list of item, not list for each item!**
if (resultSet != null) {
personObject myPerson = null;
if (resultSet.moveToFirst()) {
do {
String nameOut = resultSet.getString(1);
String phoneOut = resultSet.getString(2);
String addressOut = resultSet.getString(3);
Integer itemID = resultSet.getInt(4);
String itemType = resultSet.getString(5);
String itemDesc = resultSet.getString(6);
myPerson = new personObject(nameOut, phoneOut, addressOut, itemID, itemType,
itemDesc);
}while (resultSet.moveToNext());
}
myArray.add(myPerson);
loanList = (ListView) findViewById(R.id.loanList);
//TODO: loop through array and add each item to list, instead of entire array to list**
loanList.setAdapter(new listAdapter(this, myArray));
resultSet.close();
db.close();
}
}
My adapter:
package com.creativenative.jonathan.loanme;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by Jonathan on 7/17/2015.
*/
class listAdapter extends BaseAdapter {
Context context;
ArrayList data;
private static LayoutInflater inflater = null;
public listAdapter(Context context, ArrayList data) {
this.context = context;
this.data = data;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (vi == null)
vi = inflater.inflate(R.layout.row, null);
TextView text = (TextView) vi.findViewById(R.id.text);
text.setText(data.toString());
return vi;
}
}
The expected output is:
Header Item 1
Item 1 details
Header Item 2
Item 2 details
Actual output:
Header item 1
Item 1 details
Header item 2
Item 2 details
Header item 1
Item 1 details
Header item 2
Item 2 details
Each time i add an item to the database, it adds another list item that contains everything in the database:
Add another item output:
Header item 1
Item 1 details
Header item 2
Item 2 details
Header item 3
Item 3 details
Header item 1
Item 1 details
Header item 2
Item 2 details
Header item 3
Item 3 details
Header item 1
Item 1 details
Header item 2
Item 2 details
Header item 3
Item 3 details
I can't seem to figure out how to get each item in it's own individual spot on the list view. Any help is much appreciated, as usual...Also, I know this problem has been addressed before, but even going through the other posts has not helped.
Aucun commentaire:
Enregistrer un commentaire