Setting onClickListeners to lots of Views

时间:2016-04-12 00:41:56

标签: java android

I have a lots of Views(about 30, may be it will be more), like cards. I set onClickListener for each of them, but its does not working. No reaction. Here is my code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);
    final EditText searchProfile = (EditText) findViewById(R.id.search_profile);
    final Button btnSearch = (Button) findViewById(R.id.btn_search);
    final LinearLayout layoutProfileLeft = (LinearLayout) findViewById(R.id.layout_profiles_left);
    searchProfile.setHint("UserName...");
    btnSearch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String profileName = searchProfile.getText().toString();
            urlForProfile = "http://www.dotabuff.com/search?utf8=&q=" + searchProfile.getText().toString().replace(" ", "+") + "&commit=Search";
            while (htmlSrc == "" || htmlSrc == null) {
                setHtmlSrc(urlForProfile);
            }
            ArrayList<Integer> ids = new ArrayList<Integer>();
            for (int i = 0; i < 30; i++) {
                searchedHeroes.add(i, new SearchedHero(i));
                RelativeLayout l = new RelativeLayout(StartActivity.this);
                RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(-1,-2);
                rlp.setMargins(8, 0, 8, 8);
                l.setLayoutParams(rlp);
                l.setBackgroundColor(Color.rgb(69,90,100));
                l.setPadding(8,0,8,8);
                l.setId(i);
                //l.setLayoutParams(LinearLayout.);
                ImageView img = new ImageView(StartActivity.this);
                new DownloadImageTask(img).execute(searchedHeroes.get(i).getImageSrc());
                img.setOnClickListener(this);
                img.setId(i + 40);
                l.addView(img);
                RelativeLayout innerL = new RelativeLayout(StartActivity.this);
                RelativeLayout.LayoutParams paramsForInnerL = new RelativeLayout.LayoutParams
                        (-2, -2);
                paramsForInnerL.addRule(RelativeLayout.RIGHT_OF, img.getId());
                TextView txtProfileName = new TextView(StartActivity.this);
                txtProfileName.setText(searchedHeroes.get(i).getName());
                txtProfileName.setId(i + 80);
                TextView txtLastMatch = new TextView(StartActivity.this);
                txtLastMatch.setText(searchedHeroes.get(i).getLastMatch());
                txtProfileName.setTextSize(14);
                txtProfileName.setTextColor(Color.WHITE);
                txtLastMatch.setId(i + 120);
                RelativeLayout.LayoutParams paramsForNameLastMatch = new RelativeLayout.LayoutParams
                        (
                                RelativeLayout.LayoutParams.WRAP_CONTENT,
                                RelativeLayout.LayoutParams.WRAP_CONTENT
                        );
                innerL.addView(txtProfileName, rlp);
                paramsForNameLastMatch.addRule(RelativeLayout.BELOW, txtLastMatch.getId());
                innerL.addView(txtLastMatch, paramsForNameLastMatch);
                l.addView(innerL, paramsForInnerL);
                l.setOnClickListener(this);
                ids.add(i, l.getId());
                layoutProfileLeft.addView(l, rlp);
                //}
                //m.find();
                //passUrl = "http://www.dotabuff.com" + m.group(1);
                //Intent pass = new Intent(StartActivity.this, MainActivity.class);
                //pass.putExtra("EmpID", searchedHeroes.get(0).getProfileUrl());
                //startActivity(pass);
                //}
                //}
            }
        }
    });
}

@Override
public void onClick(View v) {
    Intent pass = new Intent(StartActivity.this, MainActivity.class);
    pass.putExtra("Link", searchedHeroes.get(v.getId()).getProfileUrl());
    startActivity(pass);
}

Probably, each view must intent other activity. Thanks.

2 个答案:

答案 0 :(得分:-1)

It seems to me that you forgot to set your views clickable: view.setClickable(true)

答案 1 :(得分:-1)

public class MainActivity extends Activity implements View.OnClickListener {
EditText searchProfile;
Button btnSearch;
LinearLayout layoutProfileLeft

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
searchProfile = (EditText) findViewById(R.id.search_profile);
btnSearch = (Button) findViewById(R.id.btn_search);
layoutProfileLeft = (LinearLayout) findViewById(R.id.layout_profiles_left);
searchProfile.setHint("UserName...");
btnSearch.setOnClickListener(this); 

}

@Override
public void onClick(View v) {
Intent pass = new Intent(StartActivity.this, MainActivity.class);
 String profileName = searchProfile.getText().toString();
        urlForProfile = "http://www.dotabuff.com/search?utf8=&q=" + searchProfile.getText().toString().replace(" ", "+") + "&commit=Search";
        while (htmlSrc == "" || htmlSrc == null) {
            setHtmlSrc(urlForProfile);
        }
        ArrayList<Integer> ids = new ArrayList<Integer>();
        for (int i = 0; i < 30; i++) {
            searchedHeroes.add(i, new SearchedHero(i));
            RelativeLayout l = new RelativeLayout(StartActivity.this);
            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(-1,-2);
            rlp.setMargins(8, 0, 8, 8);
            l.setLayoutParams(rlp);
            l.setBackgroundColor(Color.rgb(69,90,100));
            l.setPadding(8,0,8,8);
            l.setId(i);
            //l.setLayoutParams(LinearLayout.);
            ImageView img = new ImageView(StartActivity.this);
            new DownloadImageTask(img).execute(searchedHeroes.get(i).getImageSrc());
            img.setOnClickListener(this);
            img.setId(i + 40);
            l.addView(img);
            RelativeLayout innerL = new RelativeLayout(StartActivity.this);
            RelativeLayout.LayoutParams paramsForInnerL = new RelativeLayout.LayoutParams
                    (-2, -2);
            paramsForInnerL.addRule(RelativeLayout.RIGHT_OF, img.getId());
            TextView txtProfileName = new TextView(StartActivity.this);
            txtProfileName.setText(searchedHeroes.get(i).getName());
            txtProfileName.setId(i + 80);
            TextView txtLastMatch = new TextView(StartActivity.this);
            txtLastMatch.setText(searchedHeroes.get(i).getLastMatch());
            txtProfileName.setTextSize(14);
            txtProfileName.setTextColor(Color.WHITE);
            txtLastMatch.setId(i + 120);
            RelativeLayout.LayoutParams paramsForNameLastMatch = new RelativeLayout.LayoutParams
                    (
                            RelativeLayout.LayoutParams.WRAP_CONTENT,
                            RelativeLayout.LayoutParams.WRAP_CONTENT
                    );
            innerL.addView(txtProfileName, rlp);
            paramsForNameLastMatch.addRule(RelativeLayout.BELOW, txtLastMatch.getId());
            innerL.addView(txtLastMatch, paramsForNameLastMatch);
            l.addView(innerL, paramsForInnerL);
            l.setOnClickListener(this);
            ids.add(i, l.getId());
            layoutProfileLeft.addView(l, rlp);
        }
pass.putExtra("Link", searchedHeroes.get(v.getId()).getProfileUrl());
startActivity(pass);
}