I use a ListView with a custom adapter to build a list with ckeckboxes. Now I dont know how i can fetch the "ID" from the selected box.
The xml file for the items look like this:
<?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:orientation="vertical"
android:padding="5dp">
<TextView android:id="#+id/class_open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:onClick="clickHandler" />
<TextView
android:id="#+id/medi_name"
android:text="Test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/checkbox" />
</RelativeLayout>
I need the value from: "class_open"
this is my activity:
package de.bodprod.dkr;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MediNotmediUserListNew extends Activity{
LayoutInflater inflater;
ArrayList<HashMap<String, Object>> medi_notmedi_items;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.medi_notmedi_user_list_new);
ListView mediListCheck = (ListView) findViewById(android.R.id.list);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
medi_notmedi_items = new ArrayList<HashMap<String,Object>>();
SystemDatabaseHandler db = new SystemDatabaseHandler(getApplicationContext());
medi_notmedi_items = db.getAllNotmedis();
final CustomAdapter adapter = new CustomAdapter(this, R.layout.medi_notmedi_user_list_new, medi_notmedi_items);
mediListCheck.setAdapter(adapter);
}
public void clickHandler(View view) {
}
private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>>{
public CustomAdapter(Context context, int textViewResourceId, ArrayList<HashMap<String, Object>> Strings) {
super(context, textViewResourceId, Strings);
}
private class ViewHolder{
TextView class_open, title;
}
ViewHolder viewHolder;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView=inflater.inflate(R.layout.medi_notmedi_user_list_new_item, null);
viewHolder=new ViewHolder();
viewHolder.class_open=(TextView) convertView.findViewById(R.id.class_open);
viewHolder.title=(TextView) convertView.findViewById(R.id.medi_name);
convertView.setTag(viewHolder);
}else{
viewHolder=(ViewHolder) convertView.getTag();
}
viewHolder.class_open.setText(medi_notmedi_items.get(position).get("sql_id").toString());
viewHolder.title.setText(medi_notmedi_items.get(position).get("wirk").toString());
return convertView;
}
}
}
How I can get the value from the class_open field in the clickHandler?
Is it possible to assign the value directly to the checkbox and request it?
Is it possible to request the value from the class_open field?
Please excuse my bad english
Markus
Related
I am a newbie in Android, so I am really sorry if the error that you will encounter is a blunder. I tried making a Custom ListView (An ImageView and a TextView in each row) by using custom_row.xml as the layout file and CustomAdapter.java as the customized adapter.
I do not understand why this application crashes every time I run the same. The exception (As shown in Android Monitor) is caused at this very line in the CustomAdapter.java file.
View customView = myInflater.inflate(R.layout.custom_row, parent, false);
I am attaching all of the Java and Layout codes for the reference. Thanks.
CustomAdapter.java
package com.abhinavankur.listexample;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ArrayAdapter;
class CustomAdapter extends ArrayAdapter<String> {
CustomAdapter(Context context, String[] teams) {
super(context,R.layout.custom_row,teams);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater myInflater = LayoutInflater.from(getContext());
View customView = myInflater.inflate(R.layout.custom_row, parent, false);
String singleTeam = getItem(position);
TextView myText = (TextView) customView.findViewById(R.id.myText);
ImageView myImage= (ImageView) customView.findViewById(R.id.myImage);
myText.setText(singleTeam);
myImage.setImageResource(R.drawable.mine);
return customView;
}
}
custom_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="#+id/myImage"
android:src="#drawable/mine"
android:layout_margin="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/myText"
android:layout_margin="5dp" />
</LinearLayout>
activity_main.xml
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.abhinavankur.listexample.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myListView">
</ListView>
</RelativeLayout>
MainActivity.java
package com.abhinavankur.listexample;
import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String teams[] = {"Real Madrid","Manchester United","Barcelona","Chelsea","Bayern Munich","PSG","Borussia Dortmund","Liverpool","Arsenal","Valencia","Villareal","Leicester City","AS Roma"};
ListAdapter myAdapter = new CustomAdapter(this, teams);
ListView myList = (ListView) findViewById(R.id.myListView);
myList.setAdapter(myAdapter);
Toast.makeText(this,"List Shown",Toast.LENGTH_LONG).show();
myList.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String team = String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(MainActivity.this,team,Toast.LENGTH_SHORT).show();
}
}
);
}
}
Change your CustomAdapter as below. You were inflating the layout in a wrong way.
class CustomAdapter extends ArrayAdapter<String> {
Context context;
CustomAdapter(Context context, String[] teams) {
super(context,R.layout.custom_row,teams);
this.context = context
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.custom_row, null);
}
....
....
}
I am trying to create a listView with just images. Please this is not a stupid question so any help is appreciated. I think my code is correct, but when I run it, it just shows the contentView but empty. It does not show the images I tell it to show.I have created the java file and then two xml files, one with the listView which is the contentView for the java file, and the other for one single row of the listView.
My java file:
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
public class Craft extends Activity {
int[]images={R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.five,R.drawable.six,R.drawable.seven;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.craft);
listView=(ListView)findViewById(R.id.listView);
MyAdapter adapter=new MyAdapter(Craft.this,images);
listView.setAdapter(adapter);
}
class MyAdapter extends ArrayAdapter<String> {
Context c;
int[] images;
int count = 0;
public MyAdapter(Context c,int imgs[]) {
super(Craft.this, R.layout.single_row);
this.c = Craft.this;
this.images = imgs;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row, parent, false);
Log.d("Plates", "Count: " + count++);
ImageView myImage = (ImageView) row.findViewById(R.id.imageView23);
myImage.setImageResource(images[position]);
return row;
}
}
}
my craft.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:background="#drawable/woodcrop">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
My single_row.xml file
<?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:background="#drawable/woodcrop">
<ImageView
android:layout_margin="20dp"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/one"
android:id="#+id/imageView23" />
Any help is appreciated!
You should change your ListView height to:
<ListView
.
.
.
android:layout_height="match_parent"/>
I see, you have not overriden getCount() in your adapter:
So your listview dosen't know that there are items.
You should add this to your adapter:
#Override
public int getCount() {
return images.length;
}
Ty to use this super statement
Super(c , R.layout.single_row,imgs);
I have a ListView set with an ArrayAdapter in my Android app. It lists a set of string values. How do I make it show the row number in front of each list item? Like this:
One
Two
Three
Simply print the position, its in the getView's first param. Dont forget to +1 becuause position starts from 0
I guess you're trying to achieve this,
If so, here's the working code :
MainActivity.java
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
ListView lv;
String[] title = {"Windows","Apple","Android" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView1);
CustomAdapter adapter = new CustomAdapter(this,title);
lv.setAdapter(adapter);
}
}
class CustomAdapter extends ArrayAdapter<String>
{
Context context;
String[] title;
CustomAdapter(Context c, String[] title)
{
super(c, R.layout.listitem,title);
this.context = c;
this.title=title;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = vi.inflate(R.layout.listitem, parent, false);
TextView titlee = (TextView) row.findViewById(R.id.textView1);
int pos = position+1;
titlee.setText(+pos + ". " + title[position]);
pos++;
return row;
}
}
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" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
lisitem.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="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Title"
android:padding="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
I'm trying to add a button in a list View, I searched a lot on google but nothing was good enough for me.
Here's my code: I have 2 classes :
Menu.java
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class Menu extends ListActivity implements OnItemClickListener {
String[] listaMeniu = { "1", "2", "3"};
Button butonNota;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ListAdapter(this, listaMeniu));
ListView listView = getListView();
listView.setOnItemClickListener (this);
Button btnLoadMore = new Button(this);
btnLoadMore.setText("show me");
}
}
Menu.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:padding="5dp">
<ImageView
android:id="#+id/1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:src="#drawable/1" />
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize = "30dp"
android:text="1" />
</LinearLayout>
ListAdapter.java
package com.example.a;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ListAdapter extends ArrayAdapter {
private Context context;
private String[] values;
public ListAdapter(Context context, String[] values) {
// TODO Auto-generated constructor stub
super (context, R.layout.menu, values);
this.context = context;
this.values = values;
}
}
I already made the list view, but I don't know how to add the button above the list. I tried to add it in menu.xml but it's shows up a button for every item in the list. Hope you guys understand what I want.
Thank you!
The best way is to create your custom adapter. For example :
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Button;
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MySimpleArrayAdapter(Context context, String[] values) {
super(context, R.layout.rowlayout, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
Button buttonView = (Button) rowView.findViewById(R.id.button);
buttonView.setText(values[position]);
return rowView;
}
}
And here is the xml from "rowlayout.xml". You have to put the layout file in the res/layout project folder.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button"
android:layout_width="22px"
android:layout_height="22px"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_marginTop="4px">
</Button>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="20px" >
</TextView>
</LinearLayout>
And then just update your Menu.java
public class Menu extends ListActivity implements OnItemClickListener {
String[] listaMeniu = { "1", "2", "3"};
Button butonNota;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new MySimpleArrayAdapter(this, listaMeniu));
...
}
}
You need to override method getView in your adapter to return Button.
It appears that you want your 10th item in the list to be a button.
That means that when you overwrite your ArrayAdapter class, you need to modify GetView() so it returns the button instead of a picture. Create two different layout XML files, rowlayout_picture.xml and rowlayout_button.xml, and then:
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = null;
if (position < 10)
rowView = inflater.inflate(R.layout.rowlayout_picture, parent, false);
else
rowView = inflater.inflate(R.layout.rowlayout_button, parent, false);
return rowView;
}
i am trying to create a listview in my android application. But i am getting Resource Not found Exception while running the project.
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"
tools:context=".MainActivity" >
<include
android:id="#+id/title_bar"
layout="#layout/title_bar" />
<ListView
android:id="#+id/FeedList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title_bar"
></ListView>
</RelativeLayout>
list_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="#FFFFFF">
<ImageButton
android:id="#+id/FeedType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#E0E0E0"
android:padding="9dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/FeedTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Feed Title"
android:textSize="18sp"
android:layout_toRightOf="#+id/FeedType"
android:paddingLeft="5dp"
android:textColor="#000000"
/>
<TextView
android:id="#+id/FeedPostedBy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/FeedTitle"
android:text="by FeedOwner"
android:layout_toRightOf="#+id/FeedType"
android:paddingLeft="5dp"
android:textSize="10sp"
/>
<TextView
android:id="#+id/FeedContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/FeedPostedBy"
android:layout_toRightOf="#+id/FeedType"
android:paddingLeft="5dp"
android:paddingTop="8dp"
android:text="The content posted as a feed by the feed owner will appear here"
android:textSize="10sp"/>
</RelativeLayout>
ListAdapter Class :
package com.tcs.internal.prime;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
public class ListAdapter extends BaseAdapter{
private Activity listAdapterActivity = null;
private ArrayList<HashMap<String,String>> data;
private static LayoutInflater inflater;
public ListAdapter(Activity ListActivity,ArrayList<HashMap<String, String>> listData)
{
listAdapterActivity = ListActivity;
data = listData;
inflater = (LayoutInflater)listAdapterActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.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 listItem = convertView;
if(listItem == null)
listItem = inflater.inflate(R.id.FeedList, null);
ImageButton feedIcon = (ImageButton) listItem.findViewById(R.id.FeedType);
TextView feedTitle = (TextView) listItem.findViewById(R.id.FeedTitle);
TextView feedOwner = (TextView) listItem.findViewById(R.id.FeedPostedBy);
TextView feedContent = (TextView) listItem.findViewById(R.id.FeedContent);
HashMap<String, String> feedData = new HashMap<String, String>();
feedData = data.get(position);
feedIcon.setImageResource(R.drawable.ic_launcher);
feedTitle.setText(feedData.get("FeedTitle"));
feedOwner.setText(feedData.get("FeedOwner"));
feedContent.setText(feedData.get("FeedContent"));
return listItem;
}
}
MainActivity class :
package com.tcs.internal.prime;
import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<HashMap<String, String>> feedList = new ArrayList<HashMap<String,String>>();
HashMap<String, String> data = new HashMap<String, String>();
data.put("FeedTitle","Application crashed when launched in Windows 7");
data.put("FeedOwner", "By Venkatramanan");
data.put("FeedContent","Launch the financial aid adminstration in windows 7 environment");
feedList.add(data);
data.clear();
data.put("FeedTitle","Application crashed when launched in Windows 8 ");
data.put("FeedOwner", "By Siva Guru");
data.put("FeedContent","Launch the financial aid adminstration in windows 8 environment");
feedList.add(data);
ListView feedListView = (ListView)findViewById(R.id.FeedList);
ListAdapter listAdapter = new ListAdapter(this, feedList);
feedListView.setAdapter(listAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
I am getting this following exception :
android.content.res.Resources$NotFoundException: Resource ID #0x7f070001 type #0x12 is not valid
All you have to inflate the custom layout in your listview's adapter's getview method as
listItem = inflater.inflate(R.layout.list_item, null);
instead of
listItem = inflater.inflate(R.id.FeedList, null);