ListFragment OnItemClickListener - java

I have a list fragment displaying my custom list items which consist of a relative layout containing images and text etc.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/main_background"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingRight="20dp"
android:paddingBottom="10dp"
android:descendantFocusability="blocksDescendants" >
<ImageView
android:id="#+id/source_image"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#color/black"
android:layout_alignParentLeft="true"
android:contentDescription="#string/info" />
<TextView
android:id="#+id/item_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:textStyle="bold"
android:layout_toRightOf="#+id/source_image"
android:textColor="#color/black"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:textAlignment="center"
android:text="#string/item_desc" />
<ImageView
android:id="#+id/arrow_image"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="30dp"
android:layout_alignParentRight="true"
android:contentDescription="#string/info"
android:src="#drawable/arrow_right" />
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:layout_centerInParent="true"
android:background="#color/main_background"
android:layout_below="#+id/item_description">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:layout_gravity="fill_horizontal"
android:layout_marginRight="20dp"
android:padding="5dp"
android:orientation="horizontal" >
<ImageView
android:id="#+id/item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/info"
android:src="#drawable/chameleon" />
<ImageView
android:id="#+id/item_image2"
android:paddingLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/info"
android:gravity="center"
android:src="#drawable/chameleon" />
<ImageView
android:id="#+id/item_image3"
android:paddingLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/info"
android:gravity="center"
android:src="#drawable/chameleon" />
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
I am trying to set up an OnItemClickListener so when the user clicks anywhere on the list item, a new Intent is started.
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//Populate list view with array objects
adapter = new HomeListItemAdapter(items, getActivity());
listView = getListView();
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast toast = Toast.makeText(listView.getContext(), "CLICKED", Toast.LENGTH_SHORT);
toast.show();
}
});
}
I am attempting this as shown above but can't seem to get the OnItemClickListener to work (just testing with Toast instead of Intent for now). Where am I going wrong?
Thanks
EDIT: Added custom list adapter code
public class HomeListItemAdapter extends BaseAdapter {
private List<HomeListItem> items;
private Context context;
private int numItems = 0;
public HomeListItemAdapter(final List<HomeListItem> items, Context context) {
this.items = items;
this.context = context;
this.numItems = items.size();
}
public int getCount() {
return numItems;
}
public HomeListItem getItem(int position) {
return items.get(position);
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Get the current list item
final HomeListItem item = items.get(position);
// Get the layout for the list item
final RelativeLayout itemLayout = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
// Set the the item picture(s)
ImageView itemImage = (ImageView) itemLayout.findViewById(R.id.item_image);
itemImage.setImageDrawable(item.getItemPic());
//Set the profile picture / source picture
ImageView sourceImage = (ImageView) itemLayout.findViewById(R.id.source_image);
sourceImage.setImageDrawable(item.getSourcePic());
// Set the text label as defined in our list item
TextView itemDesc = (TextView) itemLayout.findViewById(R.id.item_description);
AssetManager am = context.getAssets();
Typeface font = Typeface.createFromAsset(am, "Raleway-Thin.ttf");
itemDesc.setTypeface(font);
itemDesc.setText(item.getText());
return itemLayout;
}
}

Try extending ListFragment (instead of just a normal Fragment). Then you can override onListItemClicked:
public void onListItemClick(ListView l, View v, int pos, long id)
This gives you the list, view, position, and id, so you should be able to do whatever you need.

Could you try setting the HozizontalScrollView to not get focus with the XML attribute
android:focusable="false"

If you have a TextView inside the row then that will take focus when the row is clicked. This will mean that onListItemClick is not called. You either need to have an OnClickListener on the TextView or remove focus from the TextView.
See ListFragment OnListItemClick not being called

Try this sample project for costum ListFragment. It is a very good example with a costum ListFragment showing 2 types of views for phone and tablets.
http://s000.tinyupload.com/index.php?file_id=09444774931653041243
Courtsey
http://jcrazyandroider.blogspot.in/2015/01/download-links.html

Related

ListView doesn't display on screen

I recently started to use ListView on Android Application and I'm not making it to display at the screen.
I saw many tutorials on how to do a custom Adapter and followed it line by line, but at the end I wasn't able to make the ListView display on my screen, I even let a fixed Text in one TextView to display at the ListView but it still didn't displayed it.
This is the constructor of my entity code:
public Classificacao(int colocacao, int time, int pontos) {
this.colocacao = colocacao;
this.time = time;
this.pontos = pontos;
}
This my Custom Adapter that extends "ArrayAdapter"
private Context context;
private ArrayList<Classificacao> classificacaoList;
ClassificacaoAdapter(Context context, ArrayList<Classificacao> classificacaoList) {
super(context, R.layout.layout_classificacao, classificacaoList);
this.context = context;
this.classificacaoList = classificacaoList;
}
#SuppressLint("SetTextI18n")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null){
convertView = layoutInflater.inflate(R.layout.layout_classificacao, parent, false);
}
TextView colocacao = convertView.findViewById(R.id.texto1);
colocacao.setText(Integer.toString(classificacaoList.get(position).getColocacao()));
ImageView imagem = convertView.findViewById(R.id.imagem1);
imagem.setImageResource(classificacaoList.get(position).getImagem());
TextView nome = convertView.findViewById(R.id.texto2);
nome.setText(classificacaoList.get(position).getTime());
TextView pontos = convertView.findViewById(R.id.texto3);
pontos.setText(Integer.toString(classificacaoList.get(position).getPontos()));
return convertView;
}
This the layout "activity_atividade02" that have the ListView, and some others Text views that I won't display here.
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
This is the layout "layout_classificacao" that will be filled by the adapter
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20dp">
<TextView
android:id="#+id/texto1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:textColor="#android:color/black"
android:textSize="30sp" />
<ImageView
android:id="#+id/imagem1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_weight="1"
android:contentDescription="imagem do time"
app:srcCompat="#drawable/flamengo" />
<TextView
android:id="#+id/texto2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:textColor="#color/colorPrimary"
android:textSize="30sp" />
<TextView
android:id="#+id/texto3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:textColor="#android:color/black"
android:textSize="30sp" />
</LinearLayout>
And this is the main class "Atividade02"
that start everything
public class Atividade02 extends AppCompatActivity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_atividade02);
listView = findViewById(R.id.listView);
ArrayList<Classificacao> classificacaoArrayList = new ArrayList<>();
classificacaoArrayList.add(new Classificacao(1, 2,46));
classificacaoArrayList.add(new Classificacao(2, 1,42));
classificacaoArrayList.add(new Classificacao(3,0,38));
classificacaoArrayList.add(new Classificacao(4,3,35));
classificacaoArrayList.add(new Classificacao(5,5,33));
classificacaoArrayList.add(new Classificacao(6,4,33));
ArrayAdapter classificacaoAdapter = new ClassificacaoAdapter(this, classificacaoArrayList);
listView.setAdapter(classificacaoAdapter);
}
}
Resuming, the ListView isn't displaying the "ClassificacaoAdapter" and I want it to do it.
Make sure adapter has the method getItemcount() and it returns the classificacaoList.size(). It should not return 0;
Well... it was a pretty obvious problem, in the main layout activity_atividade02 there was a LinearLayout above the ListView that it's layout_height was set to match_parent and it was occupying the entire layout and not letting the ListView being displayed.
I test your code as you write and found an error here and it works normally but I comment imageView line because you don't define it in Classificacao class
an error here
TextView nome = convertView.findViewById(R.id.texto2);
nome.setText(classificacaoList.get(position).getTime());
setText() accept String only ..but you pass integer rather than String you can do that
nome.setText(""+classificacaoList.get(position).getTimee());
or use
Integer.toString( classificacaoList.get(position).getTimee() )
as you use in other lines
please add getCount() method in your ClassificacaoAdapter class, it is an override method. It will return the size of the list,if you will not override this it will return 0 as your list size.
public int getCount() {
return classificacaoList .size();
}

long click triggers click, long click doesn't get registered

I have a listview in a constraintlayout.
when I try to do a long click on one of the elements of the list, to inflate the context menu, there are 2 problems:
1: It triggers the long click only sometimes, really random
2: After the long click was triggered the "normal" click is also triggered even though the onContextItemSelected returns true (to indicate the event was handled)
for some list elements, I want to have both the onClickListener and the long click for the contextmenu, on others only the contextmenu. (the listview is registered for contextmenu).
Heres the XML of the MainActivity
<?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="it.netknights.piauthenticator.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:backgroundTint="#color/PIBLUE"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:srcCompat="#drawable/ic_add_white_24dp" />
<TextView
android:id="#+id/countdownfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:backgroundTint="#color/PIBLUE"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/PIBLUE"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:popupTheme="#color/PIBLUE"
tools:layout_editor_absoluteY="-57dp" />
<ListView
android:id="#+id/listview"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="#+id/countdownfield"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar"
app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>
and the XML of the listentry
<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"
android:longClickable="true"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<TextView
android:id="#+id/textViewToken"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="124891"
android:textSize="28sp"
android:textStyle="bold"
tools:layout_editor_absoluteY="0dp"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="parent" />
<!-- android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"-->
<TextView
android:id="#+id/textViewLabel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="privacyidea something"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginTop="0dp"
app:layout_constraintTop_toBottomOf="#+id/textViewToken"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="parent" />
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="0dp"
app:layout_constraintTop_toBottomOf="#+id/textViewLabel"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.517"
/>
</android.support.constraint.ConstraintLayout>
the part of the MainActivity.java
final ListView listview = (ListView) findViewById(R.id.listview);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(view.getContext(), "SHORT itemclick", Toast.LENGTH_SHORT).show();
}
});
registerForContextMenu(listview);
and the getview method from my custom adapter:
#Override
public View getView(final int position, View v, ViewGroup parent) {
if (v == null) {
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
v = inflater.inflate(R.layout.tokenentry, parent, false);
}
v.setLongClickable(true);
v.setTag(position);
final ProgressBar progressBar = (ProgressBar) v.findViewById(R.id.progressBar);
final Token token = getItem(position);
final TextView tmp2 = (TextView) v.findViewById(R.id.textViewToken);
final TextView tmp1 = (TextView) v.findViewById(R.id.textViewLabel);
if (token.getType().equals(HOTP)) {
progressBar.setVisibility(GONE);
} else {
progressBar.setVisibility(VISIBLE);
//v.setClickable(false);
}
progressBar.setTag(position);
progressBar.setMax(token.getPeriod());
progressBar.getProgressDrawable().setColorFilter(
Color.rgb(0x83, 0xc9, 0x27), android.graphics.PorterDuff.Mode.SRC_IN);
token.setPb(progressBar);
tmp1.setText(token.getLabel());
tmp2.setText(token.getCurrentOTP());
EDIT: thank you for taking your time to post answers.
Ive solved the/my problem: the rootelement of the listentries was a contraintlayout and that doesn't seem to work properly with the scenarios i want, so i changed it so a relativelayout and it now it works perfectly!
hi Cxc the problem might be due to listView registered for both onClick and onLongClick . So instead of setting the onClickListener for the entire list view try setting the onClick listener to only the item view for which you need both onClickListener and long click.
You can set onClickListener to particular views inside getView() method based on their position or view type.
for example,
#Override
public View getView(final int position, View v, ViewGroup parent) {
if (v == null) {
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
v = inflater.inflate(R.layout.tokenentry, parent, false);
}
v.setLongClickable(true);
v.setTag(position);
if(position % 2 == 0)
{
v.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(v.getContext(), "SHORT itemclick",
Toast.LENGTH_SHORT).show();
}
});
hope this helps
I think you can use both onClick and onLongClick and override the onLongClick method making it returns true to avoid conflict of triggering onClick event
ex:
GridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
<do somthing here>
}
});
GridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, final View view, final int i, long l) {
<do something here>
return true;
}
});

Want to open WebView in each list item click?

I Want to open a static HTML file in each click of List item but when i call my listview is disappeared . please help me to short out this problem.
My Tab1Fragment.java Code is this
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tab1, container, false);
Resources res = getResources();
mainTopic = res.getStringArray(R.array.MainTopic);
mainDescription = res.getStringArray(R.array.DescriptionContent);
listView = (ListView) view.findViewById(R.id.listView);
MyAdapter myAdapter = new MyAdapter(getActivity(), mainTopic, image1,
mainDescription);
listView.setAdapter(myAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
wb=(WebView)view.findViewById(R.id.webView);
wb.loadUrl("file:///assets/ActivePassive.html");
wb.getSettings().setJavaScriptEnabled(true);
wb.setWebViewClient(new WebViewClient());
}
}
});
return view;
}
}
My fragment_tab1.xml code is :-
<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="androidthirst.company.abhi.totalenglish.Tab1Fragment">
<!-- TODO: Update blank fragment layout -->
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
And my separate listView code is this
single_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="50dp"
android:layout_height="50dp"
android:background="#1A237E"
android:gravity="center"
android:textStyle="bold"
android:textSize="30sp"
android:layout_margin="9dp"
android:textColor="#ffffff"
android:id="#+id/imageView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/imageView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/textView2"
android:layout_toRightOf="#+id/imageView"
android:layout_below="#+id/textView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#+id/imageView" />
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView">
</WebView>
</RelativeLayout>
Here you can see the image i just want to open thml file in first list Item Click
Try Bellow code:
list_Id.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
list_Id.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
WebView webView = new WebView(v.getContext());
String[] urls = getResources().getStringArray(R.array.bookmark_urls);
webView.loadUrl(urls[position]);
}
});
}
});
Create a file string-array.xml inside values and put all web addresses as bellow:
<resources>
<string-array name="bookmark_urls">
<item>http://www.google.com</item>
<item>http://www.android.com/</item>
<item>http://www.facebook.com/</item>
<item>http://www.android.com/</item>
<item>http://www.android.com/</item>
</string-array>
</resources>
you can use something like below
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
Your layout is "fragment_tab1"
View view = inflater.inflate(R.layout.fragment_tab1, container, false);
While you wrot layout name "My tab1_fragment.xml code is :-"
please check is that exact layout?
And your Webview is in Row file which used in adapter class.. (single_row.xml)
You tried to load in activity..
Thanks abhi..

Android ListView OnClickListener

I need to add a code responsible for action when one row from my list is clicked. I don't know if it should be OnItemClickListener or OnClickListener and how&where to write it. My app is with view holder. Here is my code:
public class JobListAdapter extends ArrayAdapter<String> {
private LayoutInflater mInflater;
public static class WorkViewHolder {
public TextView mJob;
public ImageView mImageAndroKorpo;
}
public JobListAdapter(Context mContext, List<String> mDane) {
super(mContext, R.layout.list_element_job, mDane);
this.mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
WorkViewHolder mHolder;
if(convertView == null) {
convertView = mInflater.inflate(R.layout.list_element_job, parent, false);
mHolder = new WorkViewHolder();
TextView mJobsName = (TextView) convertView.findViewById(R.id.nazwa_oferty);
ImageView mImageAndroKorpo = (ImageView)convertView.findViewById(R.id.list_image);
mHolder.mJob = mJobsName;
mHolder.mImageAndroKorpo = mImageAndroKorpo;
convertView.setTag(mHolder);
} else {
mHolder = (WorkViewHolder)convertView.getTag();
}
final String mWorkPosition = getItem(position);
mHolder.mJob.setText(mWorkPosition);
mHolder.mJob.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Here?
}
});
return convertView;
}
}
I added code where I think it should be placed. Is it ok? OnItem or OnClick? And how to use item position?
My list_element_job.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:padding="10dp"
<CheckBox
android:id="#+id/list_checkbox"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/list_image"
android:src="#drawable/android_white_piece"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_width="1dp"
android:layout_height="1dp"/>
<TextView
android:text=""
android:background="#drawable/android_korpo_transparent3"
android:textStyle="bold"
android:textColor="#android:color/black"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:gravity="left"
android:layout_toLeftOf="#id/list_image"
android:layout_width="0dp"
android:textSize="7pt"
android:layout_height="wrap_content"
android:id="#+id/nazwa_oferty"/>
<TextView
android:text="Details..."
android:background="#android:color/white"
android:clickable="true"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_below="#id/nazwa_oferty"
android:textSize="6pt"
android:textColor="#android:color/darker_gray"/>
</RelativeLayout>
Please help me somehow :)
Just remove the focus for the checkbox in xml like below:
<CheckBox
android:id="#+id/list_checkbox"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
because of check box focus list item click listeners wont work.
Just remove onClick from your adapter class. And add OnItemClickListener to your ListView
example:
JobListAdapter jobListAdapter = new JobListAdapter (...);
listView.setAdapter(jobListAdapter);
listView..setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
});

how to get gettext of a text view in getview method?

I have a listview populating using cursor in which custom row exist. There are set of textviews in the row. I also have a image view in the row. I would like to set an image based on a value of the textview. My code is
public class DisplayCards extends Activity {
private Divyadesamdb dbHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pasuram);
dbHelper = new Divyadesamdb(this);
dbHelper.open();
// Clean all data
dbHelper.deleteAllPasurams();
// Add some data
dbHelper.insertPasurams();
Intent intent = getIntent();
String category = intent.getStringExtra(MainActivity.EXTRA_CATEGORY);
String start = intent.getStringExtra(MainActivity.EXTRA_START);
String ending = intent.getStringExtra(MainActivity.EXTRA_ENDING);
// Generate ListView from SQLite Database
displayListView(category, start, ending);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void displayListView(String mcategory, String mstart, String mending) {
String category = mcategory;
String starting = mstart;
String ending = mending;
int rowsFound = 0;
Cursor cursor = dbHelper.fetchAllPasurams(category, starting, ending);
/*
* How many rows returned from the query
*/
rowsFound = cursor.getCount();
// the desired columns to be bound
String[] columns = new String[] {
Divyadesamdb.PASURAMS_COLUMN_PAASURAMNUMBER,
Divyadesamdb.PASURAMS_COLUMN_AAYIRAM,
Divyadesamdb.PASURAMS_COLUMN_AZHWAAR,
Divyadesamdb.PASURAMS_COLUMN_CATEGORY,
Divyadesamdb.PASURAMS_COLUMN_MANGALASASANAMON,
Divyadesamdb.PASURAMS_COLUMN_PAASURAM_EN_STR,
Divyadesamdb.PASURAMS_COLUMN_SUBCATEGORY,
Divyadesamdb.PASURAMS_COLUMN_TITLE,
Divyadesamdb.PASURAMS_COLUMN_TITLENUMBER };
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.tvPaasuramNumber, R.id.tvAayiram,
R.id.tvAzhwaar, R.id.tvCategory, R.id.tvMangalasasanamOn,
R.id.tvPaasuram, R.id.tvSubCategory, R.id.tvTitle,
R.id.tvtvTitleNumber };
// create the adapter using the cursor pointing to the desired data
// as well as the layout information
MyCursorAdapter dataAdapter = new MyCursorAdapter(this,
R.layout.paasuram_single_item, cursor, columns, to, 0);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(dataAdapter);
}
// extend the SimpleCursorAdapter to create a custom class where we
// can override the getView to change the row colors
private class MyCursorAdapter extends SimpleCursorAdapter {
public MyCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// get reference to the row
View view = super.getView(position, convertView, parent);
// check for odd or even to set alternate colors to the row
// background
// want to get the value of this text view,
// but not getting handle.
LayoutInflater inflater = ((Activity) Context).getLayoutInflater();
TextView azhwaarView = (TextView) view.getTag(R.id.textViewAzhwaar);
String azhVal = azhwaarView.getText().toString();
if (position % 2 == 0) {
view.setBackgroundColor(Color.rgb(238, 233, 233));
} else {
view.setBackgroundColor(Color.rgb(255, 255, 255));
}
return view;
}
}
}
not sure how to get the handle of textview. Getting Null pointer exception. Any help is greatly appreciated.
paasuram_single_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/mybackground"
android:orientation="vertical" >
<ImageView
android:id="#+id/ivAzhwaar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="false"
android:layout_margin="10dp"
android:contentDescription="#string/Azhwaar"
android:padding="5dp"
android:src="#drawable/nammalwar" />
<TextView
android:id="#+id/tvtvTitleNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/ivAzhwaar"
android:layout_toRightOf="#+id/ivAzhwaar"
android:text="TitleNumber"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/tvHyphan1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tvtvTitleNumber"
android:layout_alignBottom="#+id/tvtvTitleNumber"
android:layout_toRightOf="#+id/tvtvTitleNumber"
android:text=" "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBaseline="#+id/tvHyphan1"
android:layout_alignBottom="#+id/tvHyphan1"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/tvHyphan1"
android:text="Title"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/tvAayiram"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvtvTitleNumber"
android:layout_below="#+id/tvtvTitleNumber"
android:text="Aayiram"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvHyphan2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvtvTitleNumber"
android:layout_toRightOf="#+id/tvAayiram"
android:text=" - "
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tvHyphan2"
android:layout_alignBottom="#+id/tvHyphan2"
android:layout_toRightOf="#+id/tvHyphan2"
android:text="Category"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvHyphan3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tvCategory"
android:layout_alignBottom="#+id/tvCategory"
android:layout_toRightOf="#+id/tvCategory"
android:text=" - "
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvSubCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tvHyphan3"
android:layout_alignBottom="#+id/tvHyphan3"
android:layout_toRightOf="#+id/tvHyphan3"
android:text="SubCategory"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvPaasuram"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvAayiram"
android:layout_below="#+id/tvAayiram"
android:padding="5dp"
android:text="paasuram"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvPaasuramNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tvPaasuram"
android:layout_alignBottom="#+id/tvPaasuram"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="PaasuramNumber"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/tvAzhwaar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tvMangalasasanamOn"
android:layout_alignBottom="#+id/tvMangalasasanamOn"
android:layout_alignLeft="#+id/tvPaasuram"
android:text="Azhwaar"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvMangalasasanamOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvCategory"
android:layout_below="#+id/tvPaasuram"
android:layout_centerHorizontal="true"
android:text="MangalasasanamOn"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
Only two errors are their:
1).You are not inflating any view here:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
View view = inflater.inflate(R.layout.custom_inflator_layout,parent,false);
TextView azhwaarView = (TextView) view.getTag(R.id.textViewAzhwaar);
String azhVal = azhwaarView.getText().toString();
return view;
}
2).You are not seting any text to textview ,thats y when you trying to get the text
it gives null pointer.
In order to resolve the problem I have done the following:
created a table column with int datatype
got the R.drawable.xxx and stored the int to the database
retrieved the data and set to the image in the row level.
Thanks for all the help! Appreciate every help rendered. Big thanks once again.

Categories

Resources