2016-03-28 44 views
1

如何在viewpager中獲取textview(currentview)的值?如何在viewpager中獲取textview(currentview)的值?

問題 我的gettext TextView的,但現在當前放映的TextView其吉=下一視圖的的TextView的ttingtext

類測試

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(); 

     } 

    } 


} 

方法我使用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(); 
    } 
} 

方法我稱之爲像

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> 

回答

0

代碼,您mentione d不清楚。 嘗試下面的代碼來查看傳呼機

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

      int position = mViewPager.getCurrentItem(); 

      setClipboard(testContactName.get(position)); 
     } 
    }); 

private void setClipboard(String text) { 
    Log.v(TAG, "check..."+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(); 
    } 
} 
+0

這裏獲得當前聯繫人姓名例嘗試一次https://www.dropbox.com/s/cfhvsjrb7qj4xks/SamplePro.zip?dl=0 –

相關問題