【问题标题】:how to generate onclick listener for dynamically generated dynamic number of textviews in Androiod?如何为Android中动态生成的动态文本视图数生成onclick监听器?
【发布时间】:2016-10-11 16:23:32
【问题描述】:

我有一张表“笔记”,里面有 N 个笔记。对于每个注释,我想在我动态创建的单独文本视图中打印“标题”列的值。所以,对于 N 个笔记,我将有 N 个文本视图。 现在,每当用户单击任何 textView 时,我都想显示具有单击标题的那一行的所有其他数据。

问题:当我对这些动态创建的文本视图使用 onClickListener 时,当用户点击任何文本视图时,它总是生成最后一行(或最后一个动态创建的文本视图)的数据

public void dataview(){
    //onView is a userdefined function which simply executes a select query 
    Cursor res=db.onView();
    len=res.getCount();

    //below code is, If no notes stored in the table.
    if(res.getCount()==0)
    {
        Toast.makeText(ViewActivity.this,"No Notes.",Toast.LENGTH_SHORT).show();
        return;
    }

    //parsing the select query result using the Cursor res
    while(res.moveToNext()) {
        relative = (LinearLayout) findViewById(R.id.activity_view);
        relative.addView(createNewTextView(res.getString(1).toString()));
    }
}

public TextView createNewTextView(String text){
    final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    lparams.setMargins(0,10,0,0);

    //creating TextView and assigning properties here
    textView = new TextView(this);
    textView.setLayoutParams(lparams);
    textView.setText(text);
    index++;
    textView.setTextSize(18);
    textView.setBackgroundColor(Color.parseColor("#ff0099cc"));

    // onClick Action here
    final String title=textView.getText().toString();
    textView.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Cursor res1=db.onView();
                            while(res1.moveToNext()){
                                if(res1.getString(1).equals(title));
                                {
                                    final String text1=res1.getString(0);
                                    Intent i=new Intent("com.example.dell.todolister.ViewNoteActivity");
                                    i.putExtra("TextBox1",text1);
                                    startActivity(i);
                                    break;
                                }
                            }
                        }
                    }
            );
    return textView;
}

【问题讨论】:

  • 你为什么不用listview或者recyclerview?

标签: android


【解决方案1】:

我认为您需要为文本视图提供 id。

初始化全局变量,如 int i = 1;

然后在createNewTextView中,textView.setId(i)

在最后你返回一个视图增量 i = i + 1;

所以对于下一个 textView,您将有不同的文本视图 ID。

【讨论】:

    【解决方案2】:

    你应该使用 id 来识别 textview 来显示文本,使用这个 --> v.getid() inside onclick(View v)

    【讨论】:

      【解决方案3】:

      我花了 24 小时来找到这个问题的解决方案,我认为这个问题与某些特定于侦听器的逻辑有关。

      但问题是该死的分号';'在导致所有问题的这条线上

      if(res1.getString(1).equals(title)); // Here :'(
      {
          final String text1=res1.getString(0);
          Intent i=new Intent("com.example.dell.todolister.ViewNoteActivity");
          i.putExtra("TextBox1",text1);
          startActivity(i);
          break;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多