0

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

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

    } 
4

1 回答 1

0

I'm guessing it doesn't work?

You should try and set the Listener of your ListView and not your views in the ListView. Also use a OnItemLongClickListener instead of your OnLongClickListener.

So in your Fragment or Activity:

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 

    }
});
于 2013-09-09T18:52:10.620 回答