如何通过按钮单击动态创建两个活动(一个或另一个)?

时间:2017-03-05 01:52:09

标签: java android

我有一个将文本视图加载到其中的活动。我为这些文本视图添加了一个单击侦听器,并希望它们根据我单击的内容打开一个具有不同值的活动。它最终表明无论我点击哪一个,都会显示相同的结果,更准确地说,我在创建它时会使用相同的信息 - 我不想这样做。

create temporary table temp 
(cnumber VARCHAR(45) not null
, lclass VARCHAR(45) not null
,taken boolean not null default false
);

insert into temp(cnumber,lclass)
  select uclass,lclass from pre;


update temp,taken set temp.taken=true where temp.lclass=taken.cnumber;

select * from csclass where cnumber not in (
  (select distinct cnumber from temp where taken=0) union(select cnumber   from taken where username = "1")
);

使用两个文本视图,这会导致第二个视图的ID始终为我启动的活动中的键值对的值。我不确定我此刻做错了什么。我相信我的问题在于本节,因为我无法看到它可能来自哪里。

欢迎任何有关我的代码的帮助或建议。谢谢。

这仍然没有解决,因此我将专注于问题领域:

public void setTextToTextView (JSONArray jsonArray)
{
    RelativeLayout layout = (RelativeLayout) findViewById(R.id.activity_main);
    String s = "";
    for (int i = 0; i < jsonArray.length(); i++) {
        TextView info = new TextView(this); //actually really confused as to what the context I'm setting is - why this? Just saw other people do it like so

        JSONObject json = null;
        try {
            json = jsonArray.getJSONObject(i);
            s = s + "ID : " + json.getString("Id") + " Parent: " + json.getString("Parent") +
                    " Content: " + json.getString("Content") + " User: " + json.getString("User") +
                    " Timestamp: " + json.getString("Timestamp") + "\n\n";
        } catch (JSONException e) {
            e.printStackTrace();
        }
        info.setText(s);
        try {
            info.setId(Integer.parseInt(json.getString("Id")));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent(MainActivity.this,
                        NewActivity.class);
                myIntent.putExtra("key", v.getId()); //this is always the same
                startActivity(myIntent);
            }
        });
        layout.addView(info);
    }
}

无论我做了什么改变,每次都会给我完全相同的v.getId / v.getTag。

2 个答案:

答案 0 :(得分:1)

作为第一步,我会移动OnclickListener以超越循环,因为它总是做同样的事情:

View.OnClickListener view_ocl = new View.OnClickListener() {
    @Override
    public void onClick(View v)
    {
        Intent myIntent = new Intent(MainActivity.this, NewActivity.class);
        myIntent.putExtra("key", v.getId()); //this is always the same
        startActivity(myIntent);
    }};

for(int i =0; i <jsonArray.length(); i++)
{
    // ... as before
    info.setOnClickListener(view_ocl);
    layout.addView(info);
}

正如您所做的那样,您需要明确设置Id,或以某种方式在视图中添加标识符,以便onClick代码(每个View都相同) ),知道哪个View已被点击。目前,您依赖于系统为视图提供的任意Id

答案 1 :(得分:1)

我使用您的代码创建了一个新项目,但结果正如您所希望的那样正确。您确定第三个TextView不包含之前的内容吗?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);        
    LinearLayout layout = (LinearLayout)findViewById(R.id.activity_main);

    int [] a = {1,2};        
    String [] s = {"textView1","textView2"};
    for(int i =0; i <a.length; i++)
    {
        TextView info = new TextView(this); //actually really confused as to what the context I'm setting is - why this? Just saw other people do it like so
        info.setText(s[i]);
        info.setId(a[i]);
        info.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Toast.makeText(getApplicationContext(),
                       "clicked:"+v.getId(),Toast.LENGTH_SHORT).show();
                Intent myIntent = new Intent(MainActivity.this,
                        NewActivity.class);
                myIntent.putExtra("key", v.getId()); //this is always the same
                startActivity(myIntent);
            }
        });
        layout.addView(info);
    }

gif:

https://i.stack.imgur.com/gvV27.gif