Reputation: 176
i have written a program to display a listview , when i click on the item of the listview a new activity should display which has another listview
MainAactivity
Here i am passing the days array in the listview which is displaying perfectly and on itemclick another activity opens, and in my new activity i want a listview with the elements which i added in my arraylist , like when i click on monday i need only monday in my new listview , when i click on tuesday i need only tuesday in my new listview , i tried this program with the help of a textview and it works fine , but i want to try it with the help of a listview , need some help
public class MainActivity extends Activity {
ListView list;
final ArrayList<String> arraylist = new ArrayList<>();
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listview1);
arraylist.add("Monday");
arraylist.add("Tuesday");
arraylist.add("Wednesday");
arraylist.add("Thursday");
arraylist.add("Friday");
arraylist.add("Saturday");
arraylist.add("Sunday");
final String[] days = { "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday" };
adapter = new ArrayAdapter<String>(getBaseContext(),
android.R.layout.simple_list_item_1, days);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Intent intent = new Intent("com.demo.listview.LIST");
intent.putExtra("name", arraylist);
intent.putExtra("name", arraylist);
startActivity(intent);
}
});
My List.java which has a 2nd listview in which i want the arraylist elements to be displayed one by one when i click on the specific item on the first listview
public class List extends Activity {
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
lv = (ListView) findViewById(R.id.listView1);
Intent intent = getIntent();
ArrayList<String> arr = (ArrayList<String>)intent.getStringArrayList("name");
}
}
i know i am going wrong in both my activities any suggestions are welcomed
Upvotes: 0
Views: 927
Reputation: 162
@Arjun : There is no need to add an arraylist in your first activity. If you want only the clicked item to be moved to your second activity then use intent.putExtra(days[position]) The onItemClick function gives the position of the item that received the click. Then use that extra from the intent in the second activity....But what's the need of showing a single item in a list if you know that you have only 1 item to be shown in the second activity.? Textview for your second activity is a better option.
Upvotes: 0
Reputation: 5097
In your MainActivity, no need to have
final String[] days = { "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday" };
You can set ArrayList directly to ArrayAdapter like this,
adapter = new ArrayAdapter<String>(getBaseContext(),
android.R.layout.simple_list_item_1, arraylist);
For onItemClick,
Intent intent = new Intent("com.demo.listview.LIST");
intent.putExtra("name", arraylist.get(position));
startActivity(intent);
For List Activity, again use,
final ArrayList<String> arraylist = new ArrayList<String>();
and inside onCreate, try this.
Intent intent = getIntent();
String day = intent.getStringExtra("name");
arraylist.add(day);
adapter = new ArrayAdapter<String>(getBaseContext(),
android.R.layout.simple_list_item_1, arraylist);
Let's know if this works for you.
Upvotes: 1