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 6942065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:58:02+00:00 2026-05-27T12:58:02+00:00

Hi I’ve been having some trouble trying to get a list view I created

  • 0

Hi I’ve been having some trouble trying to get a list view I created to add whatever it is the user inputs into the EditText view. The contents should be added when the user presses the Add button but the program just crashes, not to mention running really slowly. I haven’t added the entire code below only the piece I think is relevant.

public class ListViewActivity_Two extends Activity 
{
EditText edit;          Button add;
final List<String> data = new ArrayList<String>();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    data.add( "Hello" );

    edit = ( EditText )findViewById( R.id.editTxt ); 
    add = ( Button ) findViewById( R.id.add );
    add.setOnClickListener( new OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {
            data.add( edit.getText().toString() );
        }
    }); 

    /**
    for( int i = 1; i <= 10; i++ )
    {
        data.add( String.format( "Item %d", i ) );
    }
    */

    // Declare an instance of our custom adapter class
    CustomAdapter adapter = new CustomAdapter( this, data );

    ListView listView = ( ListView )findViewById( android.R.id.list ); 
    listView.setAdapter( adapter );

    listView.setOnItemClickListener( new OnItemClickListener()
    {

        @Override
        public void onItemClick( AdapterView listview, View v, int pos, long id ) 
        {
            TextView textView = (TextView)v.findViewById( android.R.id.text1 );
            toast( (String) textView.getText() );
        }

    });

}

The data.add() is just there to make sure I can actually add to the ListView, I can. I do the same thing in the onClickButton so why is this happening? Also I am right to declare the creation of the views above the onCreate method or do they have to be within it to work correctly?

Edit

Here’s the CustomAdapter Class

package com.stylingandroid.listview;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class CustomAdapter extends BaseAdapter
{
private final Context context;
private final List<String> items;

public CustomAdapter( Context context, List<String> items )
{
    this.context = context;
    this.items = items;
}

@Override
public int getCount() 
{   
    return items.size();
}

@Override
public Object getItem(int position) 
{
    return items.get( position );
}

@Override
public long getItemId(int position) 
{
    return getItem( position ).hashCode();
}

/**
 * Method getView() below is inefficent because inflating XML layouts is memory expensive
 * because it involves parsing XML and then instantiating objects to represent all of the Views
 * in the view hierarchy
 * 
 * see: http://blog.stylingandroid.com/archives/632
 * 
 * for more details.
 * 
 * The more efficient code is below it!


@Override
public View getView( int position, View convertView, ViewGroup parent ) 
{
    LayoutInflater inflater = ( LayoutInflater )context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    View v = inflater.inflate( R.layout.item, parent, false );

    final String item = ( String ) getItem( position );
    TextView tv = ( TextView )v.findViewById( android.R.id.text1 );
    tv.setText( item );

    ImageView iv = ( ImageView )v.findViewById( R.id.imageView );
    iv.setOnClickListener( new OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {
            Toast.makeText( context, String.format( "ImageClicked: %s", item ), Toast.LENGTH_SHORT ).show();
        }

    });

    return v;
}

*/

@Override
public View getView( int position, View convertView, ViewGroup parent ) 
{
    /**
     * this method differs from the one above because, here, all we are doing is inflating a new view
     * if convertView is null. This significantly reduces the amount of object deletion and instantiation
     * that's required, and vastly improves our scrolling performance particularly on low powered devices
     */
    View v = convertView;

    if( v == null )
    {
        LayoutInflater inflater = ( LayoutInflater )context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        v = inflater.inflate( R.layout.item, parent, false );
    }

    final String item = ( String ) getItem( position );
    TextView tv = ( TextView )v.findViewById( android.R.id.text1 );
    tv.setText( item );

    ImageView iv = ( ImageView )v.findViewById( R.id.imageView );
    iv.setOnClickListener( new OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {
            Toast.makeText( context, String.format( "ImageClicked: %s", item ), Toast.LENGTH_SHORT ).show();
        }

    });

    return v;
}

}

BTW i tried to change the emulator for the program to launch on to API level 7; it totally wrecked the program. Now at every R.whatever I get an error. does it matter that I changed it in the Manifest file to 7 too? Final thing I cleaned the project but that doesn’t make a difference – what’s the problem here?

  • 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-05-27T12:58:03+00:00Added an answer on May 27, 2026 at 12:58 pm

    I implemented some code and it works fine 🙂

    ListView mListView;
    EditText mValue;
    Button mAdd;
    
    List<String> data = new ArrayList<String>();
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_listview);
    
        data.add("Hello World");
    
        mListView = (ListView) findViewById(R.id.custom_list);
        mValue = (EditText) findViewById(R.id.text_to_add);
        mAdd = (Button) findViewById(R.id.add_string);
    
        mListView.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, data));
        mAdd.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View currentView) {
                data.add(new String(mValue.getText().toString()));
            }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into

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.