I write this code for get data from DataBase and show in listView. I want to show my data in my custom List with 3 textView and 1 ImageView. how can i do that?
This is my code :
private void Name() {
ListView tvName = (ListView) findViewById(R.id.lvListOfObject);
DataBase infoName = new DataBase(this);
infoName.open();
ArrayList<String> dataName = infoName.getDataName();
infoName.close();
tvName.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, dataName));
and this is my DataBase:
package com.kalagar.warehouse;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBase {
private static final String LOGTAG = "WAREHOUSE";
private static final String DATABASE_TABLE = "WareHouse";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "SellList";
public static final String ROW_ID = "_id";
public static final String ROW_NAME = "nameOfObject";
public static final String ROW_KHARID = "ghBuy";
public static final String ROW_FOROUSH = "ghSell";
public static final String ROW_PICTURE = "picture";
private static final String TABLE_CREATE = "CREATE TABLE " + DATABASE_TABLE
+ " (" + ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + ROW_NAME
+ " TEXT, " + ROW_KHARID + " NUMERIC, " + ROW_FOROUSH
+ " NUMERIC, " + ROW_PICTURE + " TEXT " + ")";
private WareHouseDdbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class WareHouseDdbHelper extends SQLiteOpenHelper {
public WareHouseDdbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
Log.i(LOGTAG, "Table has been create");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
onCreate(db);
}
}
public DataBase(Context c) {
ourContext = c;
}
public DataBase open() throws SQLException {
ourHelper = new WareHouseDdbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
public long createEntry(String name, String kharid, String foroush) {
ContentValues cv = new ContentValues();
cv.put(ROW_NAME, name);
cv.put(ROW_KHARID, kharid);
cv.put(ROW_FOROUSH, foroush);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public ArrayList<String> getDataName() {
String[] columns = new String[] { ROW_ID, ROW_NAME, ROW_KHARID,
ROW_FOROUSH };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
null, null);
ArrayList<String> result = new ArrayList<String>();
int iRow = c.getColumnIndex(ROW_ID);
int iName = c.getColumnIndex(ROW_NAME);
int iKharid = c.getColumnIndex(ROW_KHARID);
int iForoush = c.getColumnIndex(ROW_FOROUSH);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result.add(c.getString(iRow) + " " + c.getString(iName) + " "
+ c.getString(iKharid) + " " + c.getString(iForoush));
}
return result;
}
}
for that you need a customize list view first you should add a listview in your main.xml(just an eg) file then create a class like this
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
DataHelper dh;
public MySimpleArrayAdapter(Context context, int textViewResourceId, String[] values) {
super(context, textViewResourceId, values);
this.context = context;
this.values = values;
dh=new DataHelper(getApplicationContext());
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_name, parent, false);
textView = (TextView) rowView.findViewById(R.id.textname);
textView.setText(values[position]);
// Change the icon for Windows and iPhone
textView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Toast.makeText(this,""+values[position],10000).show();
}
});
return rowView;
}
}
R.layout.list_name this will be the new xml file which will load the contents to the list view
and the the final step just in your on create method do this
con = (ListView) findViewById(R.id.main_listView);
MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(MainActivity.this, R.id.main_listView ,data);// data is String array valu to be added in list view
//setting the adapter
con.setAdapter(adapter);
Source link
You can use custom listview adapter fro this.
Here are some examples.
Follow these steps:
POJO
class for those data which will be shown as list item. BaseAdapter
to create custom adapter. Suppose, you have created a custom adapter named CustomAdapter.java
and a POJO
class named RowItem.java
. Now assign this adapter into the Listview
as follows...
ListView tvName = (ListView) findViewById(R.id.lvListOfObject); CustomAdapter adapter = new CustomAdapter(this, List<RowItem>()); tvName.setAdapter(adapter);
You can follow this tutorials...
Android: Custom ListView with Image and Text using BaseAdapter
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.