this is giving error.plz suggest some answer - android

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
DataModel dataModel = (DataModel) parent.getItemAtPosition(position);
// Log.d("imageId--- ", String.valueOf(dataModel.getImageid()));
//
// Intent intent = new Intent(MainActivity.this, DetailActivity.class);
// intent.putExtra(IntentKeyConstant.INTENT_KEY_ITEM_DRAWABLE_ID, dataModel.getImageid());
// intent.putExtra(IntentKeyConstant.INTENT_KEY_ITEM_NAME, dataModel.getName());
// intent.putExtra(IntentKeyConstant.INTENT_KEY_ITEM_COST, dataModel.getPrice());
// intent.putExtra(IntentKeyConstant.INTENT_KEY_ITEM_NAME_DETAIL, dataModel.getDetailName());
// intent.putExtra(IntentKeyConstant.INTENT_KEY_ITEM_DETAIL, dataModel.getDetail());
Intent intent1 = new Intent(MainActivity.this, DetailActivity.class);
intent1.putExtra("keyclassobject",dataModel);
startActivity(intent1);
}
});
another activity is
final DataModel dataModel = (DataModel) intent.getSerializableExtra("keyclass");
// drawableId = intent.getIntExtra(IntentKeyConstant.INTENT_KEY_ITEM_DRAWABLE_ID, 0);
// String itemName = intent.getStringExtra(IntentKeyConstant.INTENT_KEY_ITEM_NAME);
// String itemCost = intent.getStringExtra(IntentKeyConstant.INTENT_KEY_ITEM_COST);
// String itemNameDetail = intent.getStringExtra(IntentKeyConstant.INTENT_KEY_ITEM_NAME_DETAIL);
// String itemDetail = intent.getStringExtra(IntentKeyConstant.INTENT_KEY_ITEM_DETAIL);
//Log.d("name", itemName);
// Log.d("itemcost",itemCost);
//Log.d("imageId ",String.valueOf(drawableId)+ "-"+Integer.parseInt(drawableId));
Typeface typeface=Typeface.createFromAsset(getAssets(),"fonts/fontawesome-webfont.ttf");
tv_item_rupee_symbol=(TextView)findViewById(R.id.tv_item_rupee_symbol);
tv_item_rupee_symbol.setTypeface(typeface);
toolbar_title.setText(dataModel.getName());
iv_item.setImageResource(dataModel.getImageid());
tv_item_name_detail.setText(dataModel.getDetailName());
tv_item_rupee_symbol.setText("\uf156");
tv_item_cost.setText(dataModel.getPrice());
tv_item_detail.setText(dataModel.getDetail());

Intent intent = getIntent();
If your extra data is represented as strings, then you can use intent.getStringExtra(String name) method. In your case:
String id = intent.getStringExtra("keyclassobject");

Related

Pass ListView value to EditText

I have a listView in Activity A as shown below.
When the first list is clicked, it should display 3 on editText. But it displays 5 which was actually getting from the last list.
Activity A
ArrayAdapter<String> adapter;
ArrayList<String> m_listItems = new ArrayList<String>();
int mClickedPosition;
adapter=new ArrayAdapter<String (getActivity(),R.layout.claims,R.id.textView1,m_listItems);
listV = (ListView) claims.findViewById(R.id.listView1);
listV.setOnItemClickListener(new
AdapterView.OnItemClickListener() { // when list is pressed, intent to B
public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
mClickedPosition = position;
if (name.equals("Project")) {
Intent intent = new Intent(Claims1.this.getActivity(), B.class);
intent.putExtra("bitmap", true);
intent.putExtra("name", name);
intent.putExtra("result", result);
startActivityForResult(intent, 0);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) { // receive from B
case 0:
result = data.getStringExtra("text");
name = data.getStringExtra("a");
description = data.getStringExtra("c");
as = Long.parseLong(result);
Log.d("FIRST", "result:" + result);
Text = " " + name + " " + "RM" + result + "";
// m_listItems.clear();
if (mClickedPosition == -1) {
m_listItems.add(Text);
} else {
m_listItems.set(mClickedPosition, Text);
}
adapter.notifyDataSetChanged();
listV.setAdapter(adapter);
break;
}
Activity B
if(getIntent().getExtras()!=null) { //if has value pass from A
final String Amount = getIntent().getExtras().getString("result");
final String description1 = getIntent().getExtras().getString("description");
txt1.setText(description1);
txt.setText(Amount);
}
b.setOnClickListener(new View.OnClickListener() { // return to A
public void onClick(View arg0) {
Intent returnIntent = new Intent();
a = "Project";
text = txt.getText().toString(); // amount
returnIntent.putExtra("text", text);
returnIntent.putExtra("a", a);
returnIntent.putExtra("c", c); // receive from Activity C
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
viewImage.setImageBitmap(Global.img); // receive from C
}
Use you ArrayList to extract String at position of ListView like..
listV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
mClickedPosition = position;
String result = m_listItems.get(position); //add this
result = result.substring(2);
if (name.equals("Project")) {
Intent intent = new Intent(Claims1.this.getActivity(), B.class);
intent.putExtra("result", result); // your intent stuff
}
}
});

how sent integer parameter in intent

I want to send int parameter in intent like this:
String textname = (String) dataItem.get("name");
Intent m = new Intent(list.this,main.class);
m.putExtra("name",textname);
m.putExtra("page",1);
startActivity(m);
and in main class I get that parameter
Intent intent = getIntent();
name = intent.getStringExtra("name");
page1 = Integer.parseInt(intent.getStringExtra("page"));
but when I run my code it force closes!!!
You should use getIntent().getIntExtra(name, defaultValue) instead of Integer.parseInt(intent.getStringExtra("page"));
Update:
int defaultValue = -1;// take any default value of your choice
String name = intent.getStringExtra("name");
int page1 = intent.getIntExtra("page", defaultValue);
I in other class use the main class and send parameter to it and it work without any problem but just in list class i have problem
in other class i like this send parameter
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent i = new Intent(list_sub.this,main.class);
i.putExtra("name", Name[position]);
i.putExtra("page", Ted[position]);
startActivity(i);
}
and in refresh class i get ted parameter
private void refresh(){
db.open();
int s = db.List_count("text", ID);
Name= new String[s];
Ted=new String[s];
for(int i=0;i<s;i++){
Name[i]=db.List_display_sub("text", ID, i);
Ted[i]=db.page_count("text", Name[i].toString())+"";
}
db.close();
}
Activity A
String textname = (String) dataItem.get("name");
Intent m = new Intent(list.this,main.class);
m.putExtra("name",textname);
m.putExtra("page",1);
startActivity(m);
Activity B
Intent intent = getIntent();
name = intent.getStringExtra("name");
int page = intent.getIntExtra("page", 0);
where 0 is the default value.

How to refresh list View in android?

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.

Null pointer in putExtras

I have some trouble with putExtras to an intent. Could you please review my code?
public void onSelectCategory(View v) {
int category = Integer.parseInt((String) v.getTag());
Intent intent = new Intent(HomeActivity.this, ListActivity.class);
intent.putExtra("EXT_CATEGORY", category);
startActivity(intent);
}
And in the ListActivity, I'm doing the following..
public static final String EXT_CATEGORY = "category";
int category = getIntent().getExtras().getInt(EXT_CATEGORY);
From this line
intent.putExtra("EXT_CATEGORY", category);
in your another activity String name should be same means:--
public static final String EXT_CATEGORY = "EXT_CATEGORY";
You did several mistakes, here is a overworker version of your code.
public void onSelectCategory(View v) {
int category = Integer.parseInt((String) v.getTag());
Intent intent = new Intent(HomeActivity.this, ListActivity.class);
intent.putExtra(EXT_CATEGORY, category);
startActivity(intent);
}
int defaultCat = -1;
public static final String EXT_CATEGORY = "category";
int category = getIntent().getIntExtra(EXT_CATEGORY,defaultCat); // Use default int if there is no extra
You should use this to get you info, not getExtras() :
int category = getIntent().getIntExtra("EXT_CATEGORY");
getExtras() returns additional Bundle of data. You need only one integer.

listview onitemclick can't refer

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");

Categories

Resources