I have a little problem sending the right id to another activity as extra.I'm getting id from database which I'm sending to the new activity so I can get the right data. But the problem is that my code is sending only the last id,which is on arraylist. How can I fix that?
Here is the code I'm using :
for(cursorTitle.move(0); cursorTitle.moveToNext(); cursorTitle.isAfterLast()) {
if (vf != null) {
vf.removeAllViews();
}
text = cursorTitle.getString(cursorTitle.getColumnIndex("title"));
cardsCount = cursorTitle.getString(cursorTitle.getColumnIndex("cardsCount"));
collId = Integer.parseInt(cursorTitle.getString(cursorTitle.getColumnIndex("objectId")));
Log.i("CollID","Collection ID : "+collId);
b = BitmapFactory.decodeFile("/sdcard/7073d92dce10884554d7e047f1c51cb6.jpg", null);
array = new ArrayList<Integer>();
array.add(collId);
vf = new ViewFlipper(MyCollectionList.this);
myListView = new ListView(MyCollectionList.this);
hm = new HashMap<String, Object>();
hm.put(IMAGE, b);
hm.put(TITLE, text);
hm.put(CARDS_COUNT, cardsCount +" Stampii");
items.add(hm);
final SimpleAdapter adapter = new SimpleAdapter(MyCollectionList.this, items, R.layout.main_listview,
new String[]{TITLE, CARDS_COUNT, IMAGE}, new int[]{ R.id.main_name, R.id.main_info, R.id.main_img});
myListView.setAdapter(adapter);
myListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
Intent previewMessage = new Intent(getParent(), MyCollectionId.class);
previewMessage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
previewMessage.putExtra("collection_id", array.get(position));
parentActivity.startChildActivity("MyCollectionId", previewMessage);
}
});
}
Thanks in advance!
You're creating a new ArrayList in each iteration and then assigning only a single value to it. I don't think that is what you intended. You should instantiate the ArrayList outside of your loop. You'll also want to move all of your other one-time initialization code (ListView, SimpleAdapter, etc.) out of the loop.
What i got is,you need to populate a listview with the records you get in cursor.You are populating your listview in each iteration of for loop and that is what causes problem.i think,following should work for you. (Required changes for varibles should be made accordingly.)
Try this:
vf = new ViewFlipper(MyCollectionList.this);
myListView = new ListView(MyCollectionList.this);
array = new ArrayList<Integer>();
hm = new HashMap<String, Object>();
for(cursorTitle.move(0); cursorTitle.moveToNext(); cursorTitle.isAfterLast()) {
if (vf != null) {
vf.removeAllViews();
}
text = cursorTitle.getString(cursorTitle.getColumnIndex("title"));
cardsCount = cursorTitle.getString(cursorTitle.getColumnIndex("cardsCount"));
collId = Integer.parseInt(cursorTitle.getString(cursorTitle.getColumnIndex("objectId")));
Log.i("CollID","Collection ID : "+collId);
b = BitmapFactory.decodeFile("/sdcard/7073d92dce10884554d7e047f1c51cb6.jpg", null);
array.add(collId);
hm.put(IMAGE, b);
hm.put(TITLE, text);
hm.put(CARDS_COUNT, cardsCount +" Stampii");
items.add(hm);
}
final SimpleAdapter adapter = new SimpleAdapter(MyCollectionList.this, items, R.layout.main_listview,
new String[]{TITLE, CARDS_COUNT, IMAGE}, new int[]{ R.id.main_name, R.id.main_info, R.id.main_img});
myListView.setAdapter(adapter);
myListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
Intent previewMessage = new Intent(getParent(), MyCollectionId.class);
previewMessage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
previewMessage.putExtra("collection_id", array.get(position));
parentActivity.startChildActivity("MyCollectionId", previewMessage);
}
Put array = new ArrayList<Integer>(); outside of for loop.
like,
array = new ArrayList<Integer>();
for(cursorTitle.move(0); cursorTitle.moveToNext(); cursorTitle.isAfterLast()) {
{
// your stuff
array.add(collId);
// your stuff
}
Related
Hi I am new to the android & doing some simple examples. Exactly, i'm using two activities & it contains a list view on both. In that first list view (Activity1) is empty, the second list view(Activity2) has some list item with a checkbox. Now getting the selected item from the 2nd activity & listed in the first activity. It's fine but exactly what i want is again click 'add' button from the 1st activity it launches the second activity then select some item & save. This newly selected item not adding to the existing list value used in 1st activity. Can anybody help me out.
SecondActivity.java
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
checkedContactList = adapter.getSelectedItemArrayList();
}
Intent returnIntent = new Intent(v.getContext(),MainActivity.class);
returnIntent.putParcelableArrayListExtra("result", checkedContactList);
startActivity(returnIntent);
}
});
In MainActivity.java
private ArrayList<SelectedListModel> selectedArrayList = new ArrayList<SelectedListModel>();
List<Map<String, String>> dataList;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dataList = new ArrayList<Map<String, String>>();
Bundle data = getIntent().getExtras();
selectedArrayList = data.getParcelableArrayList("result");
Iterator<ArrayList<SelectedListModel>> it = selectedArrayList.iterator();
while (it.hasNext()){SelectedListModel selectedModel = (SelectedListModel)it.next();
SelectedListModel selectedModel = (SelectedListModel)it1.next();
String name1 = selectedModel.getName();
String number1 = selectedModel.getNumber();
Map<String, String> dict = new HashMap<String, String>(2);
dict.put("name", name1);
dict.put("number", number1);
dataList.add(dict);
//listdata.add(dataList);
}
SimpleAdapter adpt = new SimpleAdapter(this, dataList, android.R.layout.two_line_list_item, new String[] {"name", "number"}, new int[] {android.R.id.text1, android.R.id.text2});
selectedList.setAdapter(adpt);
}
You can use startActivityForResult()
Api Document
Call in activity 1
startActivityForResult( Activity2, Activity2RequestCode)
In Activity 2
int result = "Some message here";
output.putExtra(name , result);
setResult(RESULT_OK, output);
finish();
In Activity 1 get return value
onActivityResult (int requestCode, int resultCode, Intent data)
if (requestCode == ActivityTwoRequestCode && resultCode == RESULT_OK && data != null)
num1 = data.getIntExtra(Number1Code);
num2 = data.getIntExtra(Number2Code);
I am using .net web services. I am trying to get list in ListView. Right now it is showing me the first list, but when I am trying to get it again using the same method it is giving me a response in log but not displaying in list.
I have used mAdapter.notifyDataSetChanged(); in my Adapter but it's not working. Please help. thanks
My code:
Intent mIntent = getIntent();
mIntent.getStringExtra("folder_name");
Id = mIntent.getStringExtra("folder_ID");
mIntent.getStringExtra("item_parent");
User_ID = mIntent.getStringExtra("User_ID");
subfolderTreedata();
}
public void subfolderTreedata() {
try {
--------
--------
-------- //some code here...
SoapObject SubfolderResponse = (SoapObject)envelope.getResponse();
Log.i("SubFolders", SubfolderResponse.toString());
String File_Ext=" ";
subfoldersitem = new String[SubfolderResponse.getPropertyCount()];
System.out.println(subfoldersitem.length);
for(int i=0; i < SubfolderResponse.getPropertyCount(); i++) {
SoapObject SingleSubFolder = (SoapObject)SubfolderResponse.getProperty(i);
subfoldersitem[0] = SingleSubFolder.getProperty(1).toString();
subfoldersitem[1] = SingleSubFolder.getProperty(0).toString();
subfoldersitem[2] = SingleSubFolder.getProperty(3).toString();
if(KEY_SUBJECTTYPE.equalsIgnoreCase("Folder")) {
item = new FolderList(Folderimages[0], subfoldersitem[0], subfoldersitem[1], subfoldersitem[2]);
Subfolderdata.add(item);
} else{
StringTokenizer tokens = new StringTokenizer(Name, ".");
#SuppressWarnings("unused")
String first_string = tokens.nextToken();
File_Ext = tokens.nextToken();
if(File_Ext.equalsIgnoreCase("TIF")) {
item = new FolderList(TIFimages[0], subfoldersitem[0], subfoldersitem[1], subfoldersitem[2]);
Subfolderdata.add(item);
} else {
item = new FolderList(noImage[0], subfoldersitem[0], subfoldersitem[1], subfoldersitem[2]);
Subfolderdata.add(item); }
}
}
subfolderslistview = (ListView)findViewById(R.id.subfolderslistview);
mAdapter = new LazyAdapter(this, R.layout.jpg_row, Subfolderdata);
subfolderslistview.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
subfolderslistview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
LazyAdapter ca = (LazyAdapter)parent.getAdapter();
FolderList item_name = (FolderList)ca.getItem(position);
FolderList DocumentID = (FolderList)ca.getItem(position);
FolderList type = (FolderList)ca.getItem(position);
Intent mIntent = new Intent();
mIntent.putExtra("item_name", item_name.folder_name);
mIntent.putExtra("item_id", DocumentID.ID);
mIntent.putExtra("item_type", type.type);
mIntent.getStringExtra("item_name");
String Type = mIntent.getStringExtra("item_type");
Log.i("Type", Type);
if(Type.equalsIgnoreCase("Folder")){
Id = mIntent.getStringExtra("item_id");
mAdapter.notifyDataSetChanged();
subfolderTreedata();
} else {
Intent i = new Intent(getApplicationContext(), Display_image.class);
i.putExtra("item_name", item_name.folder_name);
i.putExtra("ID", DocumentID.ID);
i.putExtra("item_type", type.type);
i.putExtra("User_ID",User_ID);
i.getStringExtra("item_name");
Id = i.getStringExtra("ID");
i.getStringExtra("item_type");
Log.i("id", Id);
startActivity(i);
}
}
});
public void list() {
mAdapter = new LazyAdapter(this, R.layout.jpg_row, Subfolderdata);
subfolderslistview.setAdapter(mAdapter);
}
Use like this list();.
Call this method from Where do you want.
So, i plan to make a listview that if one of its item clicked, it goes to other activity. it's obvious that i want to use intent with extras.
But in my code, the extras is not reffering.
I tried to refer the extras with exact value, it worked, but if I put the extras to refer my object/variable, it won't work.
So far my code goes like this
public class bulan extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tagihan_list);
Intent i = getIntent();
final String no_pel = i.getStringExtra("no_pel");
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("nopel", no_pel));
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
JSON json = new JSON();
JSONObject jobj = json.getJSON("http://10.0.2.2/KP/tagihan.php", pairs);
try {
int length = jobj.getInt("panjang");
for(int n = 1; n <= length; n++){
String m = Integer.toString(n);
JSONObject row = jobj.getJSONObject(m);
String id_tagihan = "No. tagihan : " + row.getString("id");
String bulan = row.getString("bulan");
String tahun = row.getString("tahun");
String tagihan = "Rp. " + row.getString("tagihan");
String status = row.getString("status");
HashMap<String, String> map = new HashMap<String, String>();
map.put("id_tagihan", id_tagihan);
map.put("bulan", bulan);
map.put("tahun", tahun);
map.put("tagihan", tagihan);
map.put("status", status);
list.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(this, list, R.layout.list,
new String[] {"id_tagihan","bulan","tahun","tagihan","status"}, new int[] {
R.id.id_tagihan, R.id.bulan, R.id.tahun, R.id.tagihan, R.id.status});
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String sid = ((TextView) view.findViewById(R.id.id_tagihan)).getText().toString();
String snopel = no_pel;
Intent i = new Intent(bulan.this, rincian.class);
i.putExtra("id", sid);
i.putExtra("no_pel", snopel);
startActivity(i);
}
});
}
}
Like I explained before, it's this part
Intent i = new Intent(bulan.this, rincian.class);
i.putExtra("id", sid);
i.putExtra("no_pel", snopel);
startActivity(i);
that change my code to work if I write them like this
Intent i = new Intent(bulan.this, rincian.class);
i.putExtra("id", "00001");
i.putExtra("no_pel", "0620120001");
startActivity(i);
try
Intent i = new Intent(bulan.this, rincian.class);
Bundle b=new Bundle();
b.putString("id",sid);
b.putString("no_pel",snopel);
i.putExtras(b);
startActivity(i);
In your rincian class get the values as follows.
b=getIntent().getExtras();
String fri_id=b.getString("id");
String nopel=b.getString("no_pel");
I have filled the listview using the code given below, but I'm unablee to get back the values from list because getadpter written an object. From the object I didnt able to get one by one column value.
Code for filling the listview:
String pkManifest = manifest.pkManifestNo;
ArrayList<HashMap<String, String>> alist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
manifest_helper = new manifest_helper(this);
cursor = manifest_helper.GetDeliveriesAgainstManifest(pkManifest);
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
map = new HashMap<String, String>();
map.put("Deliv_Address",
cursor.getString(cursor.getColumnIndex("Address")));
map.put("Deliv_Time",
cursor.getString(cursor.getColumnIndex("Time")));
map.put("Deliv_Order",
cursor.getString(cursor.getColumnIndex("DeliveryOrder")));
map.put("Deliv_IsCustomerPickup",
cursor.getString(cursor.getColumnIndex("IsCustomerPickup")));
map.put("Deliv_FKDeliveryStatus",
cursor.getString(cursor.getColumnIndex("FKDeliveryStatus")));
map.put("Deliv_PKDelivery",
cursor.getString(cursor.getColumnIndex("PKDelivery")));
alist.add(map);
cursor.moveToNext();
}
sd = new SimpleAdapter(this, alist, R.layout.deliveries_list_row,
new String[] { "Deliv_Address", "Deliv_Time",
"Deliv_Order", "Deliv_IsCustomerPickup",
"Deliv_FKDeliveryStatus", "Deliv_PKDelivery" },
new int[] { R.id.tv_Deliv_Address, R.id.tv_Deliv_Time,
R.id.tv_Deliv_Order,
R.id.tv_Deliv_IsCustomerPickup,
R.id.tv_Deliv_FKDeliveryStatus,
R.id.tv_Deliv_PKDelivery });
//selectedAdapter = new SelectedAdapter(this, 0, alist);
//selectedAdapter.setNotifyOnChange(true);
ListDeliveries.setAdapter(sd);
Code for getting the value on onItemSelected is written like this:
private OnItemSelectedListener mDeviceSelectedListener = new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> av, View v, int arg2,long arg3) {
Object o = av.getAdapter().getItem(arg2);
String keyword = o.toString();
MessageBox("You selected: " + keyword);
}
private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
HashMap result = (HashMap) av.getAdapter().getItem(arg2);
pkDelivery = result.get("Deliv_PKDelivery").toString();
pbDeliveryOrder=result.get("Deliv_Order").toString();
isCustomerPickup=result.get("Deliv_IsCustomerPickup").toString();
if ("false".equals(isCustomerPickup))
{
customerPickup="false";
pbPickUp="false";
}
else
{
customerPickup="true";
pbPickUp="true";
}
launchDelivery_header(pkDelivery);
}
};
This can be done by getting the adapter value to a hash map and get it one by one
I need to display a ListView with some images, and I'm using the following code:
dataText = (TextView)findViewById(R.id.data);
dataText.setText(camping.stringIn + " >> " + camping.stringOut);
l = name.length;
ArrayList<tipologia> tipologieList=new ArrayList<tipologia>();
tipologia[] tip = new tipologia[l];
for (int i = 0; i < l; i++)
{
int rand = new Random().nextInt(3);
availability[i] = String.format("%d", rand);
tip[i] = new tipologia(image[i]);
}
for(int i=0; i<tip.length; i++)
{
tipologieList.add(tip[i]);
}
ArrayList<HashMap<String, Object>> data=new ArrayList<HashMap<String,Object>>();
for(int i=0;i<tipologieList.size();i++){
tipologia t = tipologieList.get(i);
HashMap<String,Object> tipologieMap = new HashMap<String, Object>();
tipologieMap.put("image", t.getImm());
data.add(tipologieMap);
}
String[] from={"image"};
int[] to={R.id.personImage};
SimpleAdapterMod adapter=new SimpleAdapterMod(
getApplicationContext(),
data,
R.layout.tipologia,
from,
to);
adapter.setViewBinder(new ViewBinder() {
public boolean setViewValue(View view, Object data,
String textRepresentation) {
if(data instanceof String && view instanceof Immagine ){
((Immagine)view).loadFromURL((String)data);
}
return true;
}
else{
return false;
}
}
});
I have a problem: in the list there's space for 4 rows, and when I scroll down it happens that the image of the fifth row is the same of the image of the first row and the subsequent images are wrong too.
Can someone tell me why?
Thats because the first row's view is reused for the fifth row, as the first row is now no more visible. The problem is in your adapter class.
In getView() method, do not reuse convertview, instead return a new view everytime. This is not the recommended solution but to be used as last resort.