Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ListView中的setOnLongClickListener应用于2行,而不是1行_Android_Listview_Button - Fatal编程技术网

Android ListView中的setOnLongClickListener应用于2行,而不是1行

Android ListView中的setOnLongClickListener应用于2行,而不是1行,android,listview,button,Android,Listview,Button,我试图在我的listView行上注册一个长点击,这样它就会显示一个不可见的按钮。 问题是,当我长按该行时,按钮正确显示,但只在两个不同的行中显示。我 你能看看我的代码,告诉我为什么它把我的按钮设置成两行而不是我点击的那一行 下面是我的适配器类: package android.GUI; import android.content.Context; import android.database.Cursor; import android.graphics.Typeface; import

我试图在我的listView行上注册一个长点击,这样它就会显示一个不可见的按钮。 问题是,当我长按该行时,按钮正确显示,但只在两个不同的行中显示。我

你能看看我的代码,告诉我为什么它把我的按钮设置成两行而不是我点击的那一行

下面是我的适配器类:

package android.GUI;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

// This is a custom adapter that will use cursor to get data from SQLite DB as the source.

public class FastScrollAdapter extends CursorAdapter {

    private LayoutInflater mInflater;
    private Cursor cursor;

    public FastScrollAdapter(Context context, Cursor c) {
        super(context, c);
        this.mInflater = LayoutInflater.from(context);
        this.cursor = c;

    }

    public FastScrollAdapter(Context context, Cursor c, boolean autoRequery) {
        super(context, c, autoRequery);
        this.mInflater = LayoutInflater.from(context);
        this.cursor = c;
    }

    // Here we shall look to see if the View already exists and create a new one
    // if not.

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

        // If view doesn't exists = create a new one and also store it (all of
        // the views) in our ViewHolder class called "holder"
        if (convertView == null) {

            convertView = this.mInflater.inflate(R.layout.list_entry, null);

            holder = new ViewHolder();

            holder.LL = (LinearLayout) convertView.findViewById(R.id.layoutBG);
            holder.delete = (Button) convertView.findViewById(R.id.deleteItem);
            holder.LL.setOnLongClickListener(new OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {
                    // TODO Auto-generated method stub
                    holder.delete.setVisibility(View.VISIBLE);
                    return false;
                }
            });

            holder.rowID = (TextView) convertView.findViewById(R.id.rawId);
            holder.info = (TextView) convertView.findViewById(R.id.Info);
            holder.info.setTypeface(Entry.roboto);
            holder.info.setText("Info");
            holder.dateDisp = (TextView) convertView
                    .findViewById(R.id.dateDisp);
            holder.day = (TextView) convertView.findViewById(R.id.day);
            holder.finish = (TextView) convertView.findViewById(R.id.finish);
            holder.hourMin = (TextView) convertView.findViewById(R.id.hourMin);
            holder.shiftDisp = (TextView) convertView
                    .findViewById(R.id.shiftDisp);
            holder.start = (TextView) convertView.findViewById(R.id.start);
            holder.timestarted = (TextView) convertView
                    .findViewById(R.id.timestarted);

            convertView.setTag(holder);

            // If exists, just fetch this view and display it.
        } else {
            holder = (ViewHolder) convertView.getTag();

        }
        this.cursor.moveToPosition(position);

        // Set some values...
        /*************** Taken From DB ******************/
        holder.start.setText(this.cursor.getString(this.cursor
                .getColumnIndex(DBAdapter.KEY_START)));
        holder.start.setTypeface(Shifts.roboto);

        holder.shiftDisp.setText(this.cursor.getString(this.cursor
                .getColumnIndex(DBAdapter.KEY_HOURS)));
        holder.shiftDisp.setTypeface(Shifts.roboto);

        holder.rowID.setText(this.cursor.getString(this.cursor
                .getColumnIndex(DBAdapter.KEY_ROWID)));
        holder.rowID.setTypeface(Shifts.roboto);

        holder.dateDisp.setText(this.cursor.getString(this.cursor
                .getColumnIndex(DBAdapter.KEY_DATE)));
        holder.dateDisp.setTypeface(Shifts.roboto);

        holder.day.setText(this.cursor.getString(this.cursor
                .getColumnIndex(DBAdapter.KEY_DAY)));
        holder.day.setTypeface(Shifts.roboto);

        holder.finish.setText(this.cursor.getString(this.cursor
                .getColumnIndex(DBAdapter.KEY_END)));
        holder.finish.setTypeface(Shifts.roboto);

        /*************** Regular ones ******************/

        holder.info.setTypeface(Shifts.roboto);
        holder.info.setText("Info:");

        holder.hourMin.setTypeface(Shifts.roboto);
        holder.hourMin.setText("Shift:");

        holder.timestarted.setTypeface(Shifts.roboto);
        holder.timestarted.setText("Duration:");

        return convertView;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        return null;
    }

    static class ViewHolder {

        TextView rowID;
        TextView info;
        TextView dateDisp;
        TextView day;
        TextView timestarted;
        TextView start;
        TextView finish;
        TextView hourMin;
        TextView shiftDisp;
        Button delete;
        LinearLayout LL;

    }

}

这个问题是由于视图回收造成的。要维护按钮视图的状态,您需要维护其状态的单独数组。我是这样做的:

在适配器中添加阵列:

public static int[] buttonState;
在构造函数中添加一个方法:

populateButtonState();
方法(0表示不可见,1表示可见):

然后在getView中,根据数组设置可见性:

holder.delete = (Button) convertView.findViewById(R.id.deleteItem);           
if(buttonState[position] = 1) holder.delete.setVisibility(View.VISIBLE);                
您还需要添加到
onLongClick
(假设您希望
onLongClick
切换状态):


当你点击它时是两个还是当你点击它并滚动列表时是两个?当我点击它并向下滚动列表时是两个!您希望一次看到多个,还是只看到一个?(答案会根据您想要的内容而变化)如果用户愿意,他应该能够设置其中一些,甚至全部。是的-我希望一次可以看到多个。使用变量名出错(had
chkState
而不是
populateButtonState
方法中的
buttonState
)。我纠正了它。让我知道这是否有效。谢谢你@Barak,我会试试这个(我现在没有时间——现实生活中的工作),如果有效,我会接受答案。谢谢
holder.delete = (Button) convertView.findViewById(R.id.deleteItem);           
if(buttonState[position] = 1) holder.delete.setVisibility(View.VISIBLE);                
if(buttonState[position] = 1) {
    holder.delete.setVisibility(View.INVISIBLE);
    buttonState[position] = 0;
} else {
    holder.delete.setVisibility(View.VISIBLE);
    buttonState[position] = 1;
}