Multiple Items Selected on ListView, selects wrong items - android

I have this code for a ListView on a Fragment. My goal is to select multiple items on the list and to highlight them. My code does highlight the item, but when I scroll the list, more items are highlighted and when I scroll up again, more items are highlighted again and it goes crazy over and over highlighting and changing to default state, whenever I scroll the list. I don't know why it happens.
NOTE: I've tried CHOICE_MODE_SINGLE, CHOICE_MODE_MULTIPLE, CHOICE_MODE_MULTIPLE_MODAL and I also tried MultiChoiceModeListener but my App should be working on API above 9. So that's not a solution for me.
My Fragment (Don't want to use ListFragment)
public class InventarioFragment extends Fragment {
ListView listInventario;
public static InventarioFragment newInstance()
{
return new InventarioFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_inventario_list,container,false);
listInventario = (ListView) view.findViewById(R.id.list_inv);
listInventario.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
v.setBackgroundColor(Color.BLUE);
}
});
return view;
}
}
This is the XML of my Fragment (Notice that I am using an array for the items on the list).
My Fragment 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">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:weightSum="1">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5">
<ImageButton
android:id="#+id/ib_cancel_inventario"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/btn_cancel"
android:layout_centerInParent="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5">
<ImageButton
android:id="#+id/ib_submit_inventario"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/btn_ok"
android:layout_centerInParent="true"/>
</RelativeLayout>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="20dp"
android:background="#DF0101"/>
<ListView
android:id="#+id/list_inv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:entries="#array/list_inventario"
>
</ListView>
</LinearLayout>
</LinearLayout>
I'm not using and Adapter because I set the list items from a default array.
Sorry for my english, and I hope you can help me. (I can't upload images because I don't have enough reputation yet).

You can use the following adapter to achieve your goal.
Remove android:entries="#array/list_inventario" in your Listview and add choicemode as follows in your XML.
<ListView
android:id="#+id/list_inv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="multipleChoice">
</ListView>
InventarioFragment
public class InventarioFragment extends Fragment {
private Activity activity;
private ListView listInventario;
private String[] inventatioItems;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.activity=activity;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_inventario_list,container,false);
listInventario = (ListView) view.findViewById(R.id.list_inv);
inventatioItems=getResources().getStringArray(R.array.list_inventario);
InverntarioAdapter adapter=new InverntarioAdapter(activity);
listInventario.setAdapter(adapter);
listInventario.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int pos, long arg3) {
listInventario.setItemChecked(pos, true);;
}
});
return view;
}
private class InverntarioAdapter extends BaseAdapter{
private LayoutInflater inflator;
public InverntarioAdapter(Context context){
inflator=(LayoutInflater) context.getSystemService(activity.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return inventatioItems.length;
}
#Override
public Object getItem(int position) {
return inventatioItems[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
TextView tv;
if(convertView == null){
convertView=inflator.inflate(android.R.layout.simple_list_item_1, null);
convertView.setBackground(getResources().getDrawable(R.drawable.selector_background));
}
tv=(TextView) convertView.findViewById(android.R.id.text1);
tv.setText(inventatioItems[position]);
if(listInventario.isItemChecked(position)){
//background Color of selected items
convertView.setBackgroundColor(Color.BLUE);
}
else{
convertView.setBackgroundColor(Color.WHITE);
}
return convertView;
}
}
}

Related

GridView.setOnItemClickListener is not triggering

I am trying to add a feature where, whenever the user clicks on an editText to start typing in the last empty gridview item, a new empty gridview item will be added. The problem is that the listener doesn't get called when the user starts typing. I know there are some questions here about similar issues, but my gridView has an editText in it which is what I think the problem is.
Here is the listener I have:
roleLayout.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println(roleArrayList.get(position).getRole() + "******");
if(roleArrayList.get(position).getRole().equals("")) {
if (roleArrayList.size() == 1 ) {
roleArrayList.add(new NewRoleElement());
} else {
if (!roleArrayList.get(roleArrayList.size() - 1).getRole().equals("")) {
roleArrayList.add(new NewRoleElement());
}
}
}
}
});
And here is the xml code for my GridView:
<GridView
android:id="#+id/roleList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:numColumns="1"
android:stretchMode="columnWidth" />
Here is my xml code for the actual item:
<?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="48dp"
android:layout_rowWeight="1"
android:background="#drawable/my_layout_bg"
android:orientation="horizontal">
<EditText
android:id="#+id/roleText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10"
android:hint="Enter a new role"
android:inputType="textPersonName" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:text="Repeat?" />
</LinearLayout>
My adapter code is here:
public class RoleAdapter extends BaseAdapter{
Context c;
public RoleAdapter(Context context) {
this.c = context;
}
#Override
public int getCount() {
return roleArrayList.size();
}
#Override
public Object getItem(int position) {
return roleArrayList.get(position);
}
#Override
public long getItemId(int position) {
return roleArrayList.indexOf(position);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.role_template, null);
}
roleArrayList.get(position).setRoleInput((EditText) convertView.findViewById(R.id.roleText));
roleArrayList.get(position).setRepeatCheckBox((CheckBox) convertView.findViewById(R.id.checkBox));
return convertView;
}
}
The main problem is that EditText and CheckBox capture the focus
Try this in your code.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_rowWeight="1"
android:descendantFocusability="blocksDescendants"
android:background="#drawable/my_layout_bg"
android:orientation="horizontal">
......
</LinearLayout>
Add android:descendantFocusability="blocksDescendants" in the root xml code .
Another way
Also you can set android:focusable="false" in the EditText and set android:clickable="false" in the CheckBox.

Android Custom Spinner unable to select an option and show it

I have a problem with spinner it do not let me select one item. I tried a lot of things and that still not working.
The picture shows that the spinner is in blank when the activity load
When I clicked the arrow it shows the items
but when I choose one, nothing happends.
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".Activities.Inspeccion.DatosGeneralesActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
>
<TextView
android:id="#+id/tvSubestacionTitulo"
android:layout_below="#+id/imgLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/strSubestacion"
android:textSize="18sp"
android:textColor="#color/colorPrimaryDark"
android:textStyle="bold"
/>
<TextView
android:id="#+id/tvSubestacionDato"
android:layout_below="#+id/tvSubestacionTitulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="Prueba"
/>
<Spinner
android:id="#+id/spinnerSubEstacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvSubestacionDato"
>
</Spinner>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
This is the Layout of the activity.
<?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="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvNumeroOpcion"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:text="1"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/tvDescriptionOption"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:text="Guatemala"
android:textColor="#color/black"
android:textSize="14sp" />
</LinearLayout>
That is the custom layout for the spinner
Public class ComboAdapter extends BaseAdapter{
private List<Combo> combos;
private Activity activity;
private LayoutInflater inflater;
public ComboAdapter(List<Combo> combos, Activity activity) {
this.combos = combos;
this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return combos.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (convertView == null){
view = inflater.inflate(R.layout.combo_list_item, null);
TextView tvId = (TextView) view.findViewById(R.id.tvNumeroOpcion);
TextView tvDescripcion = (TextView) view.findViewById(R.id.tvDescriptionOption);
tvId.setText(combos.get(position).getId());
tvDescripcion.setText(combos.get(position).getDescripcion());
}
return view;
}
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getView(position, convertView,parent);
}
}
That is my Adapter
And below is my activity.
public class DatosGeneralesActivity extends AppCompatActivity {
private TextView tvSubestacionDato;
private List<Combo> listaCombo;
private Spinner spinnerSubestacion;
private ArrayAdapter<Combo> adapterSubestacion;
String seleccion;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_datos_generales);
//Inicializando textos
tvSubestacionDato = (TextView) findViewById(R.id.tvSubestacionDato);
//Inicializanco listas
listaCombo = new ArrayList<>();
//Inivializando spinners
spinnerSubestacion = (Spinner) findViewById(R.id.spinnerSubEstacion);
AppService service = API.getCombos().create(AppService.class);
Call<List<Combo>> subestacionCall = service.getSubestacion();
subestacionCall.enqueue(new Callback<List<Combo>>() {
#Override
public void onResponse(Call<List<Combo>> call, Response<List<Combo>> response) {
listaCombo.clear();
listaCombo.addAll(response.body());
}
#Override
public void onFailure(Call<List<Combo>> call, Throwable t) {
}
});
//final ComboAdapter adapter = new ComboAdapter(listaCombo, DatosGeneralesActivity.this);
final ArrayAdapter<Combo> adapter = new ArrayAdapter<Combo>(this, R.layout.support_simple_spinner_dropdown_item, listaCombo);
spinnerSubestacion.setAdapter(adapter);
spinnerSubestacion.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
adapter.notifyDataSetChanged();
Toast.makeText(DatosGeneralesActivity.this, ""+position, Toast.LENGTH_SHORT).show();
tvSubestacionDato.setText(listaCombo.get(position).getDescripcion());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
Try this changes:
Call adapter like:
ComboAdapter adapter = new ComboAdapter(DatosGeneralesActivity.this,
R.layout.combo_list_item, R.id.tvDescriptionOption, listaCombo);
now in adapter class:
public ComboAdapter(Activity context,int resouceId, int textviewId, List<Combo> list){
super(context,resouceId,textviewId, list);
this.combos = list;
this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
Also inside your getView() method inflate layout like:
if (convertView == null){
view = inflater.inflate(R.layout.combo_list_item, parent , false);

What should i use to create custom ListView in Fragment to suit my need?

I've been creating News application. I used this tutorial to create good-looking Navigation Drawer. It uses fragments for every category in Navigation List. I will be using provided API to get news (in the JSON format), and i need customized List View to show the list of news. When you click list item, it will open new view to show details of news. I've found several tutorials, but none of them worked for fragment. Please, tell me way to do it.
I'm new to Android Programming. This is my first project, though it is too hard for being first project. But i have to do it. Please, help!
Thanks beforehand!
Assuming you know how to create an adapter and a custom row layout from a tutorial that works for activities. Minor modifications will help you using fragments.
Your fragment should look like this:
public class NewFragment extends Fragment{
CustomAdapter listAdapter;
ListView listView;
ArrayList<Data> yourData; // Should be filled with data from your JSONParser;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
/*
* Use your custom adapter. in this example the adapter need
* the context and an array that contains your data
*/
listAdapter = new CustomAdapter(getActivity(),yourData);
...
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.new_fragment, container, false);
listView = (LsitView)view.findViewById(R.id.your_list);
listView.setAdapter(listAdapter);
...
}
}
You can use for this ListFragment:
Here is a fully customized example:
activity_main.xml
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/frameLayout"/>
</RelativeLayout>
mylist.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">
<ListView android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
myitem.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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:id="#+id/txt_mytext" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment fr = mListFragment.newInstance();
FragmentManager fm = getFragmentManager();
fm.beginTransaction().add(R.id.frameLayout, fr, "tag").commit();
}
}
mListFragment.java
public class mListFragment extends ListFragment {
public static mListFragment newInstance() {
mListFragment f = new mListFragment();
//Bundle args = new Bundle();
//f.setArguments(args);
return f;
}
public mListFragment() {}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String[] strings = {"a", "b", "c"};
ArrayAdapter<String> adapter = new LstAdapter(
getActivity(), R.layout.myitem,
strings);
setListAdapter(adapter);
return inflater.inflate(R.layout.mylist, null);
}
public class LstAdapter extends ArrayAdapter<String> {
private String[] mArray;
public LstAdapter(Context context, int textViewResourceId, String[] mList) {
super(context, textViewResourceId, mList);
mArray = mList;
}
#Override
public int getCount() {
return mArray.length;
}
#Override
public String getItem(int position) {
return mArray[position];
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.myitem, null);
}
TextView tvName = (TextView) v.findViewById(R.id.txt_mytext);
tvName.setText(mArray[position]);
return v;
}
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.d("123", position + "");
super.onListItemClick(l, v, position, id);
}
}

How to have android longClick event to textView and not in the editText

I try to implement a longClick in the my list view but I get longClick only in the edit text field: how I can get longClick only over the textView?
Thanks, Daniele
Other details:
the main layout is "activity_main.xml" and in the list view "listView_listData" I put the "row.xml" layout with "RigaAdvAdapter" class;
the layout "row.xml" has two field: textView and editText;
I used a ArrayList to store the data that I want to view in the rows (separated by commas - Ex. "viewTextValue,editTextValue");
I need to get longClick in the each row to offer more options;
I dont't need the longClick in the editText otherwise the copy-paste not work;
In the future I'll need more data, and so I'll use an array of ArrayList;
######################### LAYOUT: activity_main.xml
<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="wrap_content"
android:layout_below="#+id/textView1"
android:layout_toRightOf="#+id/textView1"
android:orientation="vertical" >
<ListView
android:id="#+id/listView_listData"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:listSelector="#drawable/list_selector"
android:fadingEdge="none" >
</ListView>
</LinearLayout>
######################### LAYOUT: row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:shrinkColumns="*" android:stretchColumns="*"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:id="#+id/RelativeLayout_rowData"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textViewField"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:padding="3dip"
android:layout_width="0dp"
android:textColor="#666666"
android:focusable="false" />
<EditText
android:id="#+id/editTextField"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:inputType="text">
<requestFocus />
</EditText>
</TableRow>
</TableLayout>
MainActivity
public class MainActivity extends Activity
{
ArrayList<String> arrayDataSource= new ArrayList<String>();
...
I fill the array "arrayDataSource" with a string as "text,another text"
...
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView_listData =(ListView)findViewById(R.id.listView_listData);
listView_listData.setAdapter(new RigaAdvAdapter());
listView_listData.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int pos, long arg3)
{
mostraToast("hh long click"+String.valueOf(pos));
return true;
}
});
}
Adapter class
public class RigaAdvAdapter extends BaseAdapter
{
#Override
public int getCount() {return arrayDataSource.size();}
#Override
public Object getItem(int position) {return position;}
#Override
public long getItemId(int position) {return position;}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row, null);
}
TextView textViewField = (TextView)convertView.findViewById(R.id.textViewField);
EditText editTextField = (EditText)convertView.findViewById(R.id.editTextField);
String StringData=arrayDataSource.get(position);
String[] ArrayStringData=StringData.split(",",-1);
String textViewField_val=ArrayStringData[0];
String editTextField_val=ArrayStringData[1];
textViewField.setText(textViewField_val);
editTextField.setText(editTextField_val);
return convertView;
}
}
Instead of setting longclick to ListView you have to set it for TextView.In RigaAdvAdapter class you have to add this
textViewField.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return false;
}
});
Try to set you edittext property set clickable and focusable to false. As you have added Editttext so it will gets the first focus and touch event always.
And to add the Longclick event on TextView you will have to implement the longclick listener inside your adapter class.
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row, null);
}
TextView textViewField = (TextView)convertView.findViewById(R.id.textViewField);
EditText editTextField = (EditText)convertView.findViewById(R.id.editTextField);
textViewField.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return false;
}
});
String StringData=arrayDataSource.get(position);
String[] ArrayStringData=StringData.split(",",-1);
String textViewField_val=ArrayStringData[0];
String editTextField_val=ArrayStringData[1];
textViewField.setText(textViewField_val);
editTextField.setText(editTextField_val);
return convertView;
}
You should use onItemLongClickListener inside getView method of the adapter. This question can help Android : How to set onClick event for Button in List item of ListView

Listview listener not getting called

I have button, which on clicked displays a listview and on clicking the listview item it must print something on logcat. I implemented this one in such a way that the list view and its listener are present inside the button onclick listener.
All is working well except the listview listener is not getting called(i.e. nothing is printing on logcat when clicked on listview item)
Here is my listener.
public class MyClickListener implements View.OnClickListener {
LinearLayout parent;
LayoutInflater layoutInflater;
Context context;
Home home;
#Override
public void onClick(View view) {
parent = (LinearLayout) home.findViewById(R.id.main_view);
View childLayout = layoutInflater.inflate(R.layout.mylayout, (ViewGroup) view.findViewById(R.id.list_layout));
parent.addView(childLayout, 0);
LinearLayout layout = (LinearLayout) home.findViewById(R.id.main_view);
ListView list = (ListView) layout.findViewById(R.id.list);
list.setAdapter(new MyListAdapter(context));
lsit.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
System.out.println("hello Android");
}
});
}
}
mylayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list"
android:padding="15dp"
android:divider="#android:color/transparent"
android:dividerHeight="5dp"
android:listSelector="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"/>
</LinearLayout>
Home.java
public class Home extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Button button= (Button) findViewById(R.id.but);
button.setOnClickListener(new MyClickListener(this,this,layoutInflater));
}
}
home.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#color/background">
...
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:id="#+id/main_view">
</LinearLayout>
.....
Can anyone help
Edit 1
MyListAdapter.java
private class MyListAdapter extends BaseAdapter {
String[] items;
LayoutInflater inflater;
Context context;
public MyListAdapter(Context context) {
this.context = context;
Resources resources = context.getResources();
items = new String[]{resources.getString(R.string.q1), resources.getString(R.string.q2), resources.getString(R.string.q3)};
}
#Override
public int getCount() {
return items.length;
}
#Override
public Object getItem(int i) {
return i;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View view1 = view;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null)
view1 = inflater.inflate(R.layout.list_layout_item, null);
TextView question = (TextView) view1.findViewById(R.id.itemtext);
question.setText(items[i]);
return view1;
}
}
list_layout_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#style/itemStyle">
<TextView
android:id="#+id/itemtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:drawableRight="#drawable/disclosure_indicator"/>
</LinearLayout>
Remove this code from your onClick() method and implement outside of that onClick() method. Just set Adapter there, but implementation is wrong og onItemClickListener().
lsit.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
System.out.println("hello Android");
}
});
Make sure that in your list_item_layout doesn't have any element is stealing event, like button or TextView with setClickable(true).

Categories

Resources