I am trying to update listView from another fragment class after i delete an item from listView from current class. For example, i delete a consumer that has an expense, then the expense of that consumer will also be deleted. But consumer and expense is different page that using tab control, which is fragment, when i delete consumer, the expense list is not updated, until i go to previous page, then enter the page again. What can i do to update the expense list when consumer from consumer list is deleted? i heard of using broadcast receiver is able to do that, but how can i implement it in my code? I am new in android, please guide me.Thanks
Here is the code for consumer/participant list :
public class Participant extends ListFragment implements OnClickListener{
Intent intent;
TextView friendId;
Button addparticipant;
String eventId;
ListView lv;
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
EventController controller = new EventController(getActivity());
HashMap<String, String> queryValues = new HashMap<String, String>();
Intent objIntent = getActivity().getIntent();
eventId = objIntent.getStringExtra("eventId");
queryValues.put("eventId", eventId);
ArrayList<HashMap<String, String>> friendList = controller
.getAllFriends(queryValues);
if (friendList.size() != 0) {
lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
friendId = (TextView) view.findViewById(R.id.friendId);
String valFriendId = friendId.getText().toString();
Intent objIndent = new Intent(getActivity(),
EventPage.class);
objIndent.putExtra("friendId", valFriendId);
startActivity(objIndent);
}
});
lv.setOnItemLongClickListener(new OnItemLongClickListener(){
#Override
public boolean onItemLongClick(AdapterView<?> arg0,
View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
friendId = (TextView) arg1.findViewById(R.id.friendId);
registerForContextMenu(getListView());
return false;
}
});
SimpleAdapter adapter = new SimpleAdapter(getActivity(),
friendList, R.layout.view_friend_entry, new String[] {
"friendId", "friendName", "friendSpending" },
new int[] { R.id.friendId, R.id.friendName,
R.id.friendSpending });
adapter.notifyDataSetChanged();
setListAdapter(adapter);
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.participant, container, false);
addparticipant = (Button) rootView.findViewById(R.id.addpart);
addparticipant.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent objIntent = new Intent(getActivity(),
AddParticipant.class);
objIntent.putExtra("eventId", eventId);
startActivity(objIntent);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
String[] menuItems = getResources().getStringArray(R.array.menu);
for (int i = 0; i < menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
int menuItemIndex = item.getItemId();
EventController controller = new EventController(getActivity());
switch (menuItemIndex) {
case 0:
String valFriendId = friendId.getText().toString();
Intent objIndent = new Intent(getActivity(),
EditParticipant.class);
objIndent.putExtra("friendId", valFriendId);
startActivity(objIndent);
break;
case 1:
String valFriendId2 = friendId.getText().toString();
controller.deleteFriend(valFriendId2);
onResume();
}
return true;
}
#Override
public void onResume() {
super.onResume();
if (getListView() != null) {
updateData();
}
}
private void updateData() {
EventController controller = new EventController(getActivity());
HashMap<String, String> queryValues = new HashMap<String, String>();
Intent objIntent = getActivity().getIntent();
eventId = objIntent.getStringExtra("eventId");
queryValues.put("eventId", eventId);
SimpleAdapter adapter = new SimpleAdapter(getActivity(),
controller.getAllFriends(queryValues),
R.layout.view_friend_entry, new String[] { "friendId",
"friendName", "friendSpending" }, new int[] {
R.id.friendId, R.id.friendName, R.id.friendSpending });
setListAdapter(adapter);
}
}
Here is the code for expense list:
public class Expense extends ListFragment implements OnClickListener {
Button addexp;
TextView expenseId;
ListView lv;
String eventId, friendId;
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
EventController controller = new EventController(getActivity());
HashMap<String, String> queryValues = new HashMap<String, String>();
Intent objIntent = getActivity().getIntent();
eventId = objIntent.getStringExtra("eventId");
queryValues.put("eventId", eventId);
ArrayList<HashMap<String, String>> expenseList = controller
.getAllExpenses(queryValues);
if (expenseList.size() != 0) {
lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
expenseId = (TextView) view.findViewById(R.id.expenseId);
String valExpenseId = expenseId.getText().toString();
Intent objIndent = new Intent(getActivity(),
EditExpense.class);
objIndent.putExtra("expenseId", valExpenseId);
startActivity(objIndent);
}
});
SimpleAdapter adapter = new SimpleAdapter(getActivity(),
expenseList, R.layout.view_expense_entry, new String[] {
"expenseId", "expenseName","expenseQuantity" }, new int[] {
R.id.expenseId, R.id.expenseName, R.id.expenseQuantity });
adapter.notifyDataSetChanged();
setListAdapter(adapter);
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.expense, container, false);
addexp = (Button) rootView.findViewById(R.id.addexp);
addexp.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent objIntent = new Intent(getActivity(), AddExpense.class);
objIntent.putExtra("eventId", eventId);
startActivity(objIntent);
}
#Override
public void onResume() {
super.onResume();
if (getListView() != null) {
updateData();
}
}
private void updateData() {
EventController controller = new EventController(getActivity());
HashMap<String, String> queryValues = new HashMap<String, String>();
Intent objIntent = getActivity().getIntent();
eventId = objIntent.getStringExtra("eventId");
queryValues.put("eventId", eventId);
SimpleAdapter adapter = new SimpleAdapter(getActivity(),
controller.getAllExpenses(queryValues),
R.layout.view_expense_entry, new String[] { "expenseId",
"expenseName", "expenseQuantity" }, new int[] { R.id.expenseId,
R.id.expenseName, R.id.expenseQuantity });
setListAdapter(adapter);
}
}
Same scenario with me, if you used FragmentPagerAdapter, Try like this in FirstFragment
private void updateSecondFragment(){
//Way to get TagName which generated by FragmentPagerAdapter
String tagName = "android:switcher:" + R.id.pager + ":" + 1; // Your pager name & tab no of Second Fragment
//Get SecondFragment object from FirstFragment
SecondFragment f2 = (SecondFragment)getActivity().getSupportFragmentManager().findFragmentByTag(tagName);
//Then call your wish method from SecondFragment to update appropriate list
f2.updateList();
}
I got that idea by reading this. Then edit a little !
One way could be creating the methods in the activity that contains the two fragments, and access to the fragments methods from the activity. You can update the database and reload the list in the same method.
For example, In the activity
public void updateFragment2(){ ... }
In fragment 1 you should call:
((ActivityClass)getActivity()).updateFragment2();
In the section "Communicating with the Activity" you can see the explanation of this example:
http://developer.android.com/guide/components/fragments.html
Related
i'm working on a student record project And there are four fields in it like class name roll no. marks and User is putting data into it by edit text
and i'm storing the values into the json Array . Also i'm putting values in array list and i'm passing to my fragment class .
New record class
public class newrecord extends Activity {
public final static String TAG = "SomeValue";
Button but;
EditText roll, name, marks, Class;
TextView text;
String rollno, nameno, classno, marksno;
TopRatedFragment frags;
public static final String ROLL_key = "roll number";
public static final String NAME_key = "Name";
public static final String Class_key = "Class";
public static final String MARKS_key = "marks";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newrecord);
final newrecord rd = new newrecord();
// data = new dbbase(getApplicationContext());
text = (TextView) findViewById(R.id.textviewenter);
roll = (EditText) findViewById(R.id.editRollno);
name = (EditText) findViewById(R.id.editName);
marks = (EditText) findViewById(R.id.editMarks);
Class = (EditText) findViewById(R.id.edittextclass);
but = (Button) findViewById(R.id.btnadd);
frags = new TopRatedFragment();
final JSONArray ary = new JSONArray();
final JSONObject obj = new JSONObject();
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View V) {
try {
obj.put("Roll No", roll.getText().toString());
obj.put("name", name.getText().toString());
obj.put("marks", marks.getText().toString());
obj.put("Class", Class.getText().toString());
ary.put(obj);
rd.nameno = name.getText().toString();
rd.rollno = roll.getText().toString();
rd.marksno = marks.getText().toString();
rd.classno = Class.getText().toString();
Log.d(TAG, "Values Inserted");
} catch (Exception e) {
text.setText("error");
}
ArrayList<String> list_list = new ArrayList<String>();
list_list.add(rollno);
list_list.add(nameno);
list_list.add(classno);
list_list.add(marksno);
Intent i = new Intent(newrecord.this, MainActivity.class);
// Bundle b = new Bundle();
i.putStringArrayListExtra("fite", list_list);
startActivity(i);
}
});
}
public ArrayList<HashMap<String, String>> Getalldata() {
ArrayList<HashMap<String, String>> dude;
dude = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put(ROLL_key, rollno);
map.put(NAME_key, nameno);
map.put(Class_key, classno);
map.put(MARKS_key, marksno);
dude.add(map);
return dude;
}
}
And i want to return list in the listview in my fragment .
As in the values that i add in edittexts should appear in the list in my fragment class.
My topfragment class
public class TopRatedFragment extends Fragment {
ListView List;
ListAdapter mAdapter;
int nr = 0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_top_rated,
container, false);
Intent in = new Intent();
newrecord record = new newrecord();
String tim = in.getStringExtra("fite");
ArrayList<String> liist = new ArrayList<String>();
if (liist.size() == 0) {
liist.add("Template1");
} else {
liist=in.getStringArrayListExtra("fite")
}
List = (ListView) rootView.findViewById(R.id.listView1);
List.setAdapter(mAdapter);
mAdapter = new SelectionAdapter(getActivity(), R.layout.simplerow,
R.id.rowTextView, liist);
List.setAdapter(mAdapter);
List.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
// TODO Auto-generated method stub
((BaseAdapter) mAdapter).notifyDataSetChanged();
}
});
Try this out:
In NewRecord class
Intent menulist = new Intent(this, cart_activity.class);
menulist.putExtra("cartlist", cart_Array_List);
startActivity(menulist);
In your fragment:
intent = getIntent();
if (intent != null) {
arr_cart_items = (ArrayList<HashMap<String, String>>) intent
.getSerializableExtra("cartlist");
}
Make sure your ArrayList is not empty.
And one common mistake that I used to do was to set text color as white, so the contents of the listview was not actually visible. So please check that too.
Change this line
Intent in = new Intent();
To
Intent in = getActivity().getIntent();
I hope this one will help you :)
I am trying to update listView from another fragment class after i delete an item from listView from current class. For example, i delete a consumer that has an expense, then the expense of that consumer will also be deleted.
But consumer and expense is different page that using tab control, which is fragment, when i delete consumer, the expense list is not updated, until i go to previous activity, then enter the page again. What can i do to update the expense list when consumer from consumer list is deleted? Thanks.
Here is the code for consumer/participant list :
public class Participant extends ListFragment implements OnClickListener{
Intent intent;
TextView friendId;
Button addparticipant;
String eventId;
ListView lv;
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
EventController controller = new EventController(getActivity());
HashMap<String, String> queryValues = new HashMap<String, String>();
Intent objIntent = getActivity().getIntent();
eventId = objIntent.getStringExtra("eventId");
queryValues.put("eventId", eventId);
ArrayList<HashMap<String, String>> friendList = controller
.getAllFriends(queryValues);
if (friendList.size() != 0) {
lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
friendId = (TextView) view.findViewById(R.id.friendId);
String valFriendId = friendId.getText().toString();
Intent objIndent = new Intent(getActivity(),
EventPage.class);
objIndent.putExtra("friendId", valFriendId);
startActivity(objIndent);
}
});
lv.setOnItemLongClickListener(new OnItemLongClickListener(){
#Override
public boolean onItemLongClick(AdapterView<?> arg0,
View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
friendId = (TextView) arg1.findViewById(R.id.friendId);
registerForContextMenu(getListView());
return false;
}
});
SimpleAdapter adapter = new SimpleAdapter(getActivity(),
friendList, R.layout.view_friend_entry, new String[] {
"friendId", "friendName", "friendSpending" },
new int[] { R.id.friendId, R.id.friendName,
R.id.friendSpending });
adapter.notifyDataSetChanged();
setListAdapter(adapter);
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.participant, container, false);
addparticipant = (Button) rootView.findViewById(R.id.addpart);
addparticipant.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent objIntent = new Intent(getActivity(),
AddParticipant.class);
objIntent.putExtra("eventId", eventId);
startActivity(objIntent);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
String[] menuItems = getResources().getStringArray(R.array.menu);
for (int i = 0; i < menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
int menuItemIndex = item.getItemId();
EventController controller = new EventController(getActivity());
switch (menuItemIndex) {
case 0:
String valFriendId = friendId.getText().toString();
Intent objIndent = new Intent(getActivity(),
EditParticipant.class);
objIndent.putExtra("friendId", valFriendId);
startActivity(objIndent);
break;
case 1:
String valFriendId2 = friendId.getText().toString();
controller.deleteFriend(valFriendId2);
onResume();
}
return true;
}
#Override
public void onResume() {
super.onResume();
if (getListView() != null) {
updateData();
}
}
private void updateData() {
EventController controller = new EventController(getActivity());
HashMap<String, String> queryValues = new HashMap<String, String>();
Intent objIntent = getActivity().getIntent();
eventId = objIntent.getStringExtra("eventId");
queryValues.put("eventId", eventId);
SimpleAdapter adapter = new SimpleAdapter(getActivity(),
controller.getAllFriends(queryValues),
R.layout.view_friend_entry, new String[] { "friendId",
"friendName", "friendSpending" }, new int[] {
R.id.friendId, R.id.friendName, R.id.friendSpending });
setListAdapter(adapter);
}
}
Using Broadcasts:
Create this class in your list fragment:
public class UpdateReceiver extends BroadcastReceiver {
public static final String NEW_UPDATE_BROADCAST =
"your.package.NEW_UPDATE_BROADCAST";
#Override
public void onReceive(Context context, Intent intent) {
//Update the list
}
}
Declare this in the fragment:
private UpdateReceiver updateReceiver = null;
Use this to register the receiver (when you create the fragment):
IntentFilter filter = new IntentFilter(UpdateReceiver.NEW_UPDATE_BROADCAST);
filter.addCategory(Intent.CATEGORY_DEFAULT);
updateReceiver = new UpdateReceiver();
registerReceiver(updateReceiver, filter);
Use this to unregister(when you destroy the fragment):
if (updateReceiver != null){
unregisterReceiver(updateReceiver);
updateReceiver = null;
}
Then when you want to request the update you send the broadcast like this:
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(UpdateReceiver.NEW_UPDATE_BROADCAST);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
sendBroadcast(broadcastIntent);
You can even send data in the bradcast intent if you need and receive it in the onReceive method...
I'm trying to call another Fragment when I click on list item but it doesn't work, I'm using slider menu and the items of ListView are options inside the slider menu. What am I doing wrong?
public class MenuNetimoveis_Fragments extends ListFragment {
private ListView listView;
String[] itensMenu = new String[] {
" Imóveis no mapa",
"Favoritos",
"Agências",
"Dicas para Alugar",
"Alugar meu imóvel",
"Sobre"
};
int[] imagens = new int[]{
R.drawable.map_marker,
R.drawable.star,
R.drawable.house,
R.drawable.coffee,
R.drawable.pricetag,
R.drawable.pricetag
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.list, null);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ImageView tv = new ImageView(getActivity());
Drawable dw = getResources().getDrawable(R.drawable.menu_netimoveis_locacao);
tv.setImageDrawable(dw);
getListView().addHeaderView(tv);
crateListView();
}
private void crateListView(){
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<6;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", itensMenu[i]);
hm.put("imagens", Integer.toString(imagens[i]) );
aList.add(hm);
}
String[] from = { "txt","imagens" };
int[] to = { R.id.text,R.id.imagemview};
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.list_row, from, to);
setListAdapter(adapter);
}
#Override
public void onListItemClick(ListView lv, View v, int position, long id) {
Fragment newContent = null;
switch (position) {
case 0:
newContent = new Menu_DicasParaAlugar();
break;
case 1:
break;
}
if (newContent != null)
switchFragment(newContent);
}
// the meat of switching the above fragment
private void switchFragment(Fragment fragment) {
if (getActivity() == null)
return;
if (getActivity() instanceof FragmentChangeActivity) {
FragmentChangeActivity fca = (FragmentChangeActivity) getActivity();
fca.switchContent(fragment);
}
}
}
do a getListView().setOnListItemClickListener(this); at the end of onCreate()
I have a problem with my listview that when I add a task to my database, I need to update my listview with this new added task....
I'm new to android and Eclipse...
Here is my code for Main (that shows the ListView)
public class Main extends ListActivity {
Button newCat;
TextView todaytaskId;
ResultDatabase controller5 = new ResultDatabase(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
newCat = (Button) findViewById(R.id.bNewCat);
newCat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent nextScreen = new Intent(getApplicationContext(),
CategoryList.class);
startActivity(nextScreen);
}
});
ArrayList<HashMap<String, String>> todaytaskList = controller5
.getTodayTasks();
if (todaytaskList.size() != 0) {
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
todaytaskId = (TextView) view
.findViewById(R.id.todaytaskId);
String valtaskId = todaytaskId.getText().toString();
Intent objIndent = new Intent(getApplicationContext(),
DelayTask.class);
objIndent.putExtra("todaytaskId", valtaskId);
startActivity(objIndent);
}
});
SimpleAdapter adapter = new SimpleAdapter(Main.this, todaytaskList,
R.layout.view_today_task, new String[] { "taskId",
"taskName", "taskTime" }, new int[] {
R.id.todaytaskId, R.id.todaytasktv,
R.id.todaytasktimetv});
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}
Here's my code to select tasks from ResultDatabase class
public ArrayList<HashMap<String, String>> getTodayTasks() {
ArrayList<HashMap<String, String>> wordList;
wordList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM tasks where taskDate = date('now') AND taskDone = 'No'";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("taskId", cursor.getString(0));
map.put("taskName", cursor.getString(1));
map.put("taskTime", cursor.getString(3));
// map.put("taskDate", cursor.getString(4));
wordList.add(map);
} while (cursor.moveToNext());
}
return wordList;
}
After modifying the data set that is connected on the adapter you must call notifyDataSetChanged to notify the adapter to update the views with the new data. You must also call requery on the Cursor to update the cursor with new data. To clarify you should call requery before notifyDataSetChanged.
Check sample code below
public class WeatherAppActivity extends ListActivity {
Button buton;
ItemsAdapter lista;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
List<String> initialList = new ArrayList<String>();
initialList.add("Bucuresti");
initialList.add("Sibiu");
lista=new ItemsAdapter(this, initialList);
buton=(Button)findViewById(R.id.button1);
buton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
lista.add(""+System.currentTimeMillis()); // e chiar getText()
lista.notifyDataSetChanged();
}
});
setListAdapter(lista);
}
class ItemsAdapter extends ArrayAdapter<String> {
public ItemsAdapter(Context context, List<String> list) {
super(context, R.layout.lista, list);
}
#Override
public View getView(final int position, View row, final ViewGroup parent) {
final String item = getItem(position);
ItemWrapper wrapper = null;
if (row == null) {
row = getLayoutInflater().inflate(R.layout.lista, parent, false);
wrapper = new ItemWrapper(row);
row.setTag(wrapper);
} else {
wrapper = (ItemWrapper) row.getTag();
}
wrapper.refreshData(item);
return row;
}
class ItemWrapper {
TextView text;
public ItemWrapper(View row) {
text = (TextView) row.findViewById(R.id.elementLista);
}
public void refreshData(String item) {
text.setText(item);
}
}
}
}
I have been trying to get my listview to change background colour during oncreate but have had no luck.
To do this I save either 1 or 0 in the database depending on if it was clicked and colour the rows accordingly. However my rows do not change colour during oncreate.
I have supplied my oncreate and arrayadaptor extends.
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.item_list);
// Read var from Intent
Intent intent= getIntent();
final String ListID = intent.getStringExtra("ListID");
golbalItemID = ListID;
ivAdd = (ImageView) findViewById(R.id.ivAdd);
ivCancel = (ImageView) findViewById(R.id.ivCancel);
tvTotItems = (TextView) findViewById(R.id.tvTotItems);
final myDBClass myDb = new myDBClass(this);
final ArrayList<HashMap<String, String>> MebmerList = myDb.SelectAllItemData(ListID);
myData = myDb.SelectItemData(Integer.parseInt(ListID.toString()));
// listView1
final ListView lisView1 = (ListView)findViewById(R.id.listView1);
registerForContextMenu(lisView1);
MyAdapter sAdap;
sAdap = new MyAdapter(ListItems.this, MebmerList, R.layout.activity_column,
new String[] {"Name", "Price", "Quan"}, new int[] {R.id.ColName, R.id.ColTel, R.id.ColQuan});
lisView1.setAdapter(sAdap);
lisView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int position, long mylng) {
int iChk = 0;
// Show Data
String arrData[] = myDb.SelectItemData((MebmerList.get(position).get("ItemID").toString()));
if(arrData != null)
{
iChk = Integer.parseInt(arrData[4]);
}
if(iChk == 1)
{
ischkCheck(Integer.parseInt(MebmerList.get(position).get("ItemID").toString()), 0);
change_color(lisView1, position, 255, 255, 255);
System.out.println("POSITION!ichk=1" + myAdapter.getItemAtPosition(position).toString());
setTitle(myAdapter.getItemAtPosition(position).toString());
}
else if(iChk == 0)
{
ischkCheck(Integer.parseInt(MebmerList.get(position).get("ItemID").toString()), 1);
change_color(lisView1, position, 155, 155, 138);
System.out.println("POSITION!ichk=0" + myAdapter.getItemAtPosition(position).toString());
}
}});
ivAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent newActivity = new Intent(ListItems.this,AddItem.class);
newActivity.putExtra("ListID", ListID);
startActivity(newActivity);
finish();
}
});
ivCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent newActivity = new Intent(ListItems.this,MenuScreen.class);
startActivity(newActivity);
finish();
}
});
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(ListItems listItems,ArrayList<HashMap<String, String>> mebmerList,int activityColumn, String[] strings, int[] is) {
super(listItems, activityColumn, strings);
// TODO Auto-generated constructor stub
System.out.println("POSITION!ichk=1" + "FIRST!");
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView( position, convertView, parent );
view.setBackgroundColor( 0xff00ff00 );
System.out.println("POSITION!ichk=1" + "MYADGETVIEWP");
return view;
}
}