ListView onItemClickListener无法正常工作。 Onclick不起作用

时间:2016-04-28 14:07:05

标签: java android listview

适配器

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事件中显示了一个测试吐司,但它无法正常工作。请帮助

2 个答案:

答案 0 :(得分:3)

在适配器xml父布局中粘贴该行( android:descendantFocusability =&#34; blocksDescendants&#34; )并粘贴 android:focusable =&#34; false&#34; 在适配器xml文件中的复选框或按钮中。

答案 1 :(得分:0)

查看选定的答案here - 这提供了一种完成您想要做的事情的方法。但是如果你看一下this answer,你需要确认你没有在行布局中设置可聚焦对象 - 因为这样做会阻止ListView获取触摸事件。