获取以编程方式添加的最底层TextView值?

时间:2014-08-18 08:39:17

标签: android android-layout sharedpreferences textview

我在TextView上以编程方式添加LinearLayout

enter image description here

然后我在clickTextView,我将TextView的IDsTexts传递给另一个Activity(使用SharedPreferences)。

但是当我从另一个活动中的SharedPreferences获取数据并看到Log的数据时,我只得到IDValue最底层的TextView

(例如,我只看到TextView_3)的数据。

但也许我在TextView上有多个LinearLayout,我点击了第二个TextView或另一个TextView,但它只是获取了最底层TextView的数据。

public class ChatPage extends Activity {
TextView[] txt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_page);
          txt = new TextView[totalPersons];
          for (int s = 0; s < listOfPersons.getLength(); s++) {
              txt[s] = new TextView(ChatPage.this);
              if (group.equals("Admin")) {
                txt[s].setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View arg0) {
                                startActivity(new Intent(
                                    ChatPage.this,
                                    ConversationPage.class));
                                    editor.putString("Adminid", id);
                                    editor.putString("NameAdmin", name);
                                    editor.commit();
                            }
                   });
              }
              if (group.equals("User")) {
                txt[s].setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View arg0) {
                                startActivity(new Intent(
                                    ChatPage.this,
                                    ConversationPage.class));
                                    editor.putString("Userid", id);
                                    editor.putString("NameUser", name);
                                    editor.commit();
                            }
                   });
              }
          }
    }
}

另一个Activity我获取数据:

UserID = (shared.getString("Userid", "NULLID"));
UserNAME = (shared.getString("NameUser", "NULLNAME"));

IDAdmin = (shared.getString("Adminid", "NULL_idSharee"));
AdminName = (shared.getString("NameAdmin", "NULL_NameSharee"));
Log.i("test", "UserID " + UserID );
Log.i("test", "UserNAME " + UserNAME );
Log.i("test", "IDAdmin " + IDAdmin );
Log.i("test", "AdminName " + AdminName );

4 个答案:

答案 0 :(得分:1)

这是一个java问题,你需要为每个点击监听器实例化新类,你不能这样做。在代码中创建一个私有类

 private class MyOnClickListener implements OnClickListener {
    private final String    mId;

    public MyOnClickListener(String id) {
        mId= id;
    }

    public void onClick(View v) {
        Intent intent= new Intent(getApplicationContext(), ACtivity.class);
        intent.putExtra("current_post_id", mId);
        startActivity(intent);
    }

}

然后

textView.setOnClickListener(new MyOnClickListener(id));

答案 1 :(得分:1)

添加您的观点:

final View view = getLayoutInflater().inflate(R.layout.yourLayout,
                yourLinearLayout, false);
        TextView text= (TextView ) view.findViewById(R.id.chapter_page);
        view.setTag(unique Id);//your TextView's id that later use in onClick
        text.setText(yourText);
        goToPage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                int id = (Integer) view.getTag();//this return clicked textView id
                //do some thing...
            }
        });

        yourLinearLayout.addView(view);  

您可以将此代码放在循环中,并将视图添加到linearLayout多个。

答案 2 :(得分:1)

你应该为你的文本视图提供一个唯一的ID

示例

public void add_Text_view_row(String name,int i){
     TextView a = new TextView(getActivity());
        a.setText(name);


        a.setId(17*i);

        linearlayout_your.addView(a);

         a.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // your codes should be here

        }
    });


}

你可以在循环中使用这个方法。你应该将数据传递给那些参数

答案 3 :(得分:1)

您的回复是:

///In your Oncreate
txt[s].setText(usename);
txt[s].setLayoutParams(params);
txt[s].setTextColor(Color.BLACK);
txt[s].setClickable(true);
txt[s].setId(5*s);
txt[s].set

OnClickListener(new listen(s,idTwo,nameTwo));
    //----//
////And then create a class
public class listen implements OnClickListener{
    int bId;  
    String Pid;
    String Pname;
    listenSharee(int _id,String _Pid,String _Pname) {  
        bId = _id;  
        Pid = _Pid;
        Pname = _Pname;
    } 

    @Override
    public void onClick(View v) {
        startActivity(new Intent(ChatPage.this,YourClass.class));
        editor.putString("Aname", Pid);
        editor.putString("Aname", Pname);
        editor.commit();
        //Toast.makeText(getBaseContext(), txt[bId].getText()+ " * " + Pid + " * " + Pname, Toast.LENGTH_LONG).show();
    }
}
祝你好运。 ;)