0

I used custom adapter for viewing listview. I tried to define my variables in adapter class but it didnt work. I find a temporary solution using invisible textview for this but i need to solve this problem. How can I overcome this?

Edit 1>

I have a column named date and this column contains datas like 20180723182036 and i grab this data to a list. I want to place this to listview as a variable and i will use this variable later.

My temporary solution is:

I have a code like that in database:

public ArrayList<SimpleNoteItem> getAllData() {
    openDatabase();
    ArrayList<SimpleNoteItem> newList = new ArrayList<>();
    String[] columns = {KEY_ROWTITLE, KEY_ROWCONTENT, KEY_ROWDATE, KEY_ROWSTRDATE};
    Cursor cs = db.query(DB_TABLE_NORMAL, columns, null, null, null, null, null);
    while(cs.moveToNext()){
        SimpleNoteItem allData = new SimpleNoteItem(cs.getString(0), cs.getString(1), cs.getLong(2), cs.getString(3));
        newList.add(allData);
    }
    Collections.reverse(newList);
    Log.d(LOGDB, "GETALLDATA STRUCTURE WORKS WELL!");
    return newList;
}

Listview longclick constructor is this:

simpleNotesDisplay.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, final long id) {
            Log.d(TAG, "LONG CLICKED " + position + " ITEM!");
            //THIS AREA WILL BE EDITED FOR MORE ANDROID VERSION SUPPORT
            AlertDialog.Builder builder;
            final TextView getInvisibleDate = (TextView) view.findViewById(R.id.tv_date_invisible);
            builder = new AlertDialog.Builder(NotesActivity.this, android.R.style.Theme_Material_Dialog_Alert);
            builder.setTitle("Delete?")
                    .setMessage("Do you want to send this note to hell?")
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            db.deleteSimpleNote(getInvisibleDate.getText().toString());
                            Log.d(TAG, "DELETING SUCCESSFUL ON ID = " + id + "!!!");
                            NotesActivity.this.recreate();
                        }
                    })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //empty
                        }
                    })
                    .show();

            return true;
        }
    });
}

And at least my invisible date:

<TextView
        android:id="@+id/tv_date_invisible"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="TextView"
        android:visibility="invisible"
        tools:layout_editor_absoluteX="351dp"
        tools:layout_editor_absoluteY="65dp" />

i hope it can help!

tesla.sh
  • 22
  • 3

2 Answers2

0

you should better to explain more about what you want but:

1)if you want to show some data in listview you most have a list of objects that each one has its data.

2)if you want to show different data in each row of listview then you most fill different data in each object of your list

3) if you want to show different view of each listviews row you most at first define different xml layouts and in your adapter where you define infelater you can use if else statment to decide which layout shows to witch row. below links will help you

Android ListView with different layouts for each row

https://www.javacodegeeks.com/2014/08/android-listview-with-multiple-row-layout.html

hamed
  • 148
  • 11
0

I found a different solution for that>>

I grab all data from database with this code:

public ArrayList<SimpleNoteItem> getAllData() {
    openDatabase();
    ArrayList<SimpleNoteItem> newList = new ArrayList<>();
    String[] columns = {KEY_ROWTITLE, KEY_ROWCONTENT, KEY_ROWDATE, KEY_ROWSTRDATE};
    Cursor cs = db.query(DB_TABLE_NORMAL, columns, null, null, null, null, null);
    while(cs.moveToNext()){
        SimpleNoteItem allData = new SimpleNoteItem(cs.getString(0), cs.getString(1), cs.getLong(2), cs.getString(3));
        newList.add(allData);
    }
    Collections.reverse(newList);
    Log.d(LOGDB, "GETALLDATA STRUCTURE WORKS WELL!");
    return newList;
}

And in another activity i grab this list with this and then initialized it:

private ArrayList<SimpleNoteItem> getList = new ArrayList<>();
getList = db.getAllData();

initialize should be in onCreate method.

And then i used that data:

simpleNotesDisplay.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, final long id) {
            Log.d(TAG, "LONG CLICKED " + position + " ITEM!");
            //THIS AREA WILL BE EDITED FOR MORE ANDROID VERSION SUPPORT
            AlertDialog.Builder builder;
            builder = new AlertDialog.Builder(NotesActivity.this, android.R.style.Theme_Material_Dialog_Alert);
            builder.setTitle("Delete?")
                    .setMessage("Do you want to send this note to hell?")
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            db.deleteSimpleNote(String.valueOf(getList.get(position).getSimpleNoteDate()));
                            Log.d(TAG, "DELETING SUCCESSFUL ON ID = " + id + "!!!");
                            NotesActivity.this.recreate();
                        }
                    })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //empty
                        }
                    })
                    .show();

            return true;
        }
    });
tesla.sh
  • 22
  • 3