0

我正在使用基本适配器,我有一个列表,其中包含带有文本视图的列表(表示左侧发送的项目,下面是右侧接收的项目,我想单击列表中的每个位置以执行回复消息。

请建议我如何执行单击列表项的功能

下面是我正在使用的代码

类 MessageAdapter 扩展 BaseAdapter {

     ViewHolder viewHolder;
     LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);

     public int getCount() {
            // TODO Auto-generated method stub
            return Constant_Variables.Sms_Status_list.size();
        }

     public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

     @Override
     public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }



     @Override
     public View getView(final int position, View convertView, ViewGroup parent) {

         if (convertView == null) {
             convertView = inflater.inflate(R.layout.messagelist_row, null);

             viewHolder=new ViewHolder();

             viewHolder.sms_from_row=(TextView)convertView.findViewById(R.id.sms_from_txtview);
             viewHolder.sms_to_row=(TextView)convertView.findViewById(R.id.sms_sendto_txtview);

         }
         else
         {
             viewHolder = (ViewHolder) convertView.getTag();
         }


         viewHolder.sms_from_row.setVisibility(View.VISIBLE);
         viewHolder.sms_to_row.setVisibility(View.VISIBLE);


         if(Constant_Variables.Sms_Status_list.get(position).equalsIgnoreCase(Twilioresponse_Utils.sms_sent))
         {
             viewHolder.sms_from_row.setVisibility(View.GONE);
             viewHolder.sms_to_row.setText("Sent: "+Constant_Variables.Sms_from_list.get(position));

         }
         else if(Constant_Variables.Sms_Status_list.get(position).equalsIgnoreCase(Twilioresponse_Utils.sms_received))
         {
             viewHolder.sms_to_row.setVisibility(View.GONE);
             viewHolder.sms_from_row.setText("Received: "+Constant_Variables.Sms_to_list.get(position));
         }
         else
         {
             viewHolder.sms_from_row.setText("Message in queued.");
             viewHolder.sms_to_row.setVisibility(View.VISIBLE);
         }


         convertView.setTag(viewHolder);


         return convertView;
     }

     protected void onListItemClick(ListView l, View v, final int position, long id) {

         if(l.callOnClick()== true)
         {
             Log.i("the Item clicked is at position : ", ""+ position);
         }

         else
         {
             Log.i("the Item not clicked is at position : ","the Item not clicked is at position" );
         }
     }
 }

下面是我正在使用的 XML 代码

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

    <TextView
        android:id="@+id/sms_from_txtview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:layout_alignParentRight="true"
        android:background="@drawable/blue"
        android:layout_alignParentTop="true"
        android:text="From" />

    <TextView
        android:id="@+id/sms_sendto_txtview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:background="@drawable/red"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="To" />
</RelativeLayout >

问候阿姆林德·辛格

4

5 回答 5

0

android中有onItemClickListener,你可以使用它

ListView listview = (ListView)findViewById(R.id.yourlist);
listview.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // @todo
    }
});

对于列表项中的单个视图,您始终可以在 getView 方法中添加单击侦听器。

于 2013-01-25T05:35:50.230 回答
0

初始化该视图时为两者实现单击侦听器

viewHolder.sms_from_row.setOnClickListener(new OnClickListener(){
    public void onClick(View view){
        // Todo thing
    }
});
viewHolder.sms_to_row.setOnClickListener(new OnClickListener(){
    public void onClick(View view){
        // Todo thing
    }
});
于 2013-01-25T05:38:09.073 回答
0

您的代码是正确的,只需在此下方添加点击侦听器即可。

 viewHolder.sms_from_row=(TextView)convertView.findViewById(R.id.sms_from_txtview);
 viewHolder.sms_to_row=(TextView)convertView.findViewById(R.id.sms_sendto_txtview);

在此下方添加像这样的单击侦听器。

viewHolder.sms_from_row.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                    }
                });

    viewHolder.sms_to_row.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub

                        }
                    });

希望对您有所帮助。

谢谢。

于 2013-01-25T05:38:38.010 回答
0

写在项目上点击textview

   @Override
 public View getView(final int position, View convertView, ViewGroup parent) {

     if (convertView == null) {
         convertView = inflater.inflate(R.layout.messagelist_row, null);

         viewHolder=new ViewHolder();

         viewHolder.sms_from_row=(TextView)convertView.findViewById(R.id.sms_from_txtview);
         viewHolder.sms_to_row=(TextView)convertView.findViewById(R.id.sms_sendto_txtview);

viewHolder.sms_from_row.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {

                        }
                    });
     }
     else
     {
         viewHolder = (ViewHolder) convertView.getTag();
     }


     viewHolder.sms_from_row.setVisibility(View.VISIBLE);
     viewHolder.sms_to_row.setVisibility(View.VISIBLE);


     if(Constant_Variables.Sms_Status_list.get(position).equalsIgnoreCase(Twilioresponse_Utils.sms_sent))
     {
         viewHolder.sms_from_row.setVisibility(View.GONE);
         viewHolder.sms_to_row.setText("Sent: "+Constant_Variables.Sms_from_list.get(position));

     }
     else if(Constant_Variables.Sms_Status_list.get(position).equalsIgnoreCase(Twilioresponse_Utils.sms_received))
     {
         viewHolder.sms_to_row.setVisibility(View.GONE);
         viewHolder.sms_from_row.setText("Received: "+Constant_Variables.Sms_to_list.get(position));
     }
     else
     {
         viewHolder.sms_from_row.setText("Message in queued.");
         viewHolder.sms_to_row.setVisibility(View.VISIBLE);
     }


     convertView.setTag(viewHolder);


     return convertView;
 }
于 2013-01-25T05:40:48.370 回答
0

单击列表时单击
单击列表时

删除图标
单击删除图标时

这是您可以参考的代码:

    public class CaseAdapter extends BaseAdapter{

        Context context;
        ArrayList<PojoClass> list;
        LayoutInflater inflater;

        public CaseAdapter(Context context,ArrayList<PojoClass> list)
        {
            this.context=context;
            this.list=list;
            inflater=(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View row = convertView;
             ViewHolder holder = null;

            if(row == null)
            {

                row = inflater.inflate(R.layout.activity_home_screen, parent,false);            
                holder = new ViewHolder(row);
                row.setTag(holder);

            }
            else
            {
                holder = (ViewHolder) row.getTag();
            }
            holder.caseNumber.setText(list.get(position).getCaseNo());
            holder.state.setText(list.get(position).getState());
            holder.date.setText(list.get(position).getDate());

            row.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                     Toast.makeText(context, "record clicked!", Toast.LENGTH_LONG).show();

                }
            });

            holder.delete.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    v=(View) v.getParent();//utilizing the View object...or we can use new view object
                    //View view=(View)v.getParent();//this one also we can use...but instead of v object you need to use view object;

                    TextView number=(TextView)v.findViewById(R.id.textView_case_number);
                    InfoDb db=new InfoDb(context);//using for remove record from database
                    db.open();

                    if(db.removeRecord(number.getText().toString()))
                    {
list.remove(position);
                  Toast.makeText(context, "Record Deleted!", Toast.LENGTH_LONG).show();
                  CaseAdapter.this.notifyDataSetChanged();

                    }else
                         Toast.makeText(context, "Record not Deleted!", Toast.LENGTH_LONG).show();

                    db.close();
                }
            });

            return row;
        }


        public class ViewHolder{

            TextView state,caseNumber,date;
            ImageView delete;

            public ViewHolder(View v){
                state=(TextView)v.findViewById(R.id.textView_State);
                caseNumber=(TextView)v.findViewById(R.id.textView_case_number);
                date=(TextView)v.findViewById(R.id.textView_date);
                delete=(ImageView)v.findViewById(R.id.ImageView_Delete);


            }


        }

    }
于 2015-05-05T13:29:24.843 回答