Reputation: 83
I have a List view. I want to click on each list item and it would open different activity. Actually, I wrote the code, but its not working. My coding skills are unable to handle the issue. How to create a Hahmap with a string and a class pair and then put that into ArrayAdapter . Can any one please show me the code?
ListViewAcivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
public class ListViewActivity extends Activity {
ListView listView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen_activity);
// Get ListView object from xml
listView = (ListView) findViewById(R.id.list);
// Defined Array values to show in ListView
HashMap<String, Class> hashMap=new HashMap<String, Class>();
hashMap.put("A Function", MActivity.class);
hashMap.put("B Function",AActivity.class);
hashMap.put("c Function",XActivity.class);
hashMap.put("D Function",ZActivity.class);
hashMap.put("E Function", PActivity.class);
hashMap.put("F Function", QActivity.class);
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
//error is here...... // this constructor can not be solved. //I don't know how to use //String, Class pair with Array Adapter.
ArrayAdapter<HashMap<String,Class>> adapter = new ArrayAdapter<HashMap<String,Class>>(this, android.R.layout.simple_list_item_1, android.R.id.text1, hashMap);
// Assign adapter to ListView
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
switch(itemPosition){
case 0: Intent newActivity = new Intent(ListViewActivity.this, MActivity.class);
startActivity(newActivity);
break;
case 1: Intent newActivity1 = new Intent(ListViewActivity.this, AActivity.class);
startActivity(newActivity1);
break;
case 2: Intent newActivity2 = new Intent(ListViewActivity.this, XActivity.class);
startActivity(newActivity2);
break;
case 3: Intent newActivity3 = new Intent(ListViewActivity.this, ZActivity.class);
startActivity(newActivity3);
break;
case 4: Intent newActivity4 = new Intent(ListViewActivity.this, PActivity.class);
startActivity(newActivity4);
break;
case 5: Intent newActivity5 = new Intent(ListViewActivity.this, QActivity.class);
startActivity(newActivity5);
break;
}
}
@SuppressWarnings("unused")
public void onClick(View v){
};
});}
}
main_screen_Activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
Upvotes: 2
Views: 796
Reputation: 1825
Simple Static example for the functionality you are asking for.
public class MainActivity extends AppCompatActivity {
ListView lv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialize the list view
lv1 = (ListView) findViewById(R.id.mainlist);
//Add the List data
//as the array is stored starting with 0, the Layouts will be having 0th position, Intents being 1 and so on..
String[] sessiontuts = new String[]{"Activity 1", "Activity2"};
//use the Simple array adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,sessiontuts);
//now to bind the data to list, just set the adapter we just created to the listview,
lv1.setAdapter(adapter);
//we need to have click listner on the particular item,
//all the items in list will have a position starting from 0 to n,
//so, write the intent code to launch particular activity depending on list item position
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
//using switch case, to check the condition.
switch (pos){
case 0:
startActivity(new Intent(getApplicationContext(), Act1.class));
break;
case 1:
startActivity(new Intent(getApplicationContext(), Act2.class));
break;
}
}
});
}
}
Upvotes: 1
Reputation: 140427
An ArrayAdapter expects an array or a List of Objects to display. So you need to provide an array/list
What makes you think that it can deal with a Map? Lists/arrays represent sequences of things; whereas maps represent, well a mapping from some kind of thingy to some other thingy.
In other words: you can't pass a map object.
Simply pass a list or an array containing the strings you want to display ("Medical Reminders", ... ).
But there is value in having that map; you just need to change your code a bit:
First, you can use the keys of the map as input when creating the ArrayAdapter, like this:
List<String> arrayItems = new ArrayList<>(hashMap.keySet());
And then, in onClickItem()
you do not need to retrieve the index - because you already have that map that tells you which class belongs to which string! Meaning: the ListView gives you the selected item as string, and hashMap
maps all possible strings to the corresponding activity class!
So, you can throw away your whole "switch" code and instead do something like:
String itemValue = (String) listView.getItemAtPosition(position);
Class classForSelectedValue = hashMap.get(itemValue);
Intent activity = new Intent(ListViewActivity.this, classForSelectedValue);
Upvotes: 2