如何在此代码中显示另一个textview?

时间:2018-05-12 15:44:22

标签: android gridview android-viewpager

您能解释一下如何在此代码中显示另一个textview吗?我正在使用viewpager所以我只打印一个textview我还想要一个textview。拜托,任何人都可以帮助我。 提前谢谢。

这是我的代码

这是我的主要活动课程

public class ChangesofAdapter extends BaseAdapter {
    private Context mContext;
    public ChangesofAdapter(Context c) {
        mContext = c;
    }
    public int getCount() {
        return getDataContacts().length;
    }
    public Object getItem(int position) {
        return getDataContacts()[position];
    }
    public long getItemId(int position) {
        return 0;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView imageView;
        if (convertView == null) {  // If it's not recycled, initialize some attributes
            imageView = new TextView(mContext.getApplicationContext());
            imageView.setTextColor(Color.GREEN);
            imageView.setGravity(Gravity.LEFT); // new line 05052018
            imageView.setTextSize(30);
        } else {
            imageView = (TextView) convertView;
        }
        String abc =getDataContacts()[position];
        imageView.setText(abc); // new line 05052018
        return imageView;
    }
    public String[] getContacts(){
        DBUtils utils = new DBUtils(mContext.getApplicationContext());
        ArrayList<String> names = new ArrayList<String>();
        new DBUtils((mContext.getApplicationContext()));
        try {
            DBUtils.createDatabase();
        } catch (IOException e) {
            Log.w(" Create Db "+e.toString(),"===");
        }
        DBUtils.openDatabase();
        Cursor cursor = utils.getResult("select * from Cflviewpagerdata order by title");
        cursor.moveToFirst();
        while(!cursor.isAfterLast()) {
            names.add(cursor.getString(cursor.getColumnIndex("view")));
            cursor.moveToNext();
        }
        cursor.close();
        DBUtils.closeDataBase();
        return names.toArray(new String[names.size()]);
    }
    public String[] getDataContacts(){
        DBUtils utils = new DBUtils(mContext.getApplicationContext());
        ArrayList<String> names = new ArrayList<String>();
        new DBUtils((mContext.getApplicationContext()));
        try {
            DBUtils.createDatabase();
        } catch (IOException e) {
            Log.w(" Create Db "+e.toString(),"===");
        }
        DBUtils.openDatabase();
        Cursor cursor = utils.getResult("select * from Cflviewpagerdata order by title");
        cursor.moveToFirst();
        while(!cursor.isAfterLast()) {
            names.add(cursor.getString(cursor.getColumnIndex("title")));
            cursor.moveToNext();
        }
        cursor.close();
        DBUtils.closeDataBase();
        return names.toArray(new String[names.size()]);
    }
}

这是我的gridview类 - &gt; ChangesofAdapter.class

    public class ImageViewPager extends Activity implements OnInitListener {
    // Declare Variable
    int position;
    Bundle img = new Bundle();
    ViewPager vp;
    private TextToSpeech mTts;
    List<TextView> images;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set title for the ViewPager
        setTitle("ViewPager");
        // Get the view from view_pager.xml
        setContentView(R.layout.view_pager);
        mTts = new TextToSpeech(this, this);
        // Retrieve data from MainActivity on item click event
        Intent p = getIntent();
        position = p.getExtras().getInt("id");  
        ChangesofAdapter imageAdapter = new ChangesofAdapter(this); //new line 05052018
        images = new ArrayList<TextView>(); //new line 05052018

        // Retrieve all the images
        for (int i = 0; i < imageAdapter.getCount(); i++) {
            TextView imageView = new TextView(this); //new line 05052018
            String abc = imageAdapter.getContacts()[i];
            imageView.setText(imageAdapter.getContacts()[i]);  //new line 05052018
            imageView.setTextColor(Color.BLUE);
            imageView.setGravity(Gravity.CENTER_HORIZONTAL);//new line 05052018
            img.putString("CURRENT_POSITION", abc);
            images.add(imageView);
        }
        vp=(ViewPager)findViewById(R.id.pager);         //new line 06052018
        vp.setAdapter(new ViewPagerAdapter(images));    //new line 06052018
        vp.setOffscreenPageLimit(0);
        vp.setCurrentItem(position);                    //new line 06052018
    }

    public class ViewPagerAdapter extends PagerAdapter {
        private List<TextView> images;  
        public ViewPagerAdapter(List<TextView> images) {
            this.images = images;
        }   
        @Override
        public Object instantiateItem(final ViewGroup container, int position) {
            final TextView imageView = images.get(position);                              //commented line on date 06052018
            container.addView(imageView);
            imageView.setTextSize(120);
            vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                public void onPageSelected(int pos) {
                 int  currentposition = pos;
                 mTts.speak(images.get(currentposition).getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
                }
                public void onPageScrolled(int arg0, float arg1, int arg2) {                
                }
                public void onPageScrollStateChanged(int arg0) {
                }
            });     
            return imageView;
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(images.get(position));
        }   
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return images.size();
        }
        @Override
        public boolean isViewFromObject(View view, Object o) {
            // TODO Auto-generated method stub
            return view == o;
        }
    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        if (mTts != null) {
            mTts.shutdown();
        }
    }
    @Override
    protected void onDestroy() {
        //Close the Text to Speech Library
        if(mTts != null) {
            mTts.stop();
            mTts.shutdown();
        }
        super.onDestroy();
    }
    public void onInit(int arg0) {
        if(arg0 == TextToSpeech.SUCCESS){
            mTts.speak(images.get(position).getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
        }
    }
}

此代码表示单击网格视图项时此页面将调用此页面具有网格视图项的viewpager 此类是viewpager - &gt; ImageViewpager.class

results

这是我的应用程序外观

这是我的Gridview外观,当点击第七项时,这个项目将打开第二次看的viewpager。

enter image description here

这是我的viewpager外观,我想要更多的textview。

如何在项目(七)或以上项目(七)下面显示另一个文本视图

enter image description here

这是我的代码可以任何人帮助我。我是Android程序员学习者的新成员。

请........

提前致谢。

1 个答案:

答案 0 :(得分:0)

你有两种可选方式

就像我看到你使用其中一个所以你可以继续使用它

第一种方式:以编程方式

imageView = new TextView(mContext.getApplicationContext());
        imageView.setTextColor(Color.GREEN);
        imageView.setGravity(Gravity.LEFT); // new line 05052018
        imageView.setTextSize(30);

所以你可以在这里添加更多的ANYOBJECT,但将它们放入容器

检查出来

  

https://stackoverflow.com/a/26133978/6998825

SEC方式:模块和REFF

通过这种方式,您将创建XML布局并在baseadapter中对其进行充气,以便您可以使用它的元素

row_module.xml FOR EX

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

<ImageView
    android:id="@+id/table_IMG"
    android:layout_width="120dp"
    android:layout_height="120dp" />
<TextView
    android:id="@+id/table_num"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="20dp"
    android:textColor="#000000"
    android:layout_margin="40dp"/>
<TextView
    android:id="@+id/table_ID"
    android:layout_width="0dp"
    android:layout_height="0dp" />
</RelativeLayout>
你的适配器中的

就是这样的

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.table_model, null);

    TextView TV_ID = (TextView)vi.findViewById(R.id.table_ID); // ID
    ImageView bgImg =(ImageView)vi.findViewById(R.id.table_IMG); // IMG
    TextView TV_details = (TextView)vi.findViewById(R.id.table_num); // details

    HashMap<String, String> arraylist_type = new HashMap<String, String>();
    arraylist_type = data.get(position);

    // Setting all values in listview
    TV_ID.setText(arraylist_type.get("ID"));

    Glide.with(activity).load(arraylist_type.get("IMG")).into(bgImg);

    TV_details.setText(arraylist_type.get("details"));

    return vi;
}