0

setting the text view according its position in of the row a list view filled from SimpleCursorAdapter from database on the click of the onlistitemclicked my custom list view has an image& textview for a name and another invisible text view which will be visible and be setting with a different number on the row clicked only but the problem is that when i clicked any row the text appeared on the view at the first row only whatever the row i clicked and I tried to use the set and get methods but i found its used for the Base Adapter. and the textview which will visible is not from data base can some one tell me how to do it please

here is a part of the code

public class Select_players_two extends ListActivity
{

    protected static class RowViewHolder
    {
        public TextView tvOne;
        public TextView tvTwo;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(android.view.Menu menu)
    {
        // TODO Auto-generated method stub
        return super.onCreateOptionsMenu(menu);
    }

    public class CustConAdpterSelect extends SimpleCursorAdapter
    {

          int i = 0;
          int count;
          private int layout;
          LayoutInflater inflator;
          final SQLiteConnector sqlCon = new SQLiteConnector(mContext);
          private ImageButton editBtn;
          private ImageButton delBtn;
          int id ;
          TextView txt_select;
          CharSequence txt_char;
          static final String KEY_No = "playerNo";
        public CustConAdpterSelect(Context context, int layout, Cursor c,
                String[] from, int[] to, int flags)
        {
            super(context, layout, c, from, to,0);
            this.layout = layout;
            inflator= LayoutInflater.from(context);
        }

         public View newView(Context context, Cursor cursor, ViewGroup parent)
            {
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                View retView = inflater.inflate(R.layout.lv_name_photo, parent, false);

                RowViewHolder holder = new RowViewHolder();
                holder.tvOne = (TextView) retView.findViewById(R.id.name);
                holder.tvTwo = (TextView) retView.findViewById(R.id.txt_number);
         //       holder.tvOne.setOnClickListener(tvOneLapOnClickListener);
                retView.setTag(holder);

                return retView;
            }
        @Override
        public void bindView(View v, final Context context, Cursor c)
        {
            editBtn=(ImageButton) v.findViewById(R.id.edit_btn);
             if( editBtn.getVisibility() == View.VISIBLE )
                 editBtn.setVisibility(View.INVISIBLE);
                else
                    editBtn.setVisibility(View.INVISIBLE);

             //set delete button invisble
             delBtn=(ImageButton) v.findViewById(R.id.del_btn);
             if( delBtn.getVisibility() == View.VISIBLE )
                 delBtn.setVisibility(View.INVISIBLE);
                else
                    delBtn.setVisibility(View.INVISIBLE);

            //final int 
             id = c.getInt(c.getColumnIndex(Contacts.ID));
            final String name = c.getString(c.getColumnIndex(Contacts.NAME));
            final String phone = c.getString(c.getColumnIndex(Contacts.PHONE));
            final String email = c.getString(c.getColumnIndex(Contacts.MAIL));
            final String fb = c.getString(c.getColumnIndex(Contacts.FB));
            final byte[] image = c.getBlob(c.getColumnIndex(Contacts.IMAGE));

            ImageView iv = (ImageView) v.findViewById(R.id.photo);

            if (image != null)
            {
                if (image.length > 3)
                {
                    iv.setImageBitmap(BitmapFactory.decodeByteArray(image, 0,image.length));
                }
            }

            TextView tname = (TextView) v.findViewById(R.id.name);
            tname.setText(name);
            TextView tphone = (TextView) v.findViewById(R.id.phone);
            tphone.setText(phone);
            TextView temail = (TextView) v.findViewById(R.id.email);
            temail.setText(email);
            txt_select=(TextView)v.findViewById(R.id.txt_number);

        //  final SQLiteConnector sqlCon = new SQLiteConnector(context);

            //for( i = 0; i <=4; i++)   
        /*   
            v.setOnClickListener(new View.OnClickListener() 
            {

                @Override
                public void onClick(View v) 
                {
                    //onitem clicked

         txt_char = txt_select.getText();
        {   
            if (txt_char != null)
                {
                    int txt_int = Integer.parseInt(txt_char.toString());
                    int count = txt_int;
                    Log.d("count1",String.valueOf(count));
                    count++;
                     txt_select.setText(String.valueOf(count));
                     Log.d("count",String.valueOf(count));
                     if( txt_select.getVisibility() == View.INVISIBLE )
                         txt_select.setVisibility(View.VISIBLE);
                        else
                            txt_select.setVisibility(View.INVISIBLE);
                     Log.d("number", String.valueOf(i));

                }



                /*   if( txt_select.getVisibility() == View.INVISIBLE )
                         txt_select.setVisibility(View.VISIBLE);
                        else
                            txt_select.setVisibility(View.INVISIBLE);
                     i++;
                     Log.d("number", String.valueOf(i));*/
            /*  }
                }
              });
            */
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
              final View view = super.getView(position, convertView, parent);
                final TextView textView = (TextView)view.findViewById(R.id.txt_number);

    /*          textView.setOnClickListener(new OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {

                        Log.i("Click", "TextView clicked on row " + position);
                        //  textView.setTag(position);              
                        txt_char = txt_select.getText();
                           Log.d("txt", txt_char.toString());
                        if (txt_char != null)
                        {
                            int txt_int = Integer.parseInt(txt_char.toString());
                            Log.d("txt2", txt_char.toString());
                            int count = txt_int;
                            Log.d("count1",String.valueOf(count));
                            count++;
                            txt_select.setText(String.valueOf(count));

                             CharSequence txt_char2 = txt_select.getText();
                             Log.d("ttxt", txt_char2.toString());
                             Log.d("count",String.valueOf(count));
                             if( txt_select.getVisibility() == View.INVISIBLE )
                                 txt_select.setVisibility(View.VISIBLE);
                                else
                                    txt_select.setVisibility(View.INVISIBLE);
                             Log.d("number", String.valueOf(i));
                             i++;  
                        }



        }*/
               // });   // TODO Auto-generated method stub
        return view;

        }

         private OnClickListener  tvOneLapOnClickListener = new OnClickListener()
         {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 // get the RowViewHolder
                RowViewHolder holder = new RowViewHolder();
                // Get the holder for the row
                holder = (RowViewHolder) ((View) v.getParent()).getTag();
                if (holder.tvOne.getVisibility() == View.INVISIBLE)
                    holder.tvOne.setVisibility(View.VISIBLE);
                else
                    holder.tvOne.setVisibility(View.INVISIBLE);
            }


        };





    }       

}

that is the database class "SQLiteConnector"

    public class SQLiteConnector 
{
    private SQLiteDatabase db;
    private SQLiteHelper sqlHp,sqlhpc;
    private Cursor cur,curc;

    public SQLiteConnector(Context context)
    {
        sqlHp = new SQLiteHelper(context, Contacts.DB_NAME, null, 1);
        sqlhpc = new SQLiteHelper(context, Contacts.DB_NAME, null, 1);

    }
    // insert new player in the list//
    public void insertContact(String name, String phone, String mail,String fb,byte[] blob) {
        ContentValues cv = new ContentValues();
        cv.put(Contacts.NAME, name);
        cv.put(Contacts.PHONE, phone);
        cv.put(Contacts.MAIL, mail);
        cv.put(Contacts.FB, fb);
        cv.put(Contacts.IMAGE,blob);
        db = sqlHp.getWritableDatabase();
        db.insert(Contacts.TABLE, null, cv);
        db.close();
    }
    // insert the score sheet //
    public void insertContact_score(String score,  String  num_call, String  num_collection ,String shape_type,
                            String score_sec,  String  num_call_sec, String  num_collection_sec ,String shape_type_sec,
                            String score_third,  String  num_call_third, String  num_collection_third ,String shape_type_third,
                  String score_forth,  String  num_call_forth, String  num_collection_forth ,String shape_type_forth)
    {
        ContentValues cvscore = new ContentValues();
        cvscore.put(Contacts.SCORE_st, score);
        cvscore.put(Contacts.NUM_CALL_st, num_call);
        cvscore.put(Contacts.NUM_COLLECTION_st, num_collection);
        cvscore.put(Contacts.SHAPE_CALL_st, shape_type);

        cvscore.put(Contacts.SCORE_sec, score_sec);
        cvscore.put(Contacts.NUM_CALL_sec, num_call_sec);
        cvscore.put(Contacts.NUM_COLLECTION_sec, num_collection_sec);
        cvscore.put(Contacts.SHAPE_CALL_sec, shape_type_sec);

        cvscore.put(Contacts.SCORE_third, score_third);
        cvscore.put(Contacts.NUM_CALL_third, num_call_third);
        cvscore.put(Contacts.NUM_COLLECTION_third, num_collection_third);
        cvscore.put(Contacts.SHAPE_CALL_third,shape_type_third);


        cvscore.put(Contacts.SCORE_forth, score_forth);
        cvscore.put(Contacts.NUM_CALL_forth, num_call_forth);
        cvscore.put(Contacts.NUM_COLLECTION_forth, num_collection_forth);
        cvscore.put(Contacts.SHAPE_CALL_forth,shape_type_forth);

        //cvscore.put(Contacts.IMAGE,blob);
        db = sqlhpc.getWritableDatabase();
        db.insert(Contacts.TABLESCORE, null, cvscore);
        db.close();
    }

    public void updateContact_score(long id,String score,  String  num_call, String  num_collection ,String shape_type,
            String score_sec,  String  num_call_sec, String  num_collection_sec ,String shape_type_sec,
            String score_third,  String  num_call_third, String  num_collection_third ,String shape_type_third,
              String score_forth,  String  num_call_forth, String  num_collection_forth ,String shape_type_forth) 
    {
        ContentValues cvscore = new ContentValues();
        cvscore.put(Contacts.SCORE_st, score);
        cvscore.put(Contacts.NUM_CALL_st, num_call);
        cvscore.put(Contacts.NUM_COLLECTION_st, num_collection);
        cvscore.put(Contacts.SHAPE_CALL_st, shape_type);

        cvscore.put(Contacts.SCORE_sec, score_sec);
        cvscore.put(Contacts.NUM_CALL_sec, num_call_sec);
        cvscore.put(Contacts.NUM_COLLECTION_sec, num_collection_sec);
        cvscore.put(Contacts.SHAPE_CALL_sec, shape_type_sec);

        cvscore.put(Contacts.SCORE_third, score_third);
        cvscore.put(Contacts.NUM_CALL_third, num_call_third);
        cvscore.put(Contacts.NUM_COLLECTION_third, num_collection_third);
        cvscore.put(Contacts.SHAPE_CALL_third,shape_type_third);


        cvscore.put(Contacts.SCORE_forth, score_forth);
        cvscore.put(Contacts.NUM_CALL_forth, num_call_forth);
        cvscore.put(Contacts.NUM_COLLECTION_forth, num_collection_forth);
        cvscore.put(Contacts.SHAPE_CALL_forth,shape_type_forth);

        //cvscore.put(Contacts.IMAGE,blob);
        db = sqlhpc.getWritableDatabase();
        db.update(Contacts.TABLESCORE, cvscore, Contacts.ID+"="+ id, null);
        db.close();
    }

    public void updateContact(long id,String name, String phone, String mail,String fb,byte[] blob) {
        ContentValues cv = new ContentValues();
        cv.put(Contacts.NAME, name);
        cv.put(Contacts.PHONE, phone);
        cv.put(Contacts.MAIL, mail);
        cv.put(Contacts.FB, fb);
        cv.put(Contacts.IMAGE,blob);

        db = sqlHp.getWritableDatabase();
        db.update(Contacts.TABLE, cv, Contacts.ID+"="+ id, null);
        db.close();
    }

    public Cursor getAllContacts() {
        db = sqlHp.getReadableDatabase();
        cur=db.query(Contacts.TABLE,null, null,null, null, null, "name");
        return cur;
    }

    public Cursor getAllScores() {
        db = sqlhpc.getReadableDatabase();
        curc=db.query(Contacts.TABLESCORE,null, null,null, null, null, "score_st");
        return curc;
    }
    public void deletescore(long id) {
        System.out.println("DELETE ");
        db = sqlhpc.getWritableDatabase();
        db.delete(Contacts.TABLESCORE, "_id="+id , null);
        db.close();
    }

    public Cursor getOneContact(long id) {
        db = sqlHp.getReadableDatabase();
        cur=db.query(Contacts.TABLE, null, "_id="+ id, null, null, null,null);
        return cur;
    }

    public void deleteContact(long id) {
        System.out.println("DELETE ");
        db = sqlHp.getWritableDatabase();
        db.delete(Contacts.TABLE, "_id="+id , null);
        db.close();
    }
}
Tota
  • 13
  • 7

2 Answers2

0

I believe what you need to do is use the Tag facility of the row view container to identify the textview it contains and then retrieve it in the listener to identify the correct textview. The way I've done it is to use a holder for the row contained views, so you can access any that you need. For example:

protected static class RowViewHolder {
    public TextView tvOne;
    public TextView tvTwo;
}

Then in your newView method, populate the holder and set the Tag to it:

public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View retView = inflater.inflate(R.layout.single_row_item, parent, false);

    RowViewHolder holder = new RowViewHolder();
    holder.tvOne = (TextView) retView.findViewById(R.id.name);
    holder.tvTwo = (TextView) retView.findViewById(R.id.txt_number);
    retView.setTag(holder);

    return retView;
}

In your listener then you can access the correct textview:

    public void onClick(View v) {
            ListView lv = (ListView) v.getParent();
            final int position = lv.getPositionForView((View) v.getParent());

            // get the RowViewHolder
            RowViewHolder holder = new RowViewHolder();
            holder = (RowViewHolder) ((View) v.getParent()).getTag();
            if(holder.tvOne.getVisibility() == View.INVISIBLE ) {
                holder.tvOne.setVisibility(View.VISIBLE);
            }
            else
            { 
                holder.tvOne.setVisibility(View.INVISIBLE);
            }
    }

Apologies, but I've not been able to test this code but I hope it gives you a pointer to the process. You shouldn't need the RowViewHolder but I've included it so you or others can see how to access multiple views within the row.

You may find the this video helpful The World of ListView I think you'll find the relevant discussion at about 4:10 in which describes how the views are res-used and how index, position etc. relate.

I also found one of the answers to this question helpful Android: ListView elements with multiple clickable buttons

Community
  • 1
  • 1
jrisch
  • 132
  • 1
  • 9
  • thanks for your help but it doesn't work it give me that tvOne is undefined for the type for the other class row view holder – Tota Sep 01 '13 at 07:44
  • in this part holder = (RowViewHolder) ((View) v.getParent()).getTag(); holder.tvOne.); – Tota Sep 01 '13 at 09:11
  • it gives me Syntax error, insert “AssignmentOperator Expression” – Tota Sep 01 '13 at 14:04
  • Apologies, there was an incorrect line that had crept in "holder.tvOne.);" that I've taken out of the above sample. If that hasn't fixed your problem then can you post your code so I can see where you have the problem. – jrisch Sep 02 '13 at 09:39
  • Are you embedding the CursorAdapter within the activity, or is it in a separate code module? – jrisch Sep 02 '13 at 09:41
  • i have two classes CustConAdpterSelect extends SimpleCursorAdapter and the other one is SelectPlayers extends ListActivity – Tota Sep 02 '13 at 10:49
  • i have edited my code hope to tell me whats wrong in it,Thanks in advance – Tota Sep 02 '13 at 10:56
  • There are multiple ways of doing this. The way I was suggesting assumes you have a single code module for the activity containing the CustConAdpterSelect class. So the structure looks something like this: – jrisch Sep 03 '13 at 15:01
  • so please how i can do it in this case – Tota Sep 03 '13 at 15:07
  • I placed the code in a separate answer above. I couldn't see how to add code to a comment. – jrisch Sep 03 '13 at 16:25
0

There are multiple ways of doing this. The way I was suggesting assumes you have a single code module for the activity containing the CustConAdpterSelect class. So the structure looks something like this.

package com.example.totastest;

// imports

public class Copy_2_of_MainActivity extends Activity {

protected static class RowViewHolder
{
    public TextView tvOne;
    public TextView tvTwo;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public class CustConAdpterSelect extends SimpleCursorAdapter
{
    // ...

    public CustConAdpterSelect(Context context, int layout, Cursor c, String[] from, int[] to)
    {
        super(context, layout, c, from, to, 0);
        // ...
    }

    public View newView(Context context, Cursor cursor, ViewGroup parent)
    {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View retView = inflater.inflate(R.layout.lv_name_photo, parent, false);

        RowViewHolder holder = new RowViewHolder();
        holder.tvOne = (TextView) retView.findViewById(R.id.name);
        holder.tvTwo = (TextView) retView.findViewById(R.id.txt_number);
        holder.tvOne.setOnClickListener(tvOneLapOnClickListener);
        retView.setTag(holder);

        return retView;
    }

    @Override
    public void bindView(View v, final Context context, Cursor c)
    {
        // ...
    }

    @SuppressWarnings("unchecked")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        // ...
    }

    private OnClickListener tvOneLapOnClickListener = new OnClickListener() {
        @Override
        // When the tvOne button is clicked, execute this code
        public void onClick(View v) {
                // get the RowViewHolder
                RowViewHolder holder = new RowViewHolder();
                // Get the holder for the row
                holder = (RowViewHolder) ((View) v.getParent()).getTag();
                if (holder.tvOne.getVisibility() == View.INVISIBLE)
                    holder.tvOne.setVisibility(View.VISIBLE);
                else
                    holder.tvOne.setVisibility(View.INVISIBLE);
        }
    };

}

protected void onListItemClick(ListView l, View v, int position, long id)
{
    // ...
}

}

jrisch
  • 132
  • 1
  • 9
  • the onclick parameter in this case must have (DialogInterface dialog, int which) not (View) – Tota Sep 04 '13 at 09:13
  • [tag:jrisch] please help – Tota Sep 05 '13 at 12:22
  • I don't understand why you say onClick must have (DialogInterface dialog, int which). The structure I outlined above, apart from the imports, is a complete copy from my system and doesn't show any errors on my system. Perhaps if you could show me your full java file I could see where the problem is. You could also try copying the above into a new activity module on your system and complete the imports and see if it shows errors. A misplaced "}" or ";" can change the whole meaning. – jrisch Sep 05 '13 at 18:55
  • Alternatively you could possibly upload your code to a dropbox or some where I could access it. – jrisch Sep 05 '13 at 18:56
  • [tag:jrishch] i edited the code and post the new one could you please see it – Tota Sep 07 '13 at 08:03
  • Are you still having the problem that " onClick must have (DialogInterface dialog, int which)" because mine doesn't? – jrisch Sep 08 '13 at 07:13
  • What is SQLiteConnector? Is it your own class? What is the problem you are seeing? – jrisch Sep 08 '13 at 07:15
  • [tag:jrishch] that is the data base class i edited the code above – Tota Sep 08 '13 at 10:10
  • I don't recognise the class SQLiteHelper. Is that one of your own or do you mean SQLiteOpenHelper? Also previous question, are you still having the problem that " onClick must have (DialogInterface dialog, int which)"? Is Contacts one of your own too. Android Cobtacts is deprecated. I can't resolve the field names. – jrisch Sep 08 '13 at 16:58