Onitem长按不适用于listvview

时间:2016-01-01 06:41:34

标签: android listview onitemlongclicklistener

我有一个活动,它将onitemlongclicllstener实现到列表视图中。我使用parse.com作为后端来检索listvview中的数据。一切正常,但onitemlongclicllstener不在列表视图上工作。长按列表项时没有任何反应

我的主要活动

public class InterActivity extends Activity implements OnItemLongClickListener
{


ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
FinalAdapter adapter;
 List<CodeList> codelist = null;
 SharedPreference shrdPreference;



@Override
public void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.inter_layout);
    shrdPreference = new SharedPreference();
    //Execute RemoteDataTask AsyncTask
    new RemoteDataTask().execute();
}

private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(InterActivity.this);
        // Set progressdialog title
        mProgressDialog.setTitle("Loading");
        // Set progressdialog message
        mProgressDialog.setMessage("Please wait loading ...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setCancelable(false);
        // Show progressdialog
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Create the array
        codelist = new ArrayList<CodeList>();
        try {
            // Locate the class table named "Country" in Parse.com
            ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                "InterActivity");
            // Locate the column named "ranknum" in Parse.com and order list
            // by ascending
            query.orderByAscending("_created_at");
            ob = query.find();
            for (ParseObject inter : ob) {


                ParseFile video = (ParseFile) inter.get("demovideo");

        //      ParseFile downloadfile = (ParseFile) inter.get("download");

                CodeList map = new CodeList();
                map.setListHeading((String) inter.get("listheading"));
                map.setSingleItemHeading((String) inter.get("heading"));

                map.setDownloadCode((String) inter.get("download"));


                map.setDailogdemovideo(video.getUrl());
    //          map.setDownloadCode(downloadfile.getUrl());

                codelist.add(map);
            }
        } catch (ParseException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // Locate the listview in listview_main.xml
        listview = (ListView) findViewById(R.id.inter_layoutListView);
        // Pass the results into ListViewAdapter.java
        adapter = new FinalAdapter(InterActivity.this,
                                      codelist);
        // Binds the Adapter to the ListView
        listview.setAdapter(adapter);

        listview.setOnItemLongClickListener(InterActivity.this);
        // Close the progressdialog
        mProgressDialog.dismiss();
    }
}

@Override
public boolean onItemLongClick(AdapterView<?> arg0, View view, int position, long arg3)
{
    ImageView fvrtebutton = (ImageView) view.findViewById(R.id.favbtn);


    String tag = fvrtebutton.getTag().toString();
    if (tag.equalsIgnoreCase("no")) {
        shrdPreference.addFavorite(InterActivity.this, codelist.get(position));
        Toast.makeText(InterActivity.this, getString(R.string.fav_added),
                       Toast.LENGTH_SHORT).show();

        fvrtebutton.setTag("yes");
        fvrtebutton.setImageResource(R.drawable.favorite);
    } else {
        shrdPreference.removeFavorite(InterActivity.this, codelist.get(position));
        fvrtebutton.setTag("no");
        fvrtebutton.setImageResource(R.drawable.unfavorite);
        Toast.makeText(InterActivity.this,
                       getString(R.string.fav_removed),
                       Toast.LENGTH_SHORT).show();
    }
    return false;
}

@Override
protected void onResume()
{

    super.onResume();
}



@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);

}

final adapter.java

public class FinalAdapter extends BaseAdapter
 {
Context context;
LayoutInflater inflater;
ImageLoader imgLoader;
private List<CodeList> codeList = null;
private ArrayList<CodeList> arraylist;
SharedPreference shrdprfrnce;

public FinalAdapter(Context context,
                    List<CodeList> codeList) {
    this.context = context;
    this.codeList = codeList;
    inflater = LayoutInflater.from(context);
    this.arraylist = new ArrayList<CodeList>();
    this.arraylist.addAll(codeList);
    shrdprfrnce = new SharedPreference();
    imageLoader = new ImageLoader(context);

}

public class ViewHolder{
    TextView listHeading;
    TextView listHash;
    ImageView alphabetList;
    ImageView favariteImage;
}


@Override
public int getCount()
{
    return codeList.size();
}

@Override
public Object getItem(int position)
{
    return codeList.get(position);
}

@Override
public long getItemId(int position)
{
    return position;
}

@Override
public View getView(final int position, View view, ViewGroup parent)
{
    final ViewHolder holder;
    if(view == null){
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.beg_list_item,null);
        holder.listHeading = (TextView) view.findViewById(R.id.beg_list_itemTextView);

        //holder.listHash = (TextView) view.findViewById(R.id.listview_hashtags);
        holder.alphabetList = (ImageView) view.findViewById(R.id.beg_list_itemImageView);

        holder.favariteImage = (ImageView) view.findViewById(R.id.favbtn);

        view.setTag(holder);

    }else{
        holder = (ViewHolder) view.getTag();
    }
    CodeList codes = (CodeList) getItem(position);
    holder.listHeading.setText(codeList.get(position).getListHeading());
    imageLoader.DisplayImage(codeList.get(position).getAlphabetimg(),
                             holder.alphabetList);


    if (checkFavoriteItem(codes)) {
        holder.favariteImage.setImageResource(R.drawable.favorite);
        holder.favariteImage.setTag("yes");
    } else {
        holder.favariteImage.setImageResource(R.drawable.unfavorite);
        holder.favariteImage.setTag("no");
    }                    

    view.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0){
                Intent intent = new Intent(context, SingleItemView.class);

                //intent.putExtra("listheading",
                //       (codeList.get(position).getListHeading()));
                //intent.putExtra("alphabetimg",
                //              (codeList.get(position).getAlphabetimg()));

                intent.putExtra("demovideo",
                                (codeList.get(position).getDailogdemovideo()));

                intent.putExtra("download",
                                (codeList.get(position).getDownloadCode()));



                // Start SingleItemView Class
                context.startActivity(intent);

            }
        });             

    return view;
}

public boolean checkFavoriteItem(CodeList checkCodes) {
    boolean check = false;
    List<CodeList> favorites = shrdprfrnce.getFavorites(context);
    if (favorites != null) {
        for (CodeList codes : favorites) {
            if (codes.equals(checkCodes)) {
                check = true;
                break;
            }
        }
    }
    return check;
}


public void add(CodeList codes) {
    (
    codeList.add(codes);
    notifyDataSetChanged();
}


public void remove(CodeList codes) {

    codeList.remove(codes);
    notifyDataSetChanged();
}   

}

2 个答案:

答案 0 :(得分:0)

尝试使用return true;

@Override
public boolean onItemLongClick(AdapterView < ? > arg0, View arg1,
 int pos, long id) {

 Log.v("long clicked", "pos: " + pos);

 return true;
}

答案 1 :(得分:0)

plz remove 
view.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0){
            Intent intent = new Intent(context, SingleItemView.class);

            //intent.putExtra("listheading",
            //       (codeList.get(position).getListHeading()));
            //intent.putExtra("alphabetimg",
            //              (codeList.get(position).getAlphabetimg()));

            intent.putExtra("demovideo",
                            (codeList.get(position).getDailogdemovideo()));

            intent.putExtra("download",
                            (codeList.get(position).getDownloadCode()));



            // Start SingleItemView Class
            context.startActivity(intent);

        }
    });  

bcoz它获取完整视图点击这是你的OnLongClick还没有工作