Hi I'm new to stackoverflow and java too. Can someone help in solving the issue that I'm facing when I display data in listview. ListView shows record like : com.example.ayeshanaeem.addviewdata.Record@72d730 follwing is code: ViewListContents.java
public class ViewListContents extends AppCompatActivity {
DatabaseHelper myDB;
private ClipData myClip;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.viewcontents_layout);
final ListView listView = (ListView)findViewById(R.id.listView);
myDB = new DatabaseHelper(this);
final ArrayList<Record> theList = new ArrayList<>();
Cursor data = myDB.getAllData();
if(data.getCount() == 0){
Toast.makeText(ViewListContents.this, "The Database was empty", Toast.LENGTH_LONG).show();
}
else{
while(data.moveToNext()){
Record r=new Record();
r.setRecordRollno(data.getString(1));
r.setRecordName(data.getString(2));
theList.add(r);
ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, theList);
listView.setAdapter(listAdapter);
}
}
//To copy text of item on click in listview
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
String getstring = theList.get(i).toString();
myClip = ClipData.newPlainText("text",getstring);
clipboard.setPrimaryClip(myClip);
Toast.makeText(getApplicationContext(),"Text copied", Toast.LENGTH_LONG).show();
}
});
}
}
I have made Record.java + RecordAdapter.java + MainActivity.java too This is DatabaseHelper.java
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT, RollNo TEXT, NAME TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean insertData(String rollno,String name) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,rollno);
contentValues.put(COL_3,name);
long result = db.insert(TABLE_NAME,null ,contentValues);
if(result == -1)
return false;
else
return true;
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}
}
Use viewholder pattern and make a custom view to draw each row for your listview.
https://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html
Also you should use Realm to an alternative database to manage your information through objects.
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.