Clickable custom array list - android

How can I make this custom array list clickable to go to the others activities because I tried the intents but it doesn't work
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Names> namesArrayList = new ArrayList<Names>();
namesArrayList.add(new Names(R.drawable.call_centre, "Call Centre"));
namesArrayList.add(new Names(R.drawable.soco_academy_icon, "Academy"));
NamesAdapter NamesListAdapter = new NamesAdapter(this, namesArrayList);
ListView list = (ListView) findViewById(R.id.List_View);
list.setAdapter(NamesListAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}

In onItemClick() you can determine which item was clicked by doing this:
Name selectedName = NamesListAdapter.getItem(position);
Then you can do whatever you want with that.

Related

How set onclick with LinearLayout (android)

I have used the following tutorial "https://www.simplifiedcoding.net/android-volley-tutorial-to-get-json-from-server/ "to display data onto Android using ListView and LinearLayout. I want to go to another screen when I click on an item from the list. I have added this to my MainActivity but it didn't work:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//ListView lv = getListView();
buttonGet = (Button) findViewById(R.id.buttonGet);
buttonGet.setOnClickListener(this);
listView = (ListView) findViewById(R.id.listView);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
sendRequest();
linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, EventDetails.class);
MainActivity.this.startActivity(intent);
}
});
}
You'll have to set the LinearLayout to clickable. You can either do this in the XML with adding below in linearlayout tag
android:clickable="true"
Or in code with
linearLayout.setClickable(true);
That tutorial uses a ListView, not a LinearLayout.
You add Item-ClickListeners to ListViews.
final Context ctx = YourActivity.this;
yourListView = (ListView) findViewById...
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Data clicked = adapter.getItem(position);
// Do something with 'clicked'
// startActivity(ctx, ShowDataActivity.class);
}
});
int example_layout = android.R.simple_list_item_1;
adapter = new ArrayAdapter<Data>(ctx, example_layout, new ArrayList<Data>());
yourListView.setAdapter(adapter);
By the way, that tutorial uses a deprecated version of Volley
You can implement a public interface to manage the click events on the adapter elements. To do this, define a public interface in your adapter (in the tutorial is the CustomList class)
public class CustomList extends ArrayAdapter<String> {
public interface OnClickOnItemList
{
public void clickInView(View v,int position);
}
private OnClickOnItemList mListener;
public setOnClickOnItemListener(OnClickOnItemList l) {this.mListener = l}
/*...*/
}
And in the same class go to getView. You should assign this new listener to the event onClick
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.list_view_layout, null, true);
linearLayout = (LinearLayout) listViewItem.findViewById(R.id.linearLayout);
/*Rest of subviews*/
/*Linear Layout now will call the new listener*/
linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mListener.clickInView(listViewItem,position);
}
});
return listViewItem;
}
}
Subsequently, the main activity must implement this public interface
public class MainActivity extends AppCompatActivity implements CustomList.OnClickOnItemList
{
/*Code*/
#Override
public void clickInView(View v,int position)
{
Intent intent = new Intent(MainActivity.this, EventDetails.class);
startActivity(intent);
}
private void showJSON(String json){
ParseJSON pj = new ParseJSON(json);
pj.parseJSON();
CustomList cl = new CustomList(this, ParseJSON.ids,ParseJSON.names,ParseJSON.emails);
cl.setOnClickOnItemListener(this);
listView.setAdapter(cl);
}
}
You can add in your xml
<LinearLayout
android:id="#+id/yourid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"

list item select set bgcolor but scroll down automatically list item set bgcolor changed

particular list item select to set bgcolor for list item ,when scroll down listview to automatically set color for other item, my sample code suggestion and help me.(this code v.setbackgroundcolor(Color.gray) using problem occur,so clear the problem)
public class MainActivity extends Activity {
ListView list;
ArrayAdapter<String> adapter;
ArrayList<String>listitem=new ArrayList<String>();
String val;
String[] list_number={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=(ListView)findViewById(R.id.listView1);
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list_number);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
String selectedFromList =(list.getItemAtPosition(position).toString());
if(listitem.isEmpty())
{
listitem.add(selectedFromList);
Set<String> primesWithoutDuplicates = new LinkedHashSet<String>(listitem);
listitem.clear();
listitem.addAll(primesWithoutDuplicates);
v.setBackgroundColor(Color.GRAY);
}
else
{
if( listitem.contains(selectedFromList))
{
listitem.remove(selectedFromList);
v.setBackgroundColor(Color.WHITE);
}
else
{
listitem.add(selectedFromList);
Set<String> primesWithoutDuplicates = new LinkedHashSet<String>(listitem);
listitem.clear();
listitem.addAll(primesWithoutDuplicates);
v.setBackgroundColor(Color.GRAY);
}
}
}
});
}
}

How to implement onItemClickListener on each item in the ListView to go to another activity?

How to implement onItemClickListener on each item in the ListView to go to another activity / to a new class?
public class MainActivity extends Activity{
ListView list;
String[] itemname ={
"Resturants",
"Coffee Shops",
"Hotels",
"Gas Stations",
"Hospitals",
"Airports",
"ATM",
"Cinemmas",
"Phamacies"
};
Integer[] imgid={
R.drawable.restaurantz,
R.drawable.coffeeshop,
R.drawable.hotel,
R.drawable.gaspump,
R.drawable.hospitalblue,
R.drawable.airporticon,
R.drawable.atm,
R.drawable.cinemma,
R.drawable.hospitalblue,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
CustomListAdapter adapter = new CustomListAdapter(this, itemname, imgid);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}
inside onItemClick:
startActivity(new Intent(MainActivity.this, NewActivity.class));
if you need to differenciate the clicked item, you can use the position parameter, for example:
if (position == 0)
// do something
else
// do something else

setOnItemClickListener not working

I am not able get onItemCickListener to work. All I am doing is opening up a fragment using intents. Please do let me know if I am doing anything fishy here ?
public class MyListFragment extends ListFragment {
private ArrayList<String> myList = null;
private ArrayAdapter<String> myAdapter;
#Override
public void onActivityCreated(Bundle savedInstanceState){
Log.d("Example:", "In Fragement Calss");
super.onActivityCreated(savedInstanceState);
Resources myResources = getResources();
myList = new ArrayList<String>(Arrays.asList(myResources.getStringArray(R.array.myExamArray)));
myAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,myList);
setListAdapter(myAdapter);
ListView lv = (ListView) getActivity().findViewById(R.id.listView);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = new Intent(view.getContext(), DetailsFragment.class);
startActivity(myIntent);
}
});
}
}
Note that the ListView blocks clicks of an item that contains at least one focusable
descendant but it doesn’t make the content focus-reachable calling
setItemsCanFocus(true). One workaround is by disabling focusability of descendants, using
android:descendantFocusability="blocksDescendants"
in the layout defining your list item. (First learned of this myself from http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/, but a few other SO posts also point this out.) Does your layout contain buttons? If so, you'd fall into this case.
I don't see where you are inflating a specific XML for this Fragment... but it is possible you are referencing the wrong ListView.
Regardless you should use the existing onListItemClick method:
public class MyListFragment extends ListFragment {
private ArrayList<String> myList = null;
private ArrayAdapter<String> myAdapter;
#Override
public void onActivityCreated(Bundle savedInstanceState){
// Remove the OnItemClickListener, but keep everything else
}
#Override
public void onListItemClick (ListView l, View v, int position, long id) {
Intent myIntent = new Intent(this, DetailsFragment.class);
startActivity(myIntent);
}
}

How do I select a row in a listview not using ListActivity

Here is the code:
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Find your views
final ListView listPro = (ListView) findViewById(R.id.listProperty);
DBAdapter db = new DBAdapter(this);
db.open();
final Cursor c = db.getAllProperties();
startManagingCursor(c);
// Create the adapter
MainAdapter adapter = new MainAdapter(this, c);
listPro.setAdapter(adapter);
// here is where I think I should put the code to select the row but I
// haven't been able to figure out how to do it.
// all I need to get out is the row id number.. please help
Button addPropBut = (Button) findViewById(R.id.mainButton);
addPropBut.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i7 = new Intent(Main.this, Home.class);
startActivity(i7);
}
});
}
}
try this one, hope it will help you out.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
}
you can get the id number by using Log or System.out.println();
Instead of using Button's setOnClickListener use ListView's setOnItemClickListener.
listPro.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
The id parameter will give you the needed id.
Salil.

Categories

Resources