Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7949333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:58:03+00:00 2026-06-04T01:58:03+00:00

I usually use this code to add item from datebase to list view: public

  • 0

I usually use this code to add item from datebase to list view:

public class ViewEvents extends Activity {

    DBAdapter DB=new DBAdapter(this);

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewevents);

        final ListView myListView = (ListView)findViewById(R.id.MyList);

          final ArrayList<String> todoItems = new ArrayList<String>();
          final ArrayAdapter<String>  aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, todoItems);

           DB.open();
         Cursor c=DB.select();
          c.moveToFirst();
          Integer n=new Integer(c.getCount());

        for(int i=0;i<c.getCount();i++)
        {
             todoItems.add(0, c.getString(0));
             c.moveToNext();
        }

            aa.notifyDataSetChanged();
            myListView.setAdapter(aa);
enter code here

but, in my project I added image to listview as here :

<?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"
    android:padding="5dp" >
 <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    <ImageView
        android:id="@+id/logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:drawablePadding="0dip"
        android:src="@drawable/icon_remove" >
    </ImageView>

    <TextView
        android:id="@+id/label"
       android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/logo"
        android:text="@+id/label"
        android:textSize="25px" >
    </TextView>
</RelativeLayout>
</LinearLayout>

That causes problem in arrayadapter, I can’t catch listview as usuall:

ListView myListView = (ListView)findViewById(R.id.MyList);

any advice please

the new code:

public class RemoveEvent extends ListActivity {

    DBAdapter DB=new DBAdapter(this);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



        final ArrayList<String> todoItems = new ArrayList<String>();
         final ArrayAdapter<String>  aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, todoItems);


         DB.open();
         Cursor c=DB.select();
          c.moveToFirst();
          Integer n=new Integer(c.getCount());
        for(int i=0;i<c.getCount();i++)
        {
             todoItems.add(0, c.getString(0));
             c.moveToNext();
        }


           // todoItems.add(0, c.getString(0));
            aa.notifyDataSetChanged();
            setListAdapter(new RemoveArrayAdapter(this, .......));

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        //get selected items
        String selectedValue = (String) getListAdapter().getItem(position);
        Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();

    }

}

removearrayadapter code:

public class RemoveArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public RemoveArrayAdapter(Context context, String[] values) {
        super(context, R.layout.removeevent, 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.removeevent, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
        textView.setText(values[position]);

        return rowView;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:58:05+00:00Added an answer on June 4, 2026 at 1:58 am

    That causes problem in arrayadapter, I can’t catch listview as usuall:

    What exactly does this means? What is the exception you get(if any)?

    Some issues:

    If you use android.R.layout.simple_list_item_1 then what is with the xml layout you posted?
    If you plan to use that layout file then use this constructor of ArrayAdapter:

    final ArrayAdapter<String>  aa = new ArrayAdapter<String>(this, R.layout.Name_of_layout_you_posted, R.id.label, todoItems);
    

    Also if you plan to pass the Context:

     DBAdapter DB=new DBAdapter(this);
    

    Then you should initialize the db in the onCreate method:

    DBAdapter DB;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewevents);
        DB = new DBAdapter(this);
        //... rest of the code
    

    Also, you should try to use a Cursor based adapter so you don’t have to parse the cursor in a list.

    Edit :
    Modify your RemoveArrayAdapter to accept the ArrayList that you fill with data from the Cursor:

        public class RemoveArrayAdapter extends ArrayAdapter<String> {
            private final Context context;
            private final ArrayList<String> values;
    
            public RemoveArrayAdapter(Context context, ArrayList<String> values) {
                super(context, R.layout.removeevent, 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.removeevent, parent, false);
                TextView textView = (TextView) rowView.findViewById(R.id.label);
                ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
                textView.setText(values.get(position));
                // I'm guessing you want to modify the Logo?!? if yes pass another ArrayList to this adapter
               //contaning the info to set the ImageView  
                return rowView;
            }
    
    }
    

    And in the RemoveActivity:

    for(int i=0;i<c.getCount();i++) {
        todoItems.add(c.getString(0));
        c.moveToNext();
    }
    setListAdapter(new RemoveArrayAdapter(this, todoItems));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To add a rectangle to my plot I usually use this code: ret=Rectangle((x_l[i]-dx,y_l[i]-dx),width=dx,height=dx,facecolor='red',linestyle='solid') ax=gca()
I usually use this line to import file from out of the current folder
canvas.MouseMove.Add(move canvas update) MouseMove.Add( p1 p2 p3) Usually I see this use and documentation,
In the source code of Activity.java, I see some methods bellow : public View
I usually use like this $ find -name testname.c ./dir1/dir2/testname.c $ vi ./dir1/dir2/testname.c it's
I usually use C type casting in C/C++ code. My question is, does adding
I'm writing my own wrapper class for parsing XML data. Usually I use the
I usually use Visual Studio Team System 2008 Source Control Explorer with TFS, but
I usually use the following pipeline to grep for a particular search string and
I usually use pointers in the following manner char *ptr = malloc( sizeof(char) *

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.