Add item in listview arrayadapter - android

Please tell me how to add items in listview arrayadapter?
I found only how to make for standard adapter
Activity:
public class Activity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.m);
ListView lv=(ListView) findViewById(R.id.lv);
String[] Id1={"1","2","3"}, Text1={"one","two","three"};
CustomAdapter ad = new CustomAdapter(this, Id1 , Text1);
ad.setCustomListener(new LVListener() {
public void onClick(String text) {
Log.d("APP", text);
}
});
lv.setAdapter(ad);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btn.setVisibility(View.GONE);
String[] Id2={"4","5","6"},Text2={"four","five","six"};
// add Id2 and Text2 in listview
}
});
}
}
CustomAdapter:
public class CustomAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] id, text;
private ListViewListener micl;
public CustomAdapter(Context context, String[] id, String[] text) {
super(context, R.layout.list, id);
this.context = context;
this.id = id;
this.text = text;
}
public void setCustomListener(ListViewListener micl) { this.micl = micl; }
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View View = inflater.inflate(R.layout.list, parent, false);
final int pos = position;
final TextView tView = (TextView) View.findViewById(R.id.textView);
tView.setText(text[pos]);
rowView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (micl != null)
micl.onClick(text[pos]);
}
});
return View;
}
}
I tried to do with notifyDatasetChanged() but nothing happened.
Please tell me how to do that.

ArrayAdapter has the add method, but in order to use it the dataset you provide to the super can not be an array, that's because the using Arrays.asList(objects), that returns an immutable list. From the documentation
Returns a List of the objects in the specified array. The size of the
List cannot be modified, i.e. adding and removing are unsupported, but
the elements can be set. Setting an element modifies the underlying
array.

Related

How to Switch Activities using a Custom ListView Adapter?

I'm trying to use a Custom ListView Adapter to switch to different activities, but am having trouble doing so, as there is an error that I can't seem to get rid of.
Relevant code
Category Table:
public class CategoryTable extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.category_table);
populateTable();
goToPreviousActivity();
}
private void populateTable() {
final ListView mListView = findViewById(R.id.listView);
Category 1 = new Category(" One");
Category 2 = new Category(" Two");
Category 3 = new Category(" Three");
Category 4 = new Category(" Four");
final ArrayList<Category> categoryList = new ArrayList<>();
categoryList.add(1);
categoryList.add(2);
categoryList.add(3);
categoryList.add(4);
CategoryListAdapter adapter = new CategoryListAdapter(this, R.layout.adapter_view_layout, categoryList);
mListView.setAdapter(adapter);
}
private void goToPreviousActivity() {
ImageButton btn = findViewById(R.id.backButton);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
public static Intent makeIntent(Context context) {
return new Intent(context, CategoryTable.class);
}
}
Category List Adapter:
public class CategoryListAdapter extends ArrayAdapter<Category> {
private Context mContext;
int mResource;
public CategoryListAdapter(Context context, int resource, ArrayList<Category> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
}
#NonNull
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
String name = getItem(position).getName();
Category category = new Category(name);
final LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
Button catName = convertView.findViewById(R.id.btnNextTbl);
catName.setText(name);
catName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (position == 0) {
//error -> makeIntent(android.content.Context in AllToolsTable cannot be applied to com.test.test.CategoryListAdapter
Intent intent = AllToolsTable.makeIntent(CategoryListAdapter.this);
mContext.startActivity(intent);
} else {
Toast toast = Toast.makeText(getContext(), "Clicked2", Toast.LENGTH_SHORT);
toast.show();
}
}
});
return convertView;
}
}
(I commented above the erroneous piece of code)
All Tools Table:
public class AllToolsTable extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_tools_table);
}
public static Intent makeIntent(Context context) {
return new Intent(context, AllToolsTable.class);
}
}
I will provide any other relevant code/details if needed.
Any help will be greatly appreciated.
//error -> makeIntent(android.content.Context in AllToolsTable cannot be applied to com.test.test.CategoryListAdapter
Intent intent = AllToolsTable.makeIntent(CategoryListAdapter.this);
Your AllToolsTable.makeIntent() method requires a Context argument. Normally you'd be fine with something like AllToolsTable.makeIntent(MainActivity.this) because Activity is a subclass of Context. However, CategoryListAdapter is not.
So you need to get your hands on a Context object somehow.
Luckily, the getView() method passes you a non-null ViewGroup parent object, and all views have a context. So you can replace your intent creation with this code:
Context context = parent.getContext();
Intent intent = AllToolsTable.makeIntent(context);

How to increase value in each row of a ListView separately using separate button in each row?

I have a ListView and in each list view I have a button and a TextView. On button click I want to increase te value shown in the TextView. I am able to increase the vale in the TextView but the problem is that the value is incrementing in the next row of the TextView from where it is left in previous TextView .
But I want to start from 1 for each row. How can I do that?
Here is my code
MainActivitty.java
public class MainActivity extends AppCompatActivity {
ListView lv;
TextView t;
int q=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv= (ListView) findViewById(R.id.list);
lv.setAdapter(new MyAd(this));
}
public void hand(View v)
{
final RelativeLayout rl= (RelativeLayout) v.getParent();
Button b= (Button) rl.getChildAt(2);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
t= (TextView) rl.getChildAt(1);
quantity();
}
});
}
public void quantity()
{
q=q+1;
t.setText(""+q);
}
}
Edit 1:
My problem is solved till some extent. I am able to increment the value separately for each button click for separate rows but the value is not reflected in the text view. How to reflect it in text view?
My Code:
MainActivity.java
public class MainActivity extends AppCompatActivity implements MyAd.customButtonListener {
ListView lv;
int q=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv= (ListView) findViewById(R.id.list);
MyAd ma=new MyAd(this);
ma.setCustomButtonListner(this);
lv.setAdapter(ma);
}
#Override
public void onButtonClickListner(int position,TextView t) {
q=q+1;
t.setText(""+q);
}
}
MyAd.java
class MyAd extends BaseAdapter {
int q=0,a[];
List<ListItem> li=new ArrayList<>();
Context c;
MyViewHolder mvh;
customButtonListener customListner;
MyAd(Context c)
{
this.c=c;
Resources res=c.getResources();
String title[]=res.getStringArray(R.array.title);
a=new int[title.length];
for(int i=0;i<title.length;i++)
{
li.add(new ListItem(title[i]));
}
}
public interface customButtonListener
{
void onButtonClickListner(int position, TextView t);
}
public void setCustomButtonListner(customButtonListener listener)
{
this.customListner = listener;
}
#Override
public int getCount() {
return li.size();
}
#Override
public Object getItem(int i) {
return i;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(final int position, View view, ViewGroup parent) {
View row=view;
if(row==null) {
LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item, parent, false);
mvh=new MyViewHolder();
mvh.txt=row.findViewById(R.id.txt);
mvh.quan=row.findViewById(R.id.quan);
mvh.bt=row.findViewById(R.id.button);
row.setTag(mvh);
}
else
{
mvh= (MyViewHolder) row.getTag();
}
ListItem sr=li.get(position);
//String s=sr.title;
mvh.txt.setText(sr.title);
mvh.bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
q=a[position];
mvh.quan.setText(String.valueOf(q++));
a[position]=q;
System.out.println("position:"+position+"-"+q);
notifyDataSetChanged(); }
});
return row;
}
class MyViewHolder
{
TextView txt,quan;
Button bt;
}
}
ListItem.java
public class ListItem {
String title;
ListItem(String title)
{
this.title=title;
}
}
I cannot completely tell you what is wrong with your code unless you publish your custom adapter code but as far as I can understand your problem:
ListView has a onItemClickListener which gets called when you click on any item but this happens when you click on any portion of the item.
In order for you to get the callback only when the button is clicked you need to set a onClickListener to the button and call an interface function inside onClick(). Inside this callback function you will write the code to change your TextView value.
After changing your dataset you will call adapter.notifyDatasetChanged() in order to reflect the change in your listView.
This tutorial might help you in achieving what you want if I have understood your problem correctly.

how to delete a row from a ListView with a delete button in the row

I have a class which extends ArrayAdapter<String>. I want to have a delete image button deletes the particular row.. This is my code:
public class ViewCartList extends ArrayAdapter<String> {
private String[] cart_item_name;
private String[] cart_item_quan;
private String[] cart_item_price;
private Activity context;
public ViewCartList(Activity context, String[] cartitemquan, String[] cartitemname,String[] cartitemprice){
super(context,R.layout.viewcartlist,cartitemquan);
this.context = context;
this.cart_item_quan = cartitemquan;
this.cart_item_name = cartitemname;
this.cart_item_price = cartitemprice;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
final View listViewItem = inflater.inflate(R.layout.viewcartlist, null, true);
TextView textViewItemQuan = (TextView) listViewItem.findViewById(R.id.cart_quan);
TextView textViewItemName = (TextView) listViewItem.findViewById(R.id.cart_item_name);
TextView textViewItemPrice = (TextView) listViewItem.findViewById(R.id.cart_item_price);
ImageButton imcut = (ImageButton) listViewItem.findViewById(R.id.remove_row);
textViewItemQuan.setText(cart_item_quan[position]);
textViewItemName.setText(cart_item_name[position]);
textViewItemPrice.setText(cart_item_price[position]);
imcut.setVisibility(View.VISIBLE);
imcut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// I want to delete that particular row
}
});
return listViewItem;
}
}
And This is my Basket class where I am using this above adapter class.
What to do if i want to remove a particular row from the ListView from that delete button given above..
public class Basket extends AppCompatActivity implements View.OnClickListener {
TextView cartview;
MyCartDatabse myDatabase;
SQLiteDatabase sql;
ContentValues cv;
String wr;
ListView list;
public static String[] quan =null;
public static String[] itemname=null;
public static String[] baseprice=null;
TextView edit_order;
public boolean imrow;
ViewCartList vc;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basket);
myDatabase = new MyCartDatabse(this,"mydb",null,1);
sql = myDatabase.getWritableDatabase();
cv = new ContentValues();
list = (ListView)findViewById(R.id.listcart);
getRecords();
edit_order = (TextView)findViewById(R.id.edit_order);
edit_order.setOnClickListener(this);
}
public void getRecords(){
sql = myDatabase.getReadableDatabase();
Cursor cursor = sql.rawQuery("select * from cart ",null);
quan = new String[cursor.getCount()];
itemname = new String[cursor.getCount()];
baseprice = new String[cursor.getCount()];
int i = 0;
if(cursor.getCount()>0){
while(cursor.moveToNext()){
String uquan = cursor.getString(5);
String uname = cursor.getString(1);
String uprice = cursor.getString(4);
quan[i] = uquan;
itemname[i] = uname;
baseprice[i] = uprice;
i++;
}
vc = new ViewCartList(this,quan,itemname,baseprice);
list.setAdapter(vc);
}
else{
// Do something
}
cursor.close();
}
#Override
public void onClick(View view) {
edit_order.setText("Apply Changes");
}
}
The elegant approach for your problem is to pass an ArrayList of object to your Adapter and then handle the delete action.
So you might consider creating an object like this.
public class Cart {
public String cart_item_name;
public String cart_item_quan;
public String cart_item_price;
}
Now take an ArrayList of that class and populate the ArrayList to pass to your Adapter.
public class ViewCartList extends BaseAdapter {
private Context context;
private ArrayList<Cart> mCartList;
public ViewCartList(Context context, ArrayList<Cart> mCartList) {
this.context = context;
this.mCartList = mCartList;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
final View listViewItem = inflater.inflate(R.layout.viewcartlist, null, true);
TextView textViewItemQuan = (TextView) listViewItem.findViewById(R.id.cart_quan);
TextView textViewItemName = (TextView) listViewItem.findViewById(R.id.cart_item_name);
TextView textViewItemPrice = (TextView) listViewItem.findViewById(R.id.cart_item_price);
ImageButton imcut = (ImageButton) listViewItem.findViewById(R.id.remove_row);
textViewItemQuan.setText(mCartList.get(position).cart_item_quan);
textViewItemName.setText(mCartList.get(position).cart_item_name);
textViewItemPrice.setText(mCartList.get(position).cart_item_price);
// Remove the following and set the visibility true from the layout.
// imcut.setVisibility(View.VISIBLE);
imcut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Delete the row
mCartList.remove(position);
notifyDatasetChanged();
}
});
return listViewItem;
}
}
If you want to completely delete the item you should consider using List<String> instead of Array
Here is updated code for List
public class ViewCartList extends ArrayAdapter<String> {
private List<String> cart_item_name; // Use List here
private List<String> cart_item_quan; // Use List here
private List<String> cart_item_price; // Use List here
private Activity context;
public ViewCartList(Activity context, List<String> cartitemquan, List<String> cartitemname,List<String> cartitemprice){
super(context, R.layout.viewcartlist,cartitemquan);
this.context = context;
this.cart_item_quan = cartitemquan;
this.cart_item_name = cartitemname;
this.cart_item_price = cartitemprice;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
final View listViewItem = inflater.inflate(R.layout.viewcartlist, null, true);
TextView textViewItemQuan = (TextView) listViewItem.findViewById(R.id.cart_quan);
TextView textViewItemName = (TextView) listViewItem.findViewById(R.id.cart_item_name);
TextView textViewItemPrice = (TextView) listViewItem.findViewById(R.id.cart_item_price);
ImageButton imcut = (ImageButton) listViewItem.findViewById(R.id.remove_row);
textViewItemQuan.setText(cart_item_quan.get(position));
textViewItemName.setText(cart_item_name.get(position));
textViewItemPrice.setText(cart_item_price.get(position));
imcut.setVisibility(View.VISIBLE);
imcut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// I want to delete that particular row
cart_item_quan.remove(position);
cart_item_name.remove(position);
cart_item_price.remove(position);
notifyDataSetChanged();
}
});
return listViewItem;
}
}
Arrays are fixed length and can not be resized once created. You can set an element to null to remove an object reference;
imcut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// I want to delete that particular row
cart_item_name[position] = null;
cart_item_quan[position]= null;
cart_item_price[position] = null;
notifydatasetchanged();
}
});
public class ViewCartList extends ArrayAdapter<String> {
private String[] cart_item_name;
private String[] cart_item_quan;
private String[] cart_item_price;
private Activity context;
public ViewCartList(Activity context, String[] cartitemquan, String[] cartitemname,String[] cartitemprice){
super(context,R.layout.viewcartlist,cartitemquan);
this.context = context;
this.cart_item_quan = cartitemquan;
this.cart_item_name = cartitemname;
this.cart_item_price = cartitemprice;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
final View listViewItem = inflater.inflate(R.layout.viewcartlist, null, true);
TextView textViewItemQuan = (TextView) listViewItem.findViewById(R.id.cart_quan);
TextView textViewItemName = (TextView) listViewItem.findViewById(R.id.cart_item_name);
TextView textViewItemPrice = (TextView) listViewItem.findViewById(R.id.cart_item_price);
ImageButton imcut = (ImageButton) listViewItem.findViewById(R.id.remove_row);
textViewItemQuan.setText(cart_item_quan[position]);
textViewItemName.setText(cart_item_name[position]);
textViewItemPrice.setText(cart_item_price[position]);
imcut.setVisibility(View.VISIBLE);
imcut.setTag(position);
imcut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// I want to delete that particular row
int pos = Integer.parseInt(view.getTag().toString());
mCartList.remove(position);
notifyDatasetChanged();
}
});
return listViewItem;
}
}

Android listview showing only last element

i created custom listview with text and two buttons, i set up arraylist and adapter but my listview is showing every element as last, for ex. if i add 3 elements: "text1","text2","text3" my listview shows "text3", "text3" "text3" and i dont have any idea why.
private ListView lista;
private List<Piosenka> listaPiosenek;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (EditText) findViewById(R.id.editText1);
lista = (ListView) findViewById(R.id.listView1);
lista.setClickable(true);
}
public void update_listy() throws MalformedURLException, IOException
{
final List<Piosenka> listaPiosenek = new ArrayList<Piosenka>();
listaPiosenek.add(new Piosenka("text1"));
listaPiosenek.add(new Piosenka("text2"));
listaPiosenek.add(new Piosenka("text3"));
PiosenkaAdapter adapter = new PiosenkaAdapter(this, listaPiosenek);
lista.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long index)
{
System.out.println("sadsfsf");
}
});
lista.setAdapter(adapter);
}
Edit: PiosenkaAdapter code
public class PiosenkaAdapter extends BaseAdapter implements OnClickListener {
private Context context;
private List<Piosenka> listapiosenek;
public PiosenkaAdapter(Context context, List<Piosenka> listapiosenek) {
this.context = context;
this.listapiosenek = listapiosenek;
}
public int getCount() {
return listapiosenek.size();
}
public Object getItem(int position) {
return listapiosenek.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup viewGroup) {
Piosenka element = listapiosenek.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listview_element, null);
}
TextView tvTytul = (TextView) convertView.findViewById(R.id.tvTytul);
tvTytul.setText(Piosenka.getTytul());
Button btnPobierz = (Button) convertView.findViewById(R.id.btnPobierz);
btnPobierz.setFocusableInTouchMode(false);
btnPobierz.setFocusable(false);
btnPobierz.setTag(element);
Button btnPlay = (Button) convertView.findViewById(R.id.btnPlay);
btnPlay.setFocusableInTouchMode(false);
btnPlay.setFocusable(false);
btnPlay.setOnClickListener(this);
btnPlay.setTag(element);
// btnRemove.setId(position);
return convertView;
}
#Override
public void onClick(View view) {
switch(view.getId()){
case R.id.btnPobierz:
Piosenka entry = (Piosenka) view.getTag();
listapiosenek.remove(entry);
notifyDataSetChanged();
break;
case R.id.btnPlay:
entry = (Piosenka) view.getTag();
listapiosenek.remove(entry);
notifyDataSetChanged();
break;
}
}
}
Try this...
lista.setAdapter(adapter);
adapter.notifyDataSetChanged();
Can you paste you PiosenkaAdapter's code?
I don't know your language, but the Piosenka variable is fetched correctly in getView()
Piosenka element = listapiosenek.get(position);
But this looks strange to me
TextView tvTytul = (TextView) convertView.findViewById(R.id.tvTytul);
tvTytul.setText(Piosenka.getTytul());
Piosenka.getTytul() looks to me as a static method call, where you should do a regular method call to element.getTytul() instead.

Android ListView Adapter OnClickListener issue

I created a ListView with a custom layout for each view. I have several onClickListeners within the list view adapter for TextView items within the custom layout. The onClickListeners work within the list view adapter and I can obtain the position of the view, but when I try to make calls within the onClickListeners to methods in my activity, I get the "Cannot make a static reference to a non-static method" errors. So I started converting things to static, which made things work as intended, but I'm sure you all know it's a big mistake, which has finally caught up with me.
How the heck do I access methods in my main activity without making the methods static?? I'm new at this, so please forgive my noobish question. Thanks
partial code listing...
public class main extends Activity {
private ArrayList<DataItem> dataItems;
private DataItemAdapter aa;
private ListView dataListView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dataListView = (ListView)findViewById(R.id.dataListView);
dataItems = new ArrayList<DataItem>();
int resID = R.layout.dataitem;
aa = new DataItemAdapter(this, resID, dataItems);
dataListView.setAdapter(aa);
dataListView.setItemsCanFocus(true);
populateArray();
}
public void populateArray() {
DataItem newItem = new DataItem(
"2008","Ferrari","F430","Red","ASX772"
);
dataItems.add(0, newItem);
newItem = new DataItem(
"2008","Ferrari","F430","Black","TZB123"
);
dataItems.add(0, newItem);
newItem = new DataItem(
"2009","Ferrari","F430","Red","MIDAS"
);
dataItems.add(0, newItem);
aa.notifyDataSetChanged();
}
public static void modelInfo(int pos) {
Log.i("modelInfo", "=" + pos);
}
public static void makeInfo(int pos) {
Log.i("makeInfo", "=" + pos);
}
public static void assetInfo(int pos) {
Log.i("assetInfo", "=" + pos);
}
}
public class DataItemAdapter extends ArrayAdapter<DataItem> {
private Activity activity;
private int resource;
private static LayoutInflater inflater=null;
public DataItemAdapter(Activity _activity,int _resource,List<DataItem> _items) {
super(_activity, _resource, _items);
inflater = (LayoutInflater)_activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
resource = _resource;
activity = _activity;
}
public static class ViewHolder {
TextView carYear;
TextView carMake;
TextView carModel;
TextView carColor;
TextView assetTag;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(resource, null);
holder=new ViewHolder();
holder.carYear = (TextView)vi.findViewById(R.id.carYear);
holder.carMake = (TextView)vi.findViewById(R.id.carMake);
holder.carModel = (TextView)vi.findViewById(R.id.carModel);
holder.carColor = (TextView)vi.findViewById(R.id.carColor);
holder.assetTag = (TextView)vi.findViewById(R.id.assetTag);
vi.setTag(holder);
} else {
holder=(ViewHolder)vi.getTag();
}
DataItem item = getItem(position);
holder.carYear.setText(item.getCarYear());
holder.carMake.setText(item.getCarMake());
holder.carModel.setText(item.getCarModel());
holder.carColor.setText(item.getCarColor());
holder.assetTag.setText(item.getAssetTag());
holder.carYear.setTag(position);
holder.assetTag.setTag(position);
final OnClickListener makeListener = new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout)v.getParent();
TextView tv = (TextView)ll.getChildAt(0);
Integer pos = (Integer) tv.getTag();
main.makeInfo(pos);
}
};
holder.carMake.setOnClickListener(makeListener);
final OnClickListener modelListener = new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout)v.getParent();
TextView tv = (TextView)ll.getChildAt(0);
Integer pos = (Integer) tv.getTag();
main.modelInfo(pos);
}
};
holder.carModel.setOnClickListener(modelListener);
final OnClickListener assetListener = new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout)v.getParent();
TextView tv = (TextView)ll.getChildAt(0);
Integer pos = (Integer) tv.getTag();
main.assetInfo(pos);
}
};
holder.assetTag.setOnClickListener(assetListener);
return vi;
}
why you dont attach an onItemClickListener to your ListView in your activity, instead of perform ItemClick from each inner view in the ListView
dataListView = (ListView)findViewById(R.id.dataListView);
dataItems = new ArrayList<DataItem>();
int resID = R.layout.dataitem;
aa = new DataItemAdapter(this, resID, dataItems);
dataListView.setAdapter(aa);
//attach a listener to the list view
dataListView.setOnItemClickListener (listener);
dataListView.setItemsCanFocus(true);
and inside your listener on onItemClick method you can access the activity methods.
EDIT 1:
the OnItemClickListener gives to you the following parameters, AdapterView parent, View view, int position, long
your individual TextView is a child of the view parameter and you can access to it getting the childAt... something like this:
OnItemClickListener listener = new OnItemClickListener (){
#Override
onItemClick(AdapterView<?> parent, View view, int position, long id){
((TextView)view.findViewById(R.id.yourTextViewId)).getText();
//or do your stuff
}
}
EDIT 2:
your main activity, remember, by convention all Class name are capitalized, so main class must be Main class
public class Main extends Activity {
private ArrayList<DataItem> dataItems;
private DataItemAdapter aa;
private ListView dataListView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dataListView = (ListView)findViewById(R.id.dataListView);
dataItems = new ArrayList<DataItem>();
int resID = R.layout.dataitem;
aa = new DataItemAdapter(this, resID, dataItems);
dataListView.setAdapter(aa);
dataListView.setItemsCanFocus(true);
populateArray();
}
public void populateArray() {
DataItem newItem = new DataItem(
"2008","Ferrari","F430","Red","ASX772"
);
dataItems.add(0, newItem);
newItem = new DataItem(
"2008","Ferrari","F430","Black","TZB123"
);
dataItems.add(0, newItem);
newItem = new DataItem(
"2009","Ferrari","F430","Red","MIDAS"
);
dataItems.add(0, newItem);
aa.notifyDataSetChanged();
}
public void modelInfo(int pos) {
Log.i("modelInfo", "=" + pos);
}
public void makeInfo(int pos) {
Log.i("makeInfo", "=" + pos);
}
public void assetInfo(int pos) {
Log.i("assetInfo", "=" + pos);
}
}
Now, your adapter
public class DataItemAdapter extends ArrayAdapter<DataItem> {
private Activity activity;
private int resource;
private LayoutInflater inflater=null;
public DataItemAdapter(Activity _activity,int _resource,List<DataItem> _items) {
super(_activity, _resource, _items);
inflater = (LayoutInflater)_activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//i always do this way, but i dont think this is the error
//inflater = LayoutInflater.from(_activity.getBaseContext());
resource = _resource;
activity = _activity;
}
public static class ViewHolder {
TextView carYear;
TextView carMake;
TextView carModel;
TextView carColor;
TextView assetTag;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(resource, null);
holder=new ViewHolder();
holder.carYear = (TextView)vi.findViewById(R.id.carYear);
holder.carMake = (TextView)vi.findViewById(R.id.carMake);
holder.carModel = (TextView)vi.findViewById(R.id.carModel);
holder.carColor = (TextView)vi.findViewById(R.id.carColor);
holder.assetTag = (TextView)vi.findViewById(R.id.assetTag);
vi.setTag(holder);
} else {
holder=(ViewHolder)vi.getTag();
}
DataItem item = getItem(position);
holder.carYear.setText(item.getCarYear());
holder.carMake.setText(item.getCarMake());
holder.carModel.setText(item.getCarModel());
holder.carColor.setText(item.getCarColor());
holder.assetTag.setText(item.getAssetTag());
holder.carYear.setTag(position);
holder.assetTag.setTag(position);
final OnClickListener makeListener = new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout)v.getParent();
TextView tv = (TextView)ll.getChildAt(0);
Integer pos = (Integer) tv.getTag();
//main.makeInfo(pos);
((Main)activity).makeInfo(pos);
}
};
holder.carMake.setOnClickListener(makeListener);
final OnClickListener modelListener = new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout)v.getParent();
TextView tv = (TextView)ll.getChildAt(0);
Integer pos = (Integer) tv.getTag();
//main.modelInfo(pos);
((Main)activity).modelInfo(pos);
}
};
holder.carModel.setOnClickListener(modelListener);
final OnClickListener assetListener = new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout)v.getParent();
TextView tv = (TextView)ll.getChildAt(0);
Integer pos = (Integer) tv.getTag();
//main.assetInfo(pos);
((Main)activity).assetInfo(pos);
}
};
holder.assetTag.setOnClickListener(assetListener);
return vi;
}
hope it works :)

Categories

Resources