1.11M Members

Android ListView problem

 
0
 

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?

 
0
 

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?

 
0
 

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

 
0
 

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);

    }
    
}
 
0
 

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();
    }
    
}
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article