Android listview - android

I have created an android application. In that application when I click the listview item it should display in the another listview in the same layout.
Is this possible in android?

Well, quick snippet:
public Activity1 extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle b) {
// stuffs here
....
// ListView event
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("SelectedString", listView.getItemAtPosition(position));
startActivity(intent);
}
});
}
}
public Activity2 extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle b) {
// stuffs here
....
String valueFromActivity1 = getIntent().getString("SelectedString");
// ok now, u've got value from Activity1, do whatever w/ it
}
}

No you have to make intent and pass the variables of the current selected item of the listview to that intent and display the dynamic listview for that Item

Related

Android studio: ListView to Tabbed Activity, save ratingbar rating on the tab pages

I'm making my first android app using android studio. In this APP I have a listview with 12 classes(12 items). After clicking on one class, it goes into a tabbed activity with 10 items of this class. On each tab page I have a rating bar to let people rate the item.
I set an activity for the listview, and 12 independent activities for those 12 tabbed activities. The code from listview to each tabbed activity is like this:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(i==0){
Intent intent = new Intent(ListViewActivity.this, TabbedActivity1.class);
intent.putExtra("styleName", STYLE_NAMES[i]);
intent.putExtra("styleExample",STYLE_EXAMPLES[i]);
startActivity(intent);
}
else if(i==1){
Intent intent = new Intent(ListViewActivity.this, TabbedActivity2.class);
intent.putExtra("styleName", STYLE_NAMES[i]);
intent.putExtra("styleExample",STYLE_EXAMPLES[i]);
startActivity(intent);
}
...... // skip the other 10 tabbed activities.
}
Now the problem is: after I finish rating on the tabbed activities, I return to the ListView activity and click into each tabbed activity again, the ratings are gone.
I guess the reason is that in my code, each time I click on the item it opens a new tabbed activity, although same layout, the contents are not saved.
So I was wondering whether I should do something on the ListView activity to save the ratings. I have searched for relevant questions, but I found in their scenarios, each list item is just a simple ratingbar. But here, my list item is a tabbed activity with 10 ratingbars.
Therefore, I have no idea how to do it. I have no experience in android studio, so I don't know where to start to solve the problem. Any idea is appreciated! Thanks a lot in advance!!
First of all if all your tab activities are similar you can just create one activity instead of that many in your case 12 and pass the specific content and states via intent.
The basic approach to your question is would be store rating states in your main activity and when you open your tab activity each time you click the list items send the rate of the relevant activity with intent. Then in your tab activity update the rate with it.
To achieve this we are going to use startActivityForResult instead of startActivity because we need tab activity to return last state of rating bars.
You can see the basic example shown below here:
public class ListViewActivity extends AppCompatActivity {
private static final int REQUEST_RATE = 1;
private SparseIntArray rates = new SparseIntArray();
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(ListViewActivity.this, TabActivity.class);
intent.putExtra("styleName", STYLE_NAMES[i]);
intent.putExtra("styleExample", STYLE_EXAMPLES[i]);
intent.putExtra("position", i);
intent.putExtra("rating", rates.get(i, 0));
startActivityForResult(intent, REQUEST_RATE);
}
}
}
#Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_RATE:
if(resultCode == RESULT_OK) {
//retrieve and save rates
Bundle extras = data.getExtras();
int position = extras.getInt("position");
int rating = extras.getInt("rating");
rates.put(position, rating);
}
break;
}
}
}
public class TabActivity extends AppCompatActivity {
private RatingBar ratingBar;
private int position;
#Override protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
Bundle extras = getIntent().getExtras();
position = extras.getInt("position");
int rating = extras.getInt("rating");
ratingBar.setRating(rating);
}
#Override protected void onDestroy() {
//send current rating to list activity before we leave
setResult();
super.onDestroy();
}
private void setResult() {
Intent intent = new Intent();
intent.putExtra("position", position);
intent.putExtra("rating", ratingBar.getRating());
setResult(RESULT_OK, intent);
}
}

How to Pass the Data from Listview A to ListView B?

I have my Cart Activity which contains the items that the user wanted to purchase.
What I want is if I click the "CHECK OUT" button, all of the data in the cart will be sent to the next Activity having ListView B.
Here is a part of my CartCustomerAdpater.class where i set the textview fields
final Order data = items.get(position);
holder.cart_name.setText(data.getName());
holder.cart_price.setText("Php "+data.getPrice());
holder.cart_qty.setText(data.getQty());
Here is a part of my Cart.class where my CHECK OUT button is located.
checkout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//WHAT SHOULD I DO HERE?
}
});
Suppose you declared your ListView:
ListView lv =(ListView) findViewById(R.id.lv);
For the case of a listView use setOnItemClickListener() , this is my sample code for sending data to the next Activity.
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ModelClass jhild = (ModelClass) adapter.getItem(position);
Intent in = new Intent(getApplicationContext(), BuyApartmentsInformation.class);
in.putExtra(KEY_DISTRICT_NAME, jhild.getDistrict());
in.putExtra(KEY_FLOORS, jhild.getFloors());
in.putExtra(KEY_AREA, jhild.getArea());
in.putExtra(KEY_BUYLAND_CODE, jhild.getBuyno());
in.putExtra(KEY_PRICES, jhild.getPrice());
in.putExtra(KEY_INFORMATION, jhild.getInformation());
startActivity(in);
}
});
But my piece of Advice you have to update to RecyclerView
Hope it works well.
use intent
send
Intent intent = new Intent(A.this, B.class);
intent.putextra("keyName","value");
startActivity(intent);
recieve
String data = getIntent().getExtras().getString("keyName");
1. You need to implement Serializable or Parcelable in your **Order.java** class so that you can pass the ArrayList of Order through Bundle or Intent .
public class Order implements Serializable{
}
2. Pass the list of items through intent and start new activity
checkout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent newActivity = new Intent(this , NewActivity.class);
newActivity.putExtra("orderList" , items);
startActivity(newActivity);
}
});
3. Get the list in NewActivity class
List<Order> itemsList = (List<Order>)getIntent.getSerializableExtra("orderList");
Hope it helps!

How can get ListView object in setOnClickListener?

In Code 2, I use final ListView lv=this.getListView() to get ListView object, it's OK.
In Code 1, I try to use ListView lv=((ListActivity)getApplicationContext()).getListView() to get ListView object, but it's bad.
Is there a simple way to get get ListView object in setOnClickListener? Thanks
Code 1
private void IniControl(){
findViewById(R.id.btnCancel).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ListView lv=((ListActivity)getApplicationContext()).getListView();
for(int i=0;i<lv.getCount();i++){
EditText number = (EditText)lv.findViewWithTag("editNumber"+i);
Toast.makeText(getApplicationContext(),number.getText().toString()+"CW",Toast.LENGTH_SHORT).show();
}
}
});
}
Code 2
public class StepList extends ListActivity{
private ListNumberAdapter mListNumberAdapter=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sms_step_list);
}
private void IniControl(){
final ListView lv=this.getListView();
findViewById(R.id.btnCancel).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for(int i=0;i<lv.getCount();i++){
EditText number = (EditText)lv.findViewWithTag("editNumber"+i);
Toast.makeText(getApplicationContext(),number.getText().toString()+"CW",Toast.LENGTH_SHORT).show();
}
}
});
}
}
Is IniControl also a method of StepList in your Code 1 ?
If so, you can simply call getListView() from there (the OnClickListener being an inner class of your StepList), or more explicitely StepList.this.getListView().
The ListView is not a member of the ApplicationContext. Why cant you add the ListView as a member of the Activity instead? Or use the getListView() on the ListActivity's context.
You can write separate OnClickListener method and pass ListView to it.
private View.OnClickListener onClick(final ListView lv) {
return new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something
}
};
}
You can use onListItemClick Method
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(getApplicationContext(), "hoohoo..you clicked me at " + position,Toast.LENGTH_SHORT).show();
}
It will display the the row number you clicked
Make your ListView a class member Variable
private ListView mListView;
Then in onCreate(), call mListView = (ListView) this.getListView();
And then you can use the mListView object anywhere in the rest of the code.

Activity missing data on return

So I have my main activity which holds a list view and has a map which holds all my data for the list. Upon clicking on an item you are taken to a details display. When I press the back button to get back to the main activity from the detail activity, if I set a break point, my map keys are still intact, but all the strings in the objects are "" and the ints are -1. Here is what my main activity looks like:
public class MainActivity extends Activity {
private Map<String, Stunt> stunts = new LinkedHashMap<String, Stunt>();
private StuntsDao stuntsDao;
private ListAdapter listAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stuntsDao = new StuntsDao(getApplicationContext());
stunts = stuntsDao.getAllStunts();
listAdapter = new ListAdapter(this, R.layout.list_layout, new ArrayList<Stunt>(stunts.values()));
ListView listView = (ListView)findViewById(R.id.listView);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String stuntName = (String)((ViewHolder)view.getTag()).stuntName.getText();
Intent myIntent = new Intent(getApplicationContext(), StuntDetails.class);
getStuntDetailsIfNeeded(stuntName);
Stunt stunt = stunts.get(stuntName);
myIntent.putExtra("STUNT", stunt);
startActivity(myIntent);
}
});
}
...
}
Why would the objects in my map be basically empty?
onCreate() doesn't get called when you click back. You need to init your data again in onStart or onResume. See: http://developer.android.com/training/basics/activity-lifecycle/starting.html

Android loading different contents in the same screen but different activities

i am new to Android and i am facing a problem in calling different activities from the same screen with same user interface.
Actually i want to implement d functionality of a tab activity but instead of tabs i am providing buttons and the buttons should act like tabs.
I am unable to do that. I am going wrong some where.
Can anyone help me please.....
HomeScreen class is:
public class HomeScreen extends Activity implements OnItemClickListener {
public Integer[] images = { R.raw.mobile, R.raw.note_books, R.raw.ac,
R.raw.drivers, R.raw.camera, R.raw.home_theaters, R.raw.pda,
R.raw.tv, R.raw.washing_machines, R.raw.scanners };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
GridView gv = (GridView) findViewById(R.id.gridV);
LayoutInflater inflater = getLayoutInflater();
gv.setAdapter(new GridViewAdapter(images, inflater));
gv.setOnItemClickListener(this);
if (StaticUtils.scheckStatus){
parseData();
}
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent contents = new Intent(HomeScreen.this, Cat.class);
contents.putExtra("homescreen", arg2);
startActivity(contents);
}
Cat.class is this:
class Cat extends Activity implements OnClickListener{
private Button mBtnContents, mBtnBrand, mBtnCategory, mBtnBack;
#Override
public void onCreate(Bundle si){
super.onCreate(si);
setContentView(R.layout.gridtab);
int i = getIntent().getIntExtra("homescreen", 0);
mBtnContents=(Button) findViewById(R.id.btnContents);
mBtnContents.setOnClickListener(this);
mBtnBrand=(Button) findViewById(R.id.btnBrand);
mBtnBrand.setOnClickListener(this);
mBtnCategory=(Button) findViewById(R.id.btnCategory);
mBtnCategory.setOnClickListener(this);
mBtnBack=(Button) findViewById(R.id.btnBack);
mBtnBack.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v==mBtnContents){
int i = getIntent().getIntExtra("homescreen", 0);
Intent in=new Intent(Cat.this, Pc.class);
in.putExtra("homescreen", i);
startActivity(in);
} else if(v==mBtnBrand){
startActivity(new Intent(Cat.this, Sd.class));
} else if(v==mBtnCategory){
startActivity(new Intent(Cat.this, Sbc.class));
} else if(v==mBtnBack){
startActivity(new Intent(Cat.this, Hs.class));
}
}
}
When i click on contents button its displaying the details but when i click on the other buttons its not showing anythng
Instead of "v==mBtnContents" use "v.equals(mBtnContents)" because View is an object.

Categories

Resources