Hello, this is my code

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class AnimalsActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
    	String[] Animals = {"Cat","Dog","Horse","Pig"};
    	
        ListView lv = (ListView)findViewById(R.id.listv11);    
        ArrayAdapter<String> _adap;
        _adap = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Animals);         
        lv.setAdapter(_adap);

    }
    
}

The problem is with
ListView lv = (ListView)findViewById(R.id.listv11);

When i give LV ID
android:id="@+id/listView1"
IT DONT WORK

BUT - When i give
android:id="@+android:id/list"
It work, but the problem is, it take only an id
id:/list
So, i cant have 2 same ListView's :/ thats fail way...

Any suggestion?

Recommended Answers

All 4 Replies

Sorry your question doesn't make sense. "So, i cant have 2 same ListView's :/ thats fail way..." Can you explain what you trying to achieve?

Sorry your question doesn't make sense. "So, i cant have 2 same ListView's :/ thats fail way..." Can you explain what you trying to achieve?

there are some default ids wich i can use that way, so if i use that ids i will not have double ids? so... how i can have 2 ListViews when i can't give my ID

FIXED!

DAMN i was using ListActivity :/ ffs
Because lot of ppls are looking also for this ;p
This is solution ;p

main.xml (Add this part in your *.xml layout

<ListView
        android:id="@+id/MYLISTID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

*.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class AnimalsActivity extends [B]Activity [/B]{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
    	String[] Animals = {"Cat","Dog","Horse","Pig"};
    	
        ListView lv = (ListView)findViewById(R.id.MYLISTID);    
        ArrayAdapter<String> _adap;
        _adap = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Animals);        
        lv.setAdapter(_adap);

    }
    
}

Because there is an problem with

Toast.makeText(, , ,);

We cant use it directly, dunno how we can...
An way is using an function, something like this

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class AnimalsActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
    	String[] Animals = {"Cat","Dog","Horse","Pig"};
		
        final ListView _lv = (ListView)findViewById(R.id.listv11);    
        final ArrayAdapter<String> _adap = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Animals);        
        _lv.setAdapter(_adap);
        
        // Decleare event you want to catch
        _lv.setOnItemClickListener(new OnItemClickListener() {
        
		public void onItemClick(AdapterView<?> arg0, View view, int Position, long ID) {
					// Catch selected item by Position, you can also with ID
					Object ob = _adap.getItem(Position);
					/** Because we cannot use Toast here, and because we are clevers, 
					    we create an function and we send text down :), hehe */
					ShowToast(ob.toString());
			}
		});         
    }

    private void ShowToast(String Text) {
        Toast.makeText(this, Text, Toast.LENGTH_SHORT).show();
    }
    
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.