开发者

how to get text of the neighbour textviews on some other textview's onclick event in dynamic table?

开发者 https://www.devze.com 2023-02-27 10:20 出处:网络
i have textviews in table layout like this ---------------- |txt1|txt2|txt3| ---------------- |txt4|txt5|txt6|

i have textviews in table layout like this

----------------
|txt1|txt2|txt3|
----------------
|txt4|txt5|txt6|
----------------
|txt7|txt8|txt9|
----------------
|txt1|txt2|txt3|
----------------
|txt4|txt5|txt6|
----------------
|txt7|txt8|txt9|

and so on and on and on0

in this manner i am adding textviews again and again in for loop in the table layout acc to the data which is coming dynamically...

on txt7 click listener event i can get the text of the txt7 like this

((TextView)v).getText().toString();

now i need text of txt8 and txt9 text on txt7 click event

some body suggested me to do this by tagging textview

TextView uniqueTextView = (TextView)findViewById(R.id.unique_id);
TextView neighbourView = new TextView();
neighbourView.setTag(uniqueTe开发者_如何学JAVAxtView);

public void onTextViewClick(TextView view) {
TextView uniqueTextView = (TextView)view.getTag();
String text = uniqueTextView.getText().toString();   }

but this for one textview... how can i get text of both textview(txt8+txt9) on txt7 click listener.... plus i need text from txt7 also....

if it is not clear then you can comment....

this is dynamic table which i am creating

TableRow tr1 = (TableRow) new TableRow(this);
TextView txt1=new TextView(this);
TextView txt2=new TextView(this);
TextView txt3=new TextView(this);
txt1.setText("Data1);
txt2.setText("Data2);
txt3.setText("Data3);
txt1.setTag(1, txt2.getText().toString());
txt1.setTag(2, txt3.getText().toString());

txt1.setOnClickListener(new View.OnClickListener() 
{
     public void onClick(View v) 
     {
    try
    {
          Log.i("asdasd","Yahoo");
              Toast(((TextView)v).getTag(1).toString());
              Toast(((TextView)v).getTag(2).toString());
    }
    catch(Exception e)
    {
         Log.i("Error came while getting tag of txt1", e.getMessage());
        Toast("unable to bind tag");
        }
     }
});

tr1.addView(txt1);
tr1.addView(txt2);
tr1.addView(txt3);
TableLayout tl.addView(tr1,new TableLayout.LayoutParams(layoutParams));

as i cant set txt1 in place of ((TextView)v) because it will always give text of the last tablerow's txt1

04-20 12:42:39.236: ERROR/AndroidRuntime(522): Uncaught handler: thread main exiting due to uncaught exception
04-20 12:42:39.476: ERROR/AndroidRuntime(522): java.lang.NullPointerException: println needs a message
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at android.util.Log.println(Native Method)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at android.util.Log.i(Log.java:136)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at com.connection.demotable$1.onClick(demotable.java:431)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at android.view.View.performClick(View.java:2364)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at android.view.View.onTouchEvent(View.java:4179)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at android.widget.TextView.onTouchEvent(TextView.java:6541)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at android.view.View.dispatchTouchEvent(View.java:3709)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at android.os.Looper.loop(Looper.java:123)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at android.app.ActivityThread.main(ActivityThread.java:4363)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at java.lang.reflect.Method.invokeNative(Native Method)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at java.lang.reflect.Method.invoke(Method.java:521)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-20 12:42:39.476: ERROR/AndroidRuntime(522):     at dalvik.system.NativeStart.main(Native Method)


why don't you try setTag (int key, Object tag) and getTag(int)


As in the example code you posted try this

txt7.setTag(1, txt8);
txt7.setTag(2, txt9);

public void onTextViewClick(TextView view) {
TextView txt8 = (TextView)view.getTag(1);
TextView txt9 = (TextView)view.getTag(2);   }


I have not used table layout yet. But you may can use like this

TableRow t =  (TableRow) v.getParent();
TextView t1 = (TextView) t.getChildAt(0);
TextView t2 = (TextView) t.getChildAt(1);
TextView t3 = (TextView) t.getChildAt(2);
0

精彩评论

暂无评论...
验证码 换一张
取 消