Accessing an element-Button in grid view - android

I am supposed to access a button of an element in a grid view and supposed to capture the onItemClick() activity of that button, but somehow I am not able to do it. Help me out. I am posting my code below. Please check it.
package com.nik.cardone;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
public class Fragment2 extends Fragment{
GridView fragment2Grid;
POJOMenuGrid pojo;
ArrayList<POJOMenuGrid> arr;
String[] name = {"Manchow Soup","Clear Soup","Mushroom Soup","Corn Soup","Tomato Soup"};
String[] desc = {"Soya water with capsicum , carrots , medium spicy"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.new_order_fragment_2, null);
initialize();
fragment2Grid = (GridView)v.findViewById(R.id.fragment2Grid);
fragment2Grid.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
View v = arg0.getChildAt(arg2);
// TODO Auto-generated method stub
System.out.println("In grid :");
}
});
for(int i=0;i<5;i++){
arr.add(new POJOMenuGrid(name[i], desc[0], R.drawable.t2));
}
System.out.println(arr.get(0).getName()+" "+arr.get(0).getDescription()+" ");
CustomGridAdapter adapter = new CustomGridAdapter(v.getContext(), arr);
fragment2Grid.setAdapter(adapter);
System.out.println("adapter set");
return v;
}
private void initialize() {
// TODO Auto-generated method stub
arr = new ArrayList<POJOMenuGrid>();
}
}
Custom Adapter
package com.nik.cardone;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomGridAdapter extends BaseAdapter{
Context context;
ArrayList<POJOMenuGrid> arr;
LayoutInflater inflater;
TextView itemName;
TextView itemDescription;
ImageView itemImage;
Button plus;
Button minus;
TextView counter;
public CustomGridAdapter(Context context, ArrayList<POJOMenuGrid> arr) {
super();
this.context = context;
this.arr = arr;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
System.out.println("in constructor");
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return arr.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arr.get(arg0);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
System.out.println("in adapter 1");
arg1 = inflater.inflate(R.layout.grid_single_item, null);
System.out.println("in adapter 2");
itemName = (TextView)arg1.findViewById(R.id.itemName);
itemDescription = (TextView)arg1.findViewById(R.id.itemDescription);
//counter = (TextView)arg1.findViewById(R.id.counter);
itemImage = (ImageView)arg1.findViewById(R.id.itemImage);
plus = (Button)arg1.findViewById(R.id.plus);
minus = (Button)arg1.findViewById(R.id.minus);
plus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
System.out.println("In plus :");
// TODO Auto-generated method stub
TextView counter = (TextView)arg0.findViewById(R.id.counter);
int counter1 = Integer.parseInt(counter.getText().toString());
counter1++;
counter.setText(""+counter1);
}
});
minus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
TextView counter = (TextView)arg0.findViewById(R.id.counter);
int counter1 = Integer.parseInt(counter.getText().toString());
counter1--;
counter.setText(""+counter1);
}
});
System.out.println("in adapter 3");
itemName.setText(arr.get(arg0).getName());
itemDescription.setText(arr.get(arg0).getDescription());
itemImage.setImageResource(arr.get(arg0).getImage());
System.out.println("in adapter 4");
return arg1;
}
}

Related

trying to add a Custom ListView With an Adapter to a Fragment?

I'm trying to add a Custom ListView With an Adapter to a Fragment.
I don't Know where is the problem exactly, I think I got everything in place yet I cant get rid of the Unfortunately Stopped message.
I Used the debug and knew where it all collapses ;
in the Fragment class at the
for (int i = 0;i<4;i++){
.
.
.
questions.add(question);
when the debug reaches this statement it ends.
Fragment
import java.util.List;
import code.me.R.id;
import android.R.layout;
import android.app.Application.ActivityLifecycleCallbacks;
import android.app.Fragment;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
#SuppressLint("NewApi")
public class Fragment1 extends Fragment
{
ListView lv;
List<Question> questions;
Integer[] Q_Id ;
String[] AskerUserName = {"Jack" ,"John" ,"Lio" ,"Sam" ,"Mike" };
String[] AnswererUserName ={"Jacob" ,"Mario" ,"Tom" ,"Shon" ,"Jasmine" };
String[] Qusetion = {"What?" ,"Where?" ,"When" ,"How?" ,"Who?" };
String[] Answer = {"jjjjjjjjjj","llllllll","fffffffff","eeeeeeeeeeee","oooooooooooo"};
#Override
public View onCreateView( LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState )
{
for (int i = 0;i<4;i++){
int qid = i;
String AUName = AskerUserName[i].toString();
String AnUName = AnswererUserName[i].toString();
String Ans = Answer[i].toString();
String Ques = Qusetion[i].toString();
Question question = new Question(qid, AUName, AnUName, Ans, Ques);
questions.add(question);
}
View view = inflater.inflate(R.layout.frag1, container, false);
QuestionsAdapter questad = new QuestionsAdapter(getActivity(),questions);
lv = (ListView) view.findViewById(id.listView1);
lv.setAdapter(questad);
return inflater.inflate( R.layout.frag1,
container, false );
}
}
Adapter
import java.util.List;
import code.me.R.id;
import android.R.layout;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class QuestionsAdapter extends BaseAdapter {
private final Activity context;
public List<Question> questions;
LayoutInflater inflater;
public QuestionsAdapter(Activity context,List<Question> qu) {
this.context = context;
this.questions = qu;
// TODO Auto-generated constructor stub
}
#Override
public View getView(int position, View view , ViewGroup parent){
View rowView = view;
if(view == null)
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.question_answer_item, null,false);
TextView askerUserName = (TextView) view.findViewById(id.askerUsername);
askerUserName.setText(questions.get(position).getAskerUserName().toString());
TextView question = (TextView) view.findViewById(id.Question);
question.setText(questions.get(position).getQuestion().toString());
TextView ratingView = (TextView) view.findViewById(id.RatingAsker);
//ratingView.setText(questions.get(position)._RatingU);
TextView ratingAnswerer = (TextView) view.findViewById(id.RatingAnswerer);
//ratingAnswerer.setText(questions.get(position).getRatingView());
return rowView;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return questions.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return questions.get(position)._QId;
}
}
Class_Question
public class Question {
private
String _Answer ;
String _AnswererUserName;
String _AskerUserName ;
String _Question ;
Integer _QId;
Integer _RatingU;
Integer _RatingQ;
public Question (int Q_Id,String AUName,String AnUName,String Ans,String Ques){
_QId = Q_Id;
_AnswererUserName = AnUName;
_AskerUserName = AUName;
_Answer = Ans;
_Question = Ques;
}
public String getAnswer() {
// TODO Auto-generated method stub
return _Answer;
}
public String getAnswererUserName() {
// TODO Auto-generated method stub
return _AnswererUserName;
}
public String getAskerUserName() {
// TODO Auto-generated method stub
return _AskerUserName;
}
public String getQuestion() {
// TODO Auto-generated method stub
return _Question;
}
public int getRatingView() {
// TODO Auto-generated method stub
return _RatingU;
}
public void setRatingQ() {
// TODO Auto-generated method stub
}
public int getRatingQ() {
// TODO Auto-generated method stub
return _RatingQ;
}
}
You never initialize the List<Question> questions in your fragment.
You never initialize your question collection. That's why it stops when you try to add questions in onCreateView. Either initialize List questions initially or some time before you try to add things to it.

save state of checkbox

i havetwo activty in which activity b has the contact details when user select the checkbox,data is stored in the string in the activity a,for more selction when user goes back to activity b,previously selected contacts are shown checked,that means their state ramain saved,how to do that
MainActivity.java
package com.example.smscampaign;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.ActionBar;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Parcelable;
import android.provider.ContactsContract;
import android.telephony.gsm.SmsManager;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity implements OnItemClickListener {
ArrayList<String> name1 = new ArrayList<String>();
ArrayList<String> phno1 = new ArrayList<String>();
ArrayList<String> phno0 = new ArrayList<String>();
MyAdapter ma;
Button send;
ListView lv ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.get);
TextView txt=(TextView) findViewById(R.id.textView1);
getAllCallLogs(this.getContentResolver());
lv = (ListView) findViewById(R.id.lv);
ma = new MyAdapter();
lv.setAdapter(ma);
lv.setOnItemClickListener(this);
lv.setItemsCanFocus(false);
lv.setTextFilterEnabled(true);
}
/* #Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
// Save ListView state
Parcelable state = lv.onSaveInstanceState();
// Set new items
lv.setAdapter(Baseadapter);
// Restore previous state (including selected item index and scroll position)
lv.onRestoreInstanceState(state);
}*/
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.contact_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.addPage:
break;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
StringBuilder checkedcontacts = new StringBuilder();
System.out.println( ma.mCheckStates.size());
for (int i = 0; i < name1.size(); i++)
{
if (ma.mCheckStates.get(i) == true) {
phno0.add(phno1.get(i).toString());
checkedcontacts.append(name1.get(i).toString());
checkedcontacts.append("\n");
} else {
System.out.println("..Not Checked......"
+ name1.get(i).toString());
}
}
//Toast.makeText(MainActivity.this, checkedcontacts, 1000).show();
Intent returnIntent = new Intent();
returnIntent.putStringArrayListExtra("name", phno0);
setResult(RESULT_OK, returnIntent);
finish();
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
ma.toggle(arg2);
}
public void getAllCallLogs(ContentResolver cr) {
Cursor phones = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
while (phones.moveToNext()) {
String name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println( phoneNumber);
name1.add(name);
phno1.add(phoneNumber);
}
phones.close();
}
class MyAdapter extends BaseAdapter implements
CompoundButton.OnCheckedChangeListener {
private SparseBooleanArray mCheckStates;
LayoutInflater mInflater;
TextView tv1, tv;
CheckBox cb;
MyAdapter() {
mCheckStates = new SparseBooleanArray(name1.size());
mInflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
// Save ListView state
#Override
public int getCount() {
// TODO Auto-generated method stub
return name1.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (convertView == null)
vi = mInflater.inflate(R.layout.row, null);
tv = (TextView) vi.findViewById(R.id.textView1);
tv1 = (TextView) vi.findViewById(R.id.textView2);
cb = (CheckBox) vi.findViewById(R.id.checkBox1);
tv.setText(name1.get(position));
tv1.setText( phno1.get(position));
cb.setTag(position);
cb.setChecked(mCheckStates.get(position, false));
cb.setOnCheckedChangeListener(this);
return vi;
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
}
}
Pass String In Intent.
try this Code For Ex:
You need to pass it as an Intent
ArrayList<String> mylist = new ArrayList<String>();
Intent intent = new Intent(ActivityName.this, Second.class);
intent.putStringArrayListExtra("key", mylist);
startActivity(intent);
Then extract it from your new activity like this:
Intent i = getIntent();
ArrayList<String> list = i.getStringArrayListExtra("key");
hope this helps you.

Not able to see text in ListView

I am using ListActivity to display simple text with SimpleCursorAdapter to get the texts from my database. I have tried to debug the problem and found that the cursor is successfully fetching the result but the text is not displayed in the ListView.
Following is the ListActivity code I am using.
public class MyListActivity extends ListActivity{
Cursor myCursor;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
myCursor = getData();
startManagingCursor(myCursor);
ListAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.display_details, myCursor, new String[]{MyDbHelper.ID_FIELD, MyDbHelper.NAME_FIELD}, new int[]{R.id.row_id, R.id.row_name});
setListAdapter(myAdapter);
}
private Cursor getData() {
return MyDBHelper.getRoutes();
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
routesCursor.close();
}
}
and following is my xml file for each row in ListView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/row_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/row_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/row_id"
android:layout_toRightOf="#+id/row_id"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
and my layout for ListActivity is as following
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView android:id="#+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent">
</ListView>
</RelativeLayout>
Please provide me the solution. Thanks in advance.
This code can help your problem.
package com.example.getkontakdata;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnItemClickListener {
List<String> name1 = new ArrayList<String>();
List<String> phno1 = new ArrayList<String>();
MyAdapter ma;
Button select;
CheckBox cb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getAllCallLogs(this.getContentResolver());
final ListView lv= (ListView) findViewById(R.id.lv);
cb = (CheckBox) findViewById(R.id.checkBox1);
ma = new MyAdapter();
lv.setAdapter((ListAdapter) ma);
lv.setOnItemClickListener(this);
lv.setItemsCanFocus(false);
lv.setTextFilterEnabled(true);
// adding
select = (Button) findViewById(R.id.button1);
select.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
StringBuilder checkedcontacts= new StringBuilder();
System.out.println(".............." + ma.mCheckStates.size());
for(int i = 0; i < phno1.size(); i++) {
if(ma.mCheckStates.get(i) == true) {
checkedcontacts.append(phno1.get(i).toString());
checkedcontacts.append(";");
} else {
System.out.println(".. Not Checked ......" + phno1.get(i).toString());
}
}
}
});
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
ma.toggle(arg2);
}
public void getAllCallLogs(ContentResolver cr) {
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext()) {
String name=phones.getString
(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString
(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//System.out.println(".................." + phoneNumber);
name1.add(name);
phno1.add(phoneNumber);
}
phones.close();
}
class MyAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener {
private SparseBooleanArray mCheckStates;
//private SparseBooleanArray nCheckStates;
LayoutInflater mInflater;
TextView tv1,tv;
CheckBox cb;
MyAdapter() {
mCheckStates = new SparseBooleanArray(name1.size());
mCheckStates = new SparseBooleanArray(phno1.size());
mInflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public String get(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return name1.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = mInflater.inflate(R.layout.row, null);
TextView tv= (TextView) vi.findViewById(R.id.textView1);
tv1= (TextView) vi.findViewById(R.id.textView2);
cb = (CheckBox) vi.findViewById(R.id.checkBox1);
tv.setText("Nama : "+ name1.get(position));
tv1.setText("Nomor Kontak : "+ phno1.get(position));
cb.setTag(position);
cb.setChecked(mCheckStates.get(position, false));
cb.setOnCheckedChangeListener((OnCheckedChangeListener) this);
return vi;
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
System.out.println("hello...........");
notifyDataSetChanged();
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
}
}

getting problem to add items in listview from spinner item selection on button click

Hello friends i m getting problem to add items in listview from spinner item selection on button. Here i m providing code please help me to get rid of the problem.
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Adapter;
public class DefectlistviewActivity extends ListActivity {
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
/** Called when the activity is first created. */
ListView defectDisplay;
Spinner spinner;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinner = (Spinner)this.findViewById(R.id.Defect);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.defects_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
// defectDisplay = (ListView) findViewById(R.id.list);
Button add = (Button) findViewById(R.id.Add_new_Defect_Button);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent("com.android.HCS.AddDialog"));
}
});
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
private class EfficientAdapter extends BaseAdapter {
TextView tv;
// private LayoutInflater mInflater;
Context context;
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View row, ViewGroup convertView) {
// TODO Auto-generated method stub
LayoutInflater mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// mInflater = LayoutInflater.from(context);
if (null == convertView) {
row = mInflater.inflate(R.layout.listview, null);
} else {
row = convertView;
}
tv = (TextView) row.findViewById(R.id.Defect1);
//tv.setText(getItem(position));
String itemVal = (String)getItem(position);
tv.setText(itemVal);
return row;
}
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(final AdapterView<?> parent,
final View view, final int pos, long id) {
Button add_defect = (Button) findViewById(R.id.Add_Defect_Button);
add_defect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(parent.getContext(),
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
setListAdapter(new EfficientAdapter());
}
});
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
}
Your getCount() function in EfficientAdapter is returning 0. So you wont get any callback to getView(). Also getItem(position) is returning null which you trying to set to textview in getView funcion of EfficientAdapter .
I guess you should have a list set to EfficientAdapter who's size should be returned in getCount() and on spinner operation you add your addtional data to the list and notify your list adaptor ie..,EfficientAdapter

how to display the next images by clicking on the "next" button

I am trying to display the image by clicking on the "next" button but this code doesn't work.
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class MyGridView extends Activity {
GridView mGridView;
int counter,i;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.mygridacti);
mGridView = (GridView)findViewById(R.id.grdvw);
//mGridView.setAdapter(new MyImageAdapter(this));
ImageView imageview = (ImageView)findViewById(R.id.GalleryView);
Button btnnext = (Button)findViewById(R.id.btn_nxt);
Log.i("counter", ""+counter);
btnnext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//for(i=0;i<=5;i++){
mGridView.setAdapter(new MyImageAdapter(getApplicationContext()));
Log.i("counter", ""+counter);
//}
// TODO Auto-generated method stub
//mGridView.setAdapter(new MyImageAdapter(getApplicationContext()));
//Toast.makeText(MyGridView.this, "No Images", Toast.LENGTH_SHORT).show();
}
}
);
}
}
class MyImageAdapter extends BaseAdapter{
private Context mContext;
ImageView imageview;
int counter;
public MyImageAdapter(Context c){
mContext = c;
}
public MyImageAdapter(OnClickListener onClickListener) {
// TODO Auto-generated constructor stub
}
#Override
public int getCount() {
// TODO Auto-for(i = 6;i<=counter;i++) {generated method stub
//counter = mThumbsIds.length;
//System.out.println("Counter = "+counter);
return 5;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
imageview = new ImageView(mContext);
imageview.setLayoutParams(new GridView.LayoutParams(85,85));
imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageview.setPadding(8, 8, 8, 8);
}
else{
imageview = (ImageView) convertView;
}
//int[] mThumbIds = null;
imageview.setImageResource(mThumbsIds[position]);
return imageview;
//return null;
}
private Integer[] mThumbsIds = {
R.drawable.image1,R.drawable.image2,
R.drawable.image3,R.drawable.image4,
R.drawable.image5,R.drawable.icon,
R.drawable.icon,R.drawable.icon,
R.drawable.icon};
}
Please help me out to correct this.
To refresh grid view see this search result

Categories

Resources