onItemClickListener无法正常工作

onItemClickListener无法正常工作

本文介绍了ListView onItemClickListener无法正常工作. Onclick不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

适配器

package ooad.issuemanagementsystem;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import java.util.ArrayList;

public class ManagerAdapter extends ArrayAdapter<Issue> {
Context c;
int layoutFile;
ArrayList<Issue> data;
public ManagerAdapter(Context context, int resource, ArrayList<Issue> objects) {

    super(context, resource, objects);
    c = context;
    layoutFile = resource;
    data = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row;
    if(convertView == null)
    {
        LayoutInflater inflater =  ((Activity) c).getLayoutInflater();
        row = inflater.inflate(R.layout.manager_row, parent, false);
    }
    else {
        row = (View) convertView;
    }
    TextView t1 = (TextView) row.findViewById(R.id.textView32);
    TextView t2 = (TextView) row.findViewById(R.id.textView31);
    t1.setText(data.get(position).getTitle());
    t2.setText(data.get(position).getDescription());
    CheckBox c = (CheckBox) row.findViewById(R.id.checkBox2);
    c.setClickable(false);
    if(data.get(position).getOpen().equals("true"))
        c.setChecked(false);
    else
        c.setChecked(true);


    return row;
}
}

活动

package ooad.issuemanagementsystem;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.parse.FindCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import java.util.ArrayList;
import java.util.List;
public class ManagerActivity extends AppCompatActivity {
Context con;
String DeleteObject;
String dept;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_manager);
    con = this;
    Intent i = getIntent();
    dept = i.getStringExtra("dept");
    ListView l = (ListView) findViewById(R.id.listView3);
    ArrayList<Issue> reports = new ArrayList<Issue>();
    reports.add(new Issue("","","","",""));
    ManagerAdapter adapter = new ManagerAdapter(this,R.id.listView3,reports);
    l.setAdapter(adapter);
    l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            /*Intent i = new Intent(con, IssueDetails.class);
            i.putExtra("Title", reports.get(position).getTitle());
            i.putExtra("Description", reports.get(position).getDescription());
            i.putExtra("Category", reports.get(position).getCategory());
            i.putExtra("Id", reports.get(position).getID());
            i.putExtra("isOpen", reports.get(position).getOpen());
            startActivity(i);*/
            Toast.makeText(con, "hi", Toast.LENGTH_LONG).show();
        }
    });
    //String Application_id = "AMavRlzklxFLKZAtEV7OGNyHYI3ByyRnhzhoUwLD";
    //String Client_id = "hMSZHpWUsCtKXgtAlmjugpcm2ycqKwYo6lCgWgX1";
    //Parse.initialize(c, Application_id, Client_id);
    /*ParseQuery<ParseObject> query2 = ParseQuery.getQuery("ISSUES");
    query2.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> postList, ParseException e) {
            if (e == null) {
                for (ParseObject post : postList) {
                    String a = post.getObjectId();
                    String b = post.getString("Title");
                    String c = post.getString("Description");
                    String d = post.getString("isOpen");
                    String ee = post.getString("Category");
                    reports.add(new Issue(a, b, d, c, ee));
                    adapter.notifyDataSetChanged();
                }
            } else {
                Log.d(getClass().getSimpleName(), "Error: " + e.getMessage());
            }
        }
    });*/

    //adapter.notifyDataSetChanged();
    /*l.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            DeleteObject = reports.get(position).getID();
            reports.remove(position);
            adapter.notifyDataSetChanged();
            ParseQuery<ParseObject> query3 = ParseQuery.getQuery("ISSUES");
            query3.findInBackground(new FindCallback<ParseObject>() {
                @Override
                public void done(List<ParseObject> postList, ParseException e) {
                    if (e == null) {
                        for (ParseObject post : postList) {
                            String a = post.getObjectId();
                            String b = post.getString("Title");
                            String c = post.getString("Description");
                            String d = post.getString("isOpen");
                            String ee = post.getString("Category");
                            if (a.equals(DeleteObject)) {
                                post.put("isOpen", "false");
                                post.saveInBackground();
                            }
                        }
                    } else {
                        Log.d(getClass().getSimpleName(), "Error: " +     e.getMessage());
                    }
                }
            });
            return false;
        }
    });*/
}
public void generateReport(View v)
{
    Intent i = new Intent(con,GenerateReport.class);
    i.putExtra("dept1",dept);
    startActivity(i);
}
}

我在我的managerActivity中挂了listview的click事件,但是它不起作用.我还连接了我的自定义适配器.我在onclick事件中显示了一个测试吐司,但无法正常工作.请帮助

I have hookied on click event of my listview in my managerActivity but it is not working. I have also attached my custom adapter.I have displayed a test toast in the onclick event but it is not working. Kindly help

推荐答案

在适配器xml父级布局中粘贴以下行( android:descendantFocusability ="blocksDescendants" ),并粘贴 android:focusable适配器XML文件中的复选框或按钮中的="false" .

Paste the line (android:descendantFocusability="blocksDescendants") in you adapter xml parent layout and paste android:focusable="false" in the checkbox or button in the adapter xml file.

这篇关于ListView onItemClickListener无法正常工作. Onclick不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 06:37