0

根据其在行中的位置设置文本视图单击onlistitemclicked从数据库中的SimpleCursorAdapter填充的列表视图我的自定义列表视图有一个名称的图像和文本视图和另一个不可见的文本视图,该视图将可见并设置为仅单击的行上的数字不同,但问题是当我单击任何行时,文本仅出现在第一行的视图中,无论我单击的行是什么,我尝试使用 set 和 get 方法,但我发现它用于基础适配器。并且可见的文本视图不是来自数据库,有人可以告诉我该怎么做

这是代码的一部分

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);
            }


        };





    }       

}

那是数据库类“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();
    }
}
4

2 回答 2

0

我相信您需要做的是使用行视图容器的标记工具来识别它包含的文本视图,然后在侦听器中检索它以识别正确的文本视图。我这样做的方法是为包含行的视图使用一个持有者,这样您就可以访问您需要的任何内容。例如:

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

然后在你的 newView 方法中,填充持有者并将标签设置为它:

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;
}

在您的侦听器中,您可以访问正确的文本视图:

    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);
            }
    }

抱歉,但我无法测试这段代码,但我希望它能给你一个指向该过程的指针。您不需要 RowViewHolder,但我已将其包含在内,以便您或其他人可以看到如何访问行内的多个视图。

您可能会发现此视频很有帮助ListView 的世界我想您会在大约 4:10 找到相关讨论,其中描述了视图如何被重新使用以及索引、位置等如何关联。

我还发现这个问题的答案之一对Android 很有帮助:ListView elements with multiple clickable button

于 2013-08-29T18:28:39.207 回答
0

有多种方法可以做到这一点。我建议的方式假设您有一个用于包含 CustConAdpterSelect 类的活动的代码模块。所以结构看起来像这样。

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)
{
    // ...
}

}

于 2013-09-03T15:06:30.667 回答