Can you explain How to display another textview in this code? I'm using viewpager so i just print only one textview i want one more textview. please anybody can you help me. thanks in advance.
this is my code
This is my Mainactivity class
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set title for the GridView
setTitle("GridView");
// Get the view from grid_view.xml
setContentView(R.layout.grid_view);
// Set the images from ImageAdapter.java to GridView
GridView gridview = (GridView) findViewById(R.id.gridview);
//gridview.setAdapter(new ImageAdapter(this)); //commented line 05052018
gridview.setAdapter(new ChangesofAdapter(this)); //new line 05052018
// Listening to GridView item click
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Launch ImageViewPager.java on selecting GridView Item
Intent i = new Intent(getApplicationContext(), ImageViewPager.class);
// Show a simple toast message for the item position
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
// Send the click position to ImageViewPager.java using intent
i.putExtra("id", position);
// Start ImageViewPager
startActivity(i);
}
});
}
}
this is my gridview class -> ChangesofAdapter.class
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()]);
}
}
This code is represents while click grid view items this page will call this page is have viewpager of grid view items this class is viewpager -> ImageViewpager.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);
}
}
}
This is my application look
This is my Gridview look and while click item number seven this item will open viewpager that is second look.
This is my viewpager look in this look i want more textview.
how to display another textview below item(seven) or above item(seven)
This is my code can any one help me. i'm new one of Android programmer learner.
Please........
Thanks in advance.