Search code examples
androidlistviewsimplecursoradapterlistactivity

Can't get view to set elements of LISTVIEW


have problem with 'listView'. I searched everywhere but the problem does not arise none. Let me explain:

I have classic 'listview' in my 'layout':

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/roza"
android:tileMode="repeat">

<ListView
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#80ffffff"
    android:alpha="200">
</ListView>

<TextView
    android:id="@android:id/empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/no_todos" />

 </LinearLayout>

And i create custom list_row xml:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">


    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/list_name"
    android:layout_alignParentTop="true"
    android:textSize="25dp"
    android:layout_alignParentStart="true"
    android:layout_alignEnd="@+id/list_rune" />

   ------------bla bla bla -----------------
   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"

    android:layout_alignTop="@+id/list_date_update"
    android:layout_toLeftOf="@+id/list_rune"
    android:layout_toStartOf="@+id/list_rune"
    android:id="@+id/textTester" />


    </RelativeLayout>

My main class -

  public class MainActivity extends ListActivity implements         AdapterView.OnItemLongClickListener,       SharedPreferences.OnSharedPreferenceChangeListener {


     private NordBase dbHelper;
   private Cursor cursor;
  int rage = list_row; // my custom row

  -------------------on create----------------
 setContentView(R.layout.activity_main);
 this.getListView().setDividerHeight(2);

       dbHelper = new NordBase(this);
       fillData();

------------------fillData-----------------

   cursor = dbHelper.getAll();
       startManagingCursor(cursor);


       String[] from = new String[] { NordBase.NORD_NAME,    NordBase.NORD_DESCRIPTION,
                                    NordBase.NORD_PUNKTS,    NordBase.NORD_PUNKTS_CROSS,
            NordBase.NORD_DATE_CREATE, NordBase.NORD_DATE_UPDATE, NordBase.NORD_RUNE,
            NordBase.NORD_IMPORTANT, NordBase.NORD_SUM };

      int[] to = new int[] { R.id.list_name, R.id.list_description, R.id.list_punkts,
            R.id.list_punkts_cross, R.id.list_date_create, R.id.list_date_update,
            R.id.list_rune, R.id.list_important, R.id.list_sum, };

    notes = new SimpleCursorAdapter(this,
            rage, cursor, from, to);


    setListAdapter(notes);
    getListView().setOnItemLongClickListener(this);

Alas, I can not get to the items inside list_row, to bind them for comparison operators. for exp: if price == 0, then textview.setvisible = false...

I try LayoutInflater, but he make view element difrent from those in listview. I try adapter binder class - but i think its no good for that. Mostly i have an error - can't do that for the "null element"... that case - looks like all i ever do - my program can't findViewById elements in the list_row.

There is one point of contact

     int[] to = new int[] { R.id.list_name, R.id.list_description, R.id.list_punkts,
                  R.id.list_punkts_cross, R.id.list_date_create, R.id.list_date_update,
                R.id.list_rune, R.id.list_important, R.id.list_sum, };

          notes = new SimpleCursorAdapter(this,
                rage, cursor, from, to);

So help me Obi-Van Kenobi, and sorry for my dummy ENG, and i'm not sure that i past cod in right way)))


Solution

  • First of all your layout is not having the views which you have mention in the adapter like

    R.id.list_description, R.id.list_punkts,
                      R.id.list_punkts_cross, R.id.list_date_create, R.id.list_date_update,
                    R.id.list_rune, R.id.list_important, R.id.list_sum
    

    put this thing in the layout list_row xml if you mean by ------------bla bla bla ----------------- this views than ok also simpleCursorAdapter will not solve your purpose as you want to hide and show the views based on the data in respective views so my first suggestion Change your layout to linearLayout or tableLayout or other layout as you feel and then do this in Your CustomAdapter class like

    class MyAdapter extends BaseAdapter
    {
       @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    //here you infalteYour layout and and do your logical thing
    //you can take your data out from cursor and store in the arrayList<YourModelClass> type and have better control
         }
    }
    

    otherwise in relative layout just fill the data don't hide it so it may work but if you do hiding of some views chances are high it will not solve your problem

    and typo mistake is there i hope so like

    int[] to = new int[] { R.id.list_name, R.id.list_description, R.id.list_punkts,
                      R.id.list_punkts_cross, R.id.list_date_create, R.id.list_date_update,
                    R.id.list_rune, R.id.list_important, R.id.list_sum, };
    

    //here no item but still u have written "," after R.id.list_sum this like "R.id.list_sum, "

              notes = new SimpleCursorAdapter(this,
                    rage, cursor, from, to);