如何获得viewpager的TextView(有currentview)的价值? [英] how get value of textview (with currentview ) in viewpager?

查看:326
本文介绍了如何获得viewpager的TextView(有currentview)的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何viewpager得到的TextView(有currentview)的价值?

问题
我的gettext的TextView,但现在目前的表现的TextView其哎呀=下一个视图的TextView中的ttingtext

类测试

 包名;进口android.app.Activity;
进口android.content.Context;
进口android.database.Cursor;
进口android.database.sqlite.SQLiteDatabase;
进口android.os.Bundle;
进口android.support.v4.view.PagerAdapter;
进口android.support.v4.view.ViewPager;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.TextView;进口的java.util.ArrayList;
进口的java.util.List;/ **
 *创建者的名字在26-MAR-16。
 * /
公共类测试扩展活动{    私有静态最后弦乐DB_NAME =;
    私有静态最后弦乐TABLE_NAME =;
    私人SQLiteDatabase数据库;
    私人列表<串GT; testContactName =新的ArrayList<>();
    公共MyAdapter mAdapter;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.test);
        ViewPager mViewPager =(ViewPager)findViewById(R.id.testvp);
        mAdapter =新MyAdapter();
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(0);
        mViewPager.setOffscreenPageLimit(50);        getAllContacts();    }    公共无效getAllContacts(){
        DatabaseHelper dbOpenHelper =新DatabaseHelper(这一点,DB_NAME);
        数据库= dbOpenHelper.openDataBase();        查询字符串= + TABLE_NAME从选择*;        光标光标= database.rawQuery(查询,NULL);        如果(指针!= NULL){
            cursor.moveToFirst();
            对于(INT C = 0; C< cursor.getCount(); C ++){
                字符串名称= cursor.getString(cursor.getColumnIndex(内容));
                cursor.moveToNext();
               / *字符串数=将String.valueOf(cursor.getCount());
                Toast.makeText(getApplicationContext(),计数,Toast.LENGTH_LONG).show(); * /
                testContactName.add(名);                // testContactName.notifyAll();                Log.i(姓名:++---,名);            } mAdapter.notifyDataSetChanged();
        }
        cursor.close();
    }    私有类MyAdapter扩展PagerAdapter {        公共MyAdapter(){
            超();
        }        @覆盖
        公众诠释的getCount(){            返回testContactName.size();        }
        @覆盖
        公共布尔isViewFromObject(查看集合,Object对象){            返回对象==收集;
        }        @覆盖
        公共对象instantiateItem(ViewGroup中收集,诠释位置){            LayoutInflater吹气=(LayoutInflater)collection.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);            查看查看= inflater.inflate(R.layout.test1,NULL);
           TextView的页面编号;
            页面编号=(TextView中)view.findViewById(R.id.tv_test1);            尝试{
                pagenumber.setText(testContactName.get(位置));            }赶上(例外E1){
                // TODO自动生成catch块
                e1.printStackTrace();
            }
            (集合).addView(视图);
            返回视图。        }        @覆盖
        公共无效destroyItem(ViewGroup中收集,INT位置,对象视图){
            (集合).removeView((视图)视图);
            mAdapter.notifyDataSetChanged();        }    }
}

方法我使用copytoclipborad

 私人无效setClipboard(查看上下文中,字符串文本){
    如果(android.os.Build.VERSION.SDK_INT< android.os.Build.VERSION_ codeS.HONEYCOMB){
        android.text.ClipboardManager剪贴板=(android.text.ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setText(文本);
        Toast.makeText(getApplicationContext(),文本,Toast.LENGTH_LONG).show();
    }其他{
        android.content.ClipboardManager剪贴板=(android.content.ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
        android.content.ClipData夹= android.content.ClipData.newPlainText(复制的文本,文本);
        clipboard.setPrimaryClip(剪辑);
        Toast.makeText(getApplicationContext(),复制到剪贴板,Toast.LENGTH_LONG).show();
    }
}

方法我称之为像

  imgcopy.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){            setClipboard(mViewPager.getFocusedChild(),pagenumber.getText()的toString());        }
    });

的test.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直的android:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT>    < android.support.v4.view.ViewPager
        机器人:ID =@ + ID / testvp
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent>
    < /android.support.v4.view.ViewPager>
   < / RelativeLayout的>

test1.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直的android:layout_width =match_parent
    机器人:layout_height =match_parent>    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:textAppearance =机器人:ATTR / textAppearanceSmall
        机器人:文字=小文本
        机器人:ID =@ + ID / tv_test1
        机器人:layout_gravity =顶|中心
        机器人:layout_alignParentTop =真
        机器人:layout_centerHorizo​​ntal =真/>
< / RelativeLayout的>


解决方案

问题是鉴于寻呼机的默认行为。 ViewPager同时初始化两个片段。

由于片段数0安培; 1将首先被初始化。你的的TextView页面编号将有TextView中的片段号参考1 所以pagenumber.gettext()将返回段1号的文本。

有关为什么您使用的TextView在这里您可以用testContactName简单viewpage.getCurrentItem得到它()获得来自片段数0,则文字。

而不是:

setClipboard(mViewPager.getFocusedChild(),pagenumber.getText()的toString());

这样写:

setClipboard(mViewPager.getFocusedChild(),testContactName.get(mViewPager.getCurrentItem()).toString());

how get value of textview (with currentview ) in viewpager?

Problem i gettext of textview but now current showing textview its gee=ttingtext of next view's textview

class Test

package "name";

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by name on 26-Mar-16.
 */
public class Test extends Activity {

    private static final String DB_NAME = "";
    private static final String TABLE_NAME = "";
    private SQLiteDatabase database;


    private List<String> testContactName = new ArrayList<>();
    public MyAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);


        ViewPager mViewPager = (ViewPager) findViewById(R.id.testvp);
        mAdapter = new MyAdapter();
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(0);
        mViewPager.setOffscreenPageLimit(50);

        getAllContacts();

    }

    public void getAllContacts() {
        DatabaseHelper dbOpenHelper = new DatabaseHelper(this, DB_NAME);
        database = dbOpenHelper.openDataBase();

        String query = "select * from " + TABLE_NAME;

        Cursor cursor = database.rawQuery(query, null);

        if (cursor != null) {
            cursor.moveToFirst();
            for (int c = 0; c < cursor.getCount(); c++) {
                String name = cursor.getString(cursor.getColumnIndex("content"));
                cursor.moveToNext();
               /* String count= String.valueOf(cursor.getCount());
                Toast.makeText(getApplicationContext(), count , Toast.LENGTH_LONG).show();*/
                testContactName.add(name);

                //  testContactName.notifyAll();

                Log.i("Name: " + "" + "---", name);

            }mAdapter.notifyDataSetChanged();
        }
        cursor.close();
    }

    private class MyAdapter  extends PagerAdapter {

        public MyAdapter() {
            super();
        }

        @Override
        public int getCount() {

            return testContactName.size();

        }
        @Override
        public boolean isViewFromObject(View collection, Object object) {

            return object == collection;
        }

        @Override
        public Object instantiateItem(ViewGroup collection, int position) {

            LayoutInflater inflater = (LayoutInflater) collection.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View view = inflater.inflate(R.layout.test1, null);


           TextView pagenumber;
            pagenumber = (TextView) view.findViewById(R.id.tv_test1);

            try {
                pagenumber.setText(testContactName.get(position));

            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            ( collection).addView(view);
            return view;

        }



        @Override
        public void destroyItem(ViewGroup collection, int position, Object view) {
            ( collection).removeView((View) view);
            mAdapter.notifyDataSetChanged();

        }

    }


}

method i use to copytoclipborad

private void setClipboard(View context,String text) {
    if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
        android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setText(text);
        Toast.makeText(getApplicationContext(),text,Toast.LENGTH_LONG).show();
    } else {
        android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", text);
        clipboard.setPrimaryClip(clip);
        Toast.makeText(getApplicationContext(),"Copied To ClipBoard",Toast.LENGTH_LONG).show();
    }
}

method i called like

imgcopy.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            setClipboard(mViewPager.getFocusedChild(), pagenumber.getText().toString());

        }
    });

test.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v4.view.ViewPager
        android:id="@+id/testvp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>
   </RelativeLayout>

test1.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/tv_test1"
        android:layout_gravity="top|center"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

解决方案

The problem is default behavior of view pager. ViewPager initializes two fragments at a time.

As fragment number 0 & 1 will be initialized first. Your "TextView pagenumber" will be having the reference of TextView in Fragment number 1 so pagenumber.gettext() will return the text in fragment number 1.

For getting the text from fragment number 0, Why are you using textview where you can simply get it from testContactName with viewpage.getCurrentItem().

Instead of :

setClipboard(mViewPager.getFocusedChild(), pagenumber.getText().toString());

Write this :

setClipboard(mViewPager.getFocusedChild(),testContactName.get(mViewPager.getCurrentItem()).toString());

这篇关于如何获得viewpager的TextView(有currentview)的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆