I tried various solutions mentioned in SO but still my issue is not resolved. Mentioned the code below.
Example.java
package com.android.Example;
import java.util.Collections;
import java.util.List;
import com.android.Example.db.Tbl_example_Title_DAO;
import com.Example.database.handler.example_Titles;
import com.android.Example.adapter.ArrayListAdapter;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
public class Example extends Activity {
Tbl_example_Title_DAO tbl_example_title;
example_Titles example_Titles;
ListView lvlist;
List<String> exampleTitlesList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_singleitems_list);
lvlist = (ListView)findViewById(R.id.list);
tbl_example_title = new Tbl_example_Title_DAO(this);
lvlist.setClickable(true);
lvlist.setLongClickable(true);
lvlist.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String s=exampleTitlesList.get(arg2);
Log.w("HHHHHHHHHHHHHH", s);
return true;
}
});
showlist();
}
private void showlist() {
example_Titles = new example_Titles();
example_Titles.setexample("E");
//List<example_Titles> exampleTitlesList = tbl_example_title.getTbl_example_Detailss(example_Titles);
exampleTitlesList = tbl_example_title.getTbl_example_Details_str("E");
Collections.sort(exampleTitlesList);
ArrayListAdapter arrayAdapter = new ArrayListAdapter(Example.this, exampleTitlesList);
lvlist.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
}
}
listview_singleitems_list.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#color/transparent"
android:dividerHeight="10dp"
android:drawSelectorOnTop="true"
android:footerDividersEnabled="false"
android:padding="10dp"
android:scrollbarStyle="outsideOverlay" />
</RelativeLayout>
Each row of the listview - amc.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="#+id/lvi"
android:descendantFocusability="blocksDescendants"
>
<TextView
android:id="#+id/tvCategory_listItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"/>
</LinearLayout>
Data is getting populated but not responding to any click events.
Kindly help me understand what i am missing in the code. Thanks
Update 1 - Tried adding the below method. But Did not work.
lvlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
Log.w("HHHHHHHHHHHHHH", item);
}
});
Update 2 - I Tried to use the below code and now the onclick works fine.So, the issue is with the way i have created the amc.xml file only. If anybody knows what is the issue, please let me know
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, categoryTitlesList);
lvlist.setAdapter(adapter);
adapter.notifyDataSetChanged();
instead of
ArrayListAdapter arrayAdapter = new ArrayListAdapter(ExpenseCategoryList.this, categoryTitlesList);
lvlist.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
get rid of these codes..
lvlist.setClickable(true);
lvlist.setLongClickable(true);
listview responds to item click and long click events.. so i could say it's a viewgroup..
but when you specify a listview to respond to click events like you've done you are telling the listview which is a view itself to respond to click events not its child..
and if you look at your code its
setOnItemLongClickListener or setOnItemClickListener respectively
not
setOnClickListener or setOnLongClickListener
so test it and let me know if it helps
Related
I'm trying to learn List view and adapters, made a very basic app to learn in which I have a button that inserts a String object to the data source of an array adapter and the other view is list view which shows this data set. add() and addAll() method apends the data to the data set but when I do that list view shows it but I didn't call notifyDataSetChanged method, so why list view refreshed itself and if it did so then why do I need the method? Could anyone help me by giving some clarity? add(), addAll() and insert() methods already making the list view show the new added data but how? so what's the difference?
the app's only layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="8dp">
<Button
android:id="#+id/insert_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="insert data"/>
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
the main activity code:
package famiddle.smart.apps.learningandroid;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Button insertButton;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> data = new ArrayList<>();
data.add("hello world");
ListView listView = findViewById(R.id.list_view);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
listView.setAdapter(adapter);
insertButton = findViewById(R.id.insert_button);
insertButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
adapter.add("hi there");
}
});
}
}
Below is the code for my first attempt at setting up a list fragment that clicks through to an activity (just as a test).
I've followed the Android docs example at: List Fragment Example
However, onListItemClick shows as "never used". When I run the app my list shows up, but when I click on the items in it I just get the following returned:
D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
The XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrationuser.piclo.ListViewFragment"
tools:showIn="#layout/activity_list_view"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="0dp"
>
<FrameLayout
android:id="#+id/list_container_id"
android:layout_width="0dp"
android:layout_height="0dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false" >
</ListView>
</FrameLayout>
</android.support.constraint.ConstraintLayout>
And here is the list fragment:
package com.example.administrationuser.piclo;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
public class ListViewFragment extends ListFragment {
public ListViewFragment() {}
String[] myStringArray = new String[]{"aa","bbb","ccccc","dddddd"};
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Populate list with our static array of titles.
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_activated_1, myStringArray));
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
showDetails(position);
}
void showDetails(int index) {
int mCurCheckPosition = index;
Intent intent = new Intent();
intent.setClass(getActivity(), MessageDetails.class);
intent.putExtra("Inty", mCurCheckPosition);
startActivity(intent);
startActivity(intent);
}
}
I have solved the problem.
It is trivial and would seem obvious to any seasoned Android developer, but here it is for the beginners out there who may fall into this trap....
...do not name your parent activity 'ListView'... it confuses the OnListItemClick override cause it doesn't know which class of ListView it should be overriding... the actual one or the one that you may have unfortunately created through the worst possible choice of name for your parent activity.
am developing an android application. My problem statement is as follows:
1) I need to have a ListView of courses available at some colleges.
2) I the points for each of them(e.g medicine:30,maths:24,English:14,French:28,etc...)
3) I need to parse the points(integer) to corresponding courses and lower courses than that.
Full sample:
Enter your points:(say integer 26 )(edittext)
After the person added his point he gets a list of courses he is likely to apply at the college like that:
You are eligible to apply for these courses:
maths
english(to be listed in as a listview)
P.s am using android studio.
can someone help me programmatically? thanks
mainactivity.java
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
String[] courses = new String[]{
"Mathematics",
"Chemical Engineering",
"Civil Engineering",
"Agriculture",
"Law and Management",
"Sociology",
"Information and Communication Technologies",
"Computer Science",
"Information Systems",
"Software Engineering",
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event2icon);
EditText editText = null;
editText=(EditText)findViewById(R.id.edit_text);
final String point=editText.getText().toString();
final int point_integer=Integer.parseInt(point);
final ListView courselist = (ListView)findViewById(R.id.courseNames);
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.listviewheader, courselist, false);
courselist.addHeaderView(header, null, false);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, courses);
courselist.setAdapter(adapter);
courselist.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
//we use the items of the listview as title of the next activity
if((Integer.parseInt(String.valueOf(point_integer))<=10))
{
}
}
});
}
}
event2icon.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/edit_text"
android:text="Enter your points:"/>
<ListView
android:id="#+id/courseNames"
android:layout_width="match_parent"
android:layout_below="#+id/edit_text"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:dividerHeight="15.0sp"
>
</ListView>
</LinearLayout>
listviewheader.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
</LinearLayout>
you need to parse points into integer and then by pass that integer to query to get list of eligible courses ;get point_integer from below code and use it into your query for database or arraylist and set aadapter to listview for eligible courses list related to that point
String point=edittext.getText().toString.trim();
int point_integer=Integer.parseInt(point);
I can not get the correct screen open.
I have the following code, and I also added the corresponding class in the manifest.
Please, if you can give me some idea.
Thank you.
listV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {
ListAdapter adp = (ListAdapter)parent.getAdapter();
//lista.get(position);
Seccion s = lista.get(Integer.parseInt(adp.getItem(position).toString()));
if (s.getTitle().contains("Estudiar")) {
//sel.setText("Seleccionado el del if"+ s.getTitle());
Intent i = new Intent(LiverpoolguideActivity.this,tipoListado.class);
startActivity(i);
}
}
});
The code tipoListado.class :
package jorgechu.com.liverpoolguide;
import java.util.ArrayList;
import jorgechu.com.liverpoolguide.Secciones.Seccion;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class tipoListado extends Activity {
public class LiverpoolguideActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listadotipo);
}
}
}
And the code screen xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/textViewListadoTipo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tipo:"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<ListView
android:id="#+id/listViewType"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Focusable views inside a ListView item will disable the ability to select ListView items. Applying android:focusable="false" to the TextView will allow OnItemClick to work again.
I have an application with tabs. In one tab I need to put data (strings) in rows. To do so I chose tableLayout but when I wanted to use a contextmenu on its rows it doesn't work.
I can show the contextmenu onLongClick but the problem is that I can't get the info about the selected row to edit or delete the selected row. Then I read in a discussion that using listView is better than tablelayout if we have many rows. But the examples I saw extend listactivity but I don't want to do this.
So when I try working on a listView without extending listactivity I don't know how to do it what I mean is that I've never used listView before so I try different examples I found on the internet to understand it but it's not working. Here's what I did so far for the listView:
String [] items=getRessources().getStringArray(R.arra.resolution);
//Resolution is an array of strings
ListView lv=(ListeView) findViewById(R.id.listView);
v.setAdapter(new ArrayAdapter<string>(this, android.R.layout.simple_list_item_1, items);
When I compile it I get a list with elements of my array in it but first, I want to change the color of text which I can't. And secondly I want to add rows dynamically to the list which I don't know how to do either. I think I have to use an adapter to do it but I don't know how.
Can someone please guide me through this. I just want to know how to attach my list to an adapter which'll allow me to dynamically add rows, add contextMenu etc.
main Activity class:
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
public class SelectedActivity extends Activity {
private SelectedAdapter selectedAdapter;
private ArrayList<String> list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selected_example);
// populate the model - a simple a list
list = new ArrayList<String>();
list.add("Apple");
list.add("Orange");
list.add("Grape");
list.add("Grape1");
list.add("Grape2");
list.add("Grape3");
list.add("Grape4");
list.add("Grape5");
list.add("Grape6");
// create our SelectedAdapter
selectedAdapter = new SelectedAdapter(this,0,list);
selectedAdapter.setNotifyOnChange(true);
ListView listview = (ListView) findViewById(R.id.listExample);
listview.setAdapter(selectedAdapter);
listview.setOnItemClickListener(new OnItemClickListener() {
//#Override
public void onItemClick(AdapterView arg0, View view,
int position, long id) {
// user clicked a list item, make it "selected"
selectedAdapter.setSelectedPosition(position);
}
});
}
Adapter class:
import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
public class SelectedAdapter extends ArrayAdapter{
// used to keep selected position in ListView
private int selectedPos = -1; // init value for not-selected
public SelectedAdapter(Context context, int textViewResourceId,
List objects) {
super(context, textViewResourceId, objects);
}
public void setSelectedPosition(int pos){
selectedPos = pos;
// inform the view of this change
notifyDataSetChanged();
}
public int getSelectedPosition(){
return selectedPos;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
// only inflate the view if it's null
// if (v == null) {
LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.selected_row, null);
// }
// get text view
TextView label = (TextView)v.findViewById(R.id.txtExample);
Button btn=(Button)v.findViewById(R.id.btn1);
// change the row color based on selected state
if(selectedPos == position){
label.setBackgroundColor(Color.CYAN);
btn.setBackgroundResource(R.drawable.next);
}
else{
label.setBackgroundColor(Color.WHITE);
}
label.setText(this.getItem(position).toString());
return(v);
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg8"
android:id="#+id/RootView"
>
<LinearLayout android:id="#+id/myLayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
</LinearLayout>
</LinearLayout>
You need to define an xml which will be used to hold data of each row:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weightSum="1">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="793dp"
android:layout_height="wrap_content"
android:layout_weight="0.23" >
<TextView android:id="#+id/col1"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:width="50dp"
android:textSize="18sp"
/>
<TextView android:id="#+id/col2"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:width="150dp"
android:textSize="18sp"
/>
<ImageView android:id="#+id/editimage"
android:background="#drawable/edit"
android:clickable="true"
android:onClick="ClickHandlerForEditImage"
android:layout_width="35dp"
android:layout_height="35dp"/>
</TableRow>
</LinearLayout>
In the above xml i have also included the ImageView, it is not really required but this is just to update you that we can include the other controls also.
& at the last you should have a function in your related class:
private void LoadData()
{
DBAdapter db = new DBAdapter(this);
db.open();
Cursor cur = db.GetData();
private ListView lv = (ListView)findViewById(R.id.myLayout);
lv.setAdapter(null);
if(cur.moveToFirst())
{
String[] from = new String[] {"_id","column1"};
int[] to = new int[] {R.id.col1, R.id.col2};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.grid_item, cur, from, to);
lv.setAdapter(adapter);
}
db.close();
}
}
It appears that noone has answered you contextMenu question. To get a context menu to work with your list , after you call ListView yourList = getListView(); you must call registerForContextMenu(yourList);
And to handle the menu creation you must implement the method
#Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
super.onCreateContextMenu(context, v,menuInfo);
MenuInflater inflater = getMenuInflater();
menu.setHeaderTitle("YOUR TITLE");
menu.setHeaderIcon(R.drawable.YOUR DRAWABLE);
inflater.inflate(R.menu.YOUR_MENU_RESOURCE, menu);
}
Then you can respond to clicks by implenting the method
#Override public boolean onContextItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.YOUR_MENU_ITEM: // do stuff if the item is selected
return true;
case R.id.YOUR_MENU_ITEM: // do stuff if the item is selected
return true;
case R.id.YOUR_MENU_ITEM: // do stuff if the item is selected
return true;
}
return false; // nothing selected
}