getting text of custom listview item on its click - android

I have a custom layout for the ListView which is as follows -
<?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="vertical"
android:background="#drawable/plain" >
<TextView
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
android:textSize="16sp"
android:textColor="#000000"
android:paddingRight="10dip"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
When I click the item in a listview, I am getting error -
02-28 17:37:38.240: E/AndroidRuntime(20869): FATAL EXCEPTION: main
02-28 17:37:38.240: E/AndroidRuntime(20869):
java.lang.ClassCastException: android.widget.LinearLayout cannot be
cast to android.widget.TextView
This is because now I am using custom listview layout as above which is wrapped in a linearlayout.
The error line is-
class ListItemClickedonPaired implements OnItemClickListener
{
#SuppressLint("NewApi")
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
final String info = ((TextView) view).getText().toString(); //ERROR HERE
}
}
My question is-
How to get the text of the clicked item (technically a textview) when we have these as a custom items of a listview defined with above xml ?

Try
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
TextView tv = (TextView)view.findViewById(android.R.id.text1);
String info = tv.getText().toString();
}
You inflate a LinearLayout for each row. What you are doing is casting LinearLayout to TextView.
You can also use setTag and getTag
You have used a id from the android framework. So it should be android.R.id.text1 not R.id.text1 coz you have android:id="#android:id/text1". It is a mistake which i have edited.
http://developer.android.com/reference/android/R.id.html
public static final int text1
Added in API level 1
Constant Value: 16908308 (0x01020014

Instead of writing a text view inside a Linear layout make the TextView as the root Tag so that the view argument in the OnItemClick() returns the reference of text view, Now you can directly typecast the view object to the TextView.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
android:textSize="16sp"
android:textColor="#000000"
android:paddingRight="10dip"
android:textAppearance="?android:attr/textAppearanceLarge" />

Try this :
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
switch(view.getId()) {
case R.id.text1:
final String info = ((TextView) view).getText().toString();
break;
}
}

Related

onItemClick() receives layout instead of TextView

I have a ListView in my Activity whose rows are populated by logins fetched from a database. Each row in my ListView contains one TextView inside a Constraint Layout. When user touches one of the rows of this ListView, another activity should be launched. I used OnItemClickListener to achieve this and everything seems to work fine, but onClickItem() method from Listener receives a reference to object of type ConstraintLayout and I need to obtain text from TextView inside this layout. How can I do this?
Here is my xml of a simple row:
<?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">
<TextView
android:id="#+id/row"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:text="TextView"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
And here is my implementation of OnItemClickListener:
private AdapterView.OnItemClickListener myItemClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView currentView = (TextView) view; // Here I get an exception that ConstraintLayout cannot be casted to TextView
String login = currentView.getText().toString();
Intent intent = new Intent(Powitanie.this,UserPanel.class);
intent.putExtra(LOGIN, login);
Powitanie.this.startActivity(intent);
}
};
We can use getChildAt() method. onItemClick() receives reference to a view. We can cast it to ConstraintLayout and get a child from a specified index.
private AdapterView.OnItemClickListener myItemClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ConstraintLayout layout = (ConstraintLayout) view;
TextView currentView = (TextView) layout.getChildAt(0); // In getChildAt we need to specify index of a child. In this case it's 0.
String login = currentView.getText().toString();
Intent intent = new Intent(Powitanie.this,UserPanel.class);
intent.putExtra(LOGIN, login);
Powitanie.this.startActivity(intent);
}
};

Android ListView with subitem causes TwoLineListItem cannot be cast to android.widget.TextView

I've created a ListView that can display a list of items and subitems, following this question: android-listview-subitems.
However, when I click on one of the rows, I get the error: E/MessageQueue-JNI: java.lang.ClassCastException: android.widget.TwoLineListItem cannot be cast to android.widget.TextView
The error only started occurring once I added a subitem to each item in the ListView.
The error occurs in the listener for the listView which is this code below:
//listview listener
lvAppFeatures.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//find the selected item and store the value to send to next screen
strSelectedFeature = ((TextView) view).getText().toString();
//move to display screen
Intent switchActivity = new Intent(MainActivity.this,
NextActivity.class);
startActivity(switchActivity);
}
});
Is there a way to have a set of items and a subitems in a ListView but have intent to open another activity work?
It seems you're using android.R.layout.simple_list_item_2
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="2dip"
android:paddingBottom="2dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/listItemFirstLineStyle"/>
<TextView android:id="#android:id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#android:id/text1"
style="?android:attr/listItemSecondLineStyle" />
</TwoLineListItem>
A)
get item directly from adapter
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//find the selected item and store the value to send to next screen
strSelectedFeature = (String)parent.getAdapter().getItem(position);
or B), if you want to have access to textviews
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//find the selected item and store the value to send to next screen
TextView textView1 = view.findViewById(android.R.id.text1);
TextView textView2 = view.findViewById(android.R.id.text2);
//do other things like textView1.getText() etc

Get the text of TextView of Item of ListView

I want to get the text of a TextView that is in a ListView's item.
See the code and you will find what's my question.
Activity.java:
SimpleAdapter adapter = new SimpleAdapter(
rootView.getContext(),
result,
R.layout.article_list_item,
new String[]{"article_title", "article_PubDate", "article_category", "article_articleNo"},
new int[]{R.id.article_title, R.id.article_PubDate, R.id.article_category, R.id.article_articleNo});
ListView articleItem.setAdapter(adapter);
articleItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//I want get the text of R.id.article_articleNo(see article_list_item.xml)?
//What should I do?
}
});
article_list_item.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="vertical"
android:id="#+id/article">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文章标题"
android:id="#+id/article_title"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="30-Apr-2014"
android:id="#+id/article_PubDate"
android:layout_gravity="center_horizontal"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="personal_finance"
android:id="#+id/article_category"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9312"
android:id="#+id/article_articleNo"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="20dp"/>
</LinearLayout>
</LinearLayout>
That's all!!
I want to get the text of R.id.article_article (see article_list_item.xml).
How can I do it?
Does this work for you?
articleItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = result.get(position).get("article_articleNo");
//^^ does not necessarily need to be a string,
// make this whatever data type you are storing in the map
}
});
The idea is that in your SimpleAdapter, you are populating your ListView with a List<Map<String,Object>> (in this case named result).
Therefore, you can get that specific Map<String,Object> from the List by using .get(position), then once again using .get("article_articleNo") to get the Object from the map.
Edit after seeing your comment: Try this:
articleItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tv = (TextView) view.findViewById(R.id.article_articleNo);
String text = tv.getText().toString();
}
});
First of all, forget about the xml layout. It's just a placeholder for data, which is used to define the layout of the data, and has got nothing to do with storing the data.
Now, your problem statement can be re-stated as: how to fetch data from an array (or list or arraylist or whatever the format of your result is) on clicking list item.
Hmm. Sounds simple now.
Refer to #RougeBaneling's answer for technical details.

Unable to do Operation on child of listView

I want to get the child of listview and set the visibility of its two hidden element to true.
My code is
canvasListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
/*
joinCanvasBtn.setEnabled(true);
joinCanvasPasswordTxt.setEnabled(true);*/
Log.d(TAG,"Selected positon : "+position);
int index = canvasListView.getFirstVisiblePosition() + position;
View v = canvasListView.getChildAt(index);
if(v!=null) {
joinCanvasBtn = (Button) v.findViewById(R.id.joinCanvasBtn);
canvasPassword = (EditText) v.findViewById(R.id.joinCanvasPasswordTxt);
joinCanvasBtn.setVisibility(View.VISIBLE);
joinCanvasPasswordTxt.setVisibility(View.VISIBLE);
}
else {
Log.d(TAG,"Unable to find the selected child of listView");
}
}
});
But this is not working its giving me Nullpointer exception. My layout file is given below. Basically I want to show the password and join button whenever user click on that row.
<?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="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<!-- Item Name -->
<TextView
android:id="#+id/canvasName"
android:height="100dp"
/>
<TextView
android:id="#+id/canvasCreator"
android:text="TextView" />
<EditText
android:id="#+id/joinCanvasPasswordTxt"
android:visibility="invisible"
/>
<Button
android:id="#+id/joinCanvasBtn"
android:text="Join"
android:visibility="invisible"
/>
</LinearLayout>
Please help me to find the solution
Try to write
int index = position - canvasListView.getFirstVisiblePosition();
instead of
int index = canvasListView.getFirstVisiblePosition() + position;

android view extraction from OnItemClickMethod

I have a list View and In the adaptor of this list View i have a method
public void onItemClick(AdapterView<?> AdapterView, View View,
int position, long id)
each row of list contain 3 view
xml for row is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#android:color/white" android:padding="5dp">
<TextView android:id="#+id/name" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp" android:textStyle="bold" android:textColor="#android:color/black"
android:layout_marginTop="10dp" android:focusable="false">
</TextView>
<TextView android:id="#+id/street"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="#id/name"
android:textColor="#android:color/black" android:focusable="false">
</TextView>
<ImageView android:id="#+id/pic" android:src="#drawable/pic"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentRight="true" android:layout_marginRight="10dp"
android:layout_below="#id/name" android:focusable="false">
</ImageView>
</RelativeLayout>
In OnItemClickMethod i want to find click event on these 3 view. how can i do this?
Note: if i am using view.getId() it is returning -1 for first row . so i am unable to use this one.
if(view.getId() == R.id.name){
dotask();
}
Update: I just call onClick method inside getView Method
setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(context,position.toString(), Toast.LENGTH_LONG).show();
}
});
and my problem get solved.
Thanks FunkTheMonk and Vikky.
Here in onItemClick View view refers to RelativeLayout and not textviews according to your xml file.
public void onItemClick(AdapterView<?> AdapterView, View View, int position, long id) {
TextView name = View.findViewById ( R.id.name );
for other views .......
}
You can set an onClickListener on Views inside the row used in a ListView.
abstract class Clicker implements OnClickListener {
int mPosition;
public Clicker(int position) {
mPosition = position;
}
}
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
//inflate
}
//do your stuff
convertView.findViewById(R.id.button1).setOnClickListener(new Clicker(position) {
public void onClick(View v)
{
int row = mPosition;
//button 1 clicked
}
});
convertView.findViewById(R.id.button2).setOnClickListener(new Clicker(position) {
public void onClick(View v)
{
int row = mPosition;
//button 2 clicked
}
});
}
onItemClick gives only the selected row, and provides no touch co-ordinates so you can't manually determine which child has been clicked
You can use view.findViewById on the view you get in onItemClick. It says here ( http://www.vogella.de/articles/AndroidListView/article.html ) that it's a relatively lengthy operation, and that the recommended way is using the view's tag to store its relevant child-views. It seems logical, but it might be premature optimization in your case - depending on the number of uses for your view.

Categories

Resources