长按元素修改listview内容

时间:2013-09-09 18:39:56

标签: android listview

我有一个包含textview元素的列表视图。我想在长按行时修改列表视图的文本。由于我的数据不断变化,我不想将文本视图引用的修饰符设置为final。我已经实现了一个视图持有者,用于在滚动时保持数据。我的代码是:

static class viewHolder
{
    TextView t1,t2,t3;
}

public class myAdapter extends ArrayAdapter<searchData>{

        public myAdapter(Context context, int resource, ArrayList<searchData> data) {
            super(context, resource,data);
            // TODO Auto-generated constructor stub

        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            final viewHolder holder;

            if(convertView == null){
                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.location_list3, parent, false);     
                holder = new viewHolder();
                holder.t1 = (TextView) convertView.findViewById(R.id.textView1);
                holder.t2 = (TextView) convertView.findViewById(R.id.textView2);
                holder.t3 = (TextView) convertView.findViewById(R.id.textView_t1);
                convertView.setTag(holder);
            }
            else
            {
                 holder = (viewHolder) convertView.getTag();
            }
            Typeface typeFace=Typeface.createFromAsset(getAssets(),"fonts/agencyfb-regular.ttf");

        //  TextView but = (TextView) row.findViewById(R.id.imageButton1);
            holder.t1.setTypeface(typeFace,typeFace.BOLD);
            holder.t2.setTypeface(typeFace);
            holder.t3.setTypeface(typeFace,typeFace.BOLD);              
            holder.t1.setText(dataList.get(position).getName());
            holder.t2.setText(dataList.get(position).getStatus());
            holder.t3.setText(dataList.get(position).getWaitingTime());

            //For adding layout change upon Long Press

            convertView.setOnLongClickListener(new OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(),             "Long Press at position: "+position, Toast.LENGTH_SHORT).show();
                    //ChangeText
                    return false;
                }
            });




            return convertView;
        }

    } 

1 个答案:

答案 0 :(得分:0)

我猜它不起作用?

您应该尝试在ListView中设置ListView的监听器而不是您的视图。还可以使用 OnItemLongClickListener 代替OnLongClickListener。

所以在你的片段或活动中:

listview.OnItemLongClickListener.setOnItemLongClickListener(
    new OnItemLongClickListener() {

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, 
        View view, int position, long id) {

        Object o = listview.getAdapter().getItem(position);
        //do stuff 

    }
});