动态TextViews OnCLickListener不会在第一次加载Activity时触发

时间:2013-01-11 05:20:12

标签: android onclicklistener

我的LinearLayout填充了TextViews。这些TextViews中的每一个都设置为相同的OnClickListener。监听器会被触发,但仅限于活动加载的第二次。

我怎样才能让它第一次触发/为什么它不是第一次触发?

public static void loadArticlesIntoSimilarList(Context mContext, String myid)
{
    //POPULATES THE LIST OF ARTICLES
    //associatedArticles = articlesDataSource.getAssociatedArticlesData(articleId);
    for(final Article similarArticle : articlesDataSource.getAssociatedArticlesList(myid))
    {
        TextView similarArticleTextView = new TextView(mContext);
        similarArticleTextView.setTextColor(mContext.getResources().getColor(R.color.meddark_gray));

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);

        similarArticleTextView.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
        layoutParams.setMargins(ConversionHelper.dpToPx(5), 0, ConversionHelper.dpToPx(5), ConversionHelper.dpToPx(20));

        similarArticleTextView.setText(similarArticle.title);
        similarArticleTextView.setId((int) similarArticle.id);

        similarArticlesLinearLayout.addView(similarArticleTextView);
        similarArticleTextView.setClickable(true);
        similarArticleTextView.setOnClickListener(relatedArticleClickListener);
        similarArticleTextView.setLayoutParams(layoutParams);
    }
}

我首先在顶部设置侦听器

private static OnClickListener relatedArticleClickListener;

功能是:

    //RELATED ARTICLE CLICK
    relatedArticleClickListener = new OnClickListener()
    {
        public void onClick(View v) {
            Log.d("MYLOG", "RL Article clicked " + v.getId());
            Intent myIntent = new Intent(v.getContext(),MainActivity.class);
            myIntent.putExtra("id", v.getId());
            startActivity(myIntent);
            finish();
        }
    };

0 个答案:

没有答案