Button in Listview zwingen

  • 0 Antworten
  • Letztes Antwortdatum
G

guguli

Neues Mitglied
0
Hallo zusammen

ich hab ein clickable Listview. der Inhalt der Listview soll aus der XML file in values geholt werden. DIese soll verscheidene Farben beinhalten.
ich hab das mit String_array gemacht, damit kann ich aber nur der name der jeweiligen Farben ausgeben. ich will aber die Farbe selber ausgeben.

ich poste schon ma mein Code:

die MapActivity ist :
Code:
public  class MapActivity extends Activity  {
	
		private DBAdapter thisTestDBAdapter;
		
	    /** Called when the activity is first created. */
	    @Override
	    public void onCreate(Bundle savedInstanceState) 
	    {   	
	    	thisTestDBAdapter = new DBAdapter(this);
	        
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_map);
			
	 		fillData();
	        
	    }
	    
		
	    public void myClickHandler(View v) 
	    {
	    	  
	    	//reset all the listView items background colours before we set the clicked one..
	    	ListView lvItems = getListView();
	    	for (int i=0; i<lvItems.getChildCount(); i++) 
	    	{
				lvItems.getChildAt(i).setBackgroundColor(Color.BLUE);		
			}
	    	
	    	
	    	//get the row the clicked button is in
	    	LinearLayout vwParentRow = (LinearLayout)v.getParent();
	 		
	    	TextView child = (TextView)vwParentRow.getChildAt(0);
	    	Button btnChild = (Button)vwParentRow.getChildAt(1);
	    	btnChild.setText(child.getText());
	    	btnChild.setText("I've been clicked!");
	    	
			int c = Color.CYAN;
			
			vwParentRow.setBackgroundColor(c); 
			vwParentRow.refreshDrawableState();
	    	
	    }
	    
	    private ListView getListView() {
			// TODO Auto-generated method stub
			return null;
		}


	
		private void fillData() {
	    	Cursor coloursCursor;

				coloursCursor = thisTestDBAdapter.fetchAllEntriesForTable();	    		
				startManagingCursor(coloursCursor);
	        String[] from = new String[]{DBAdapter.KEY_TITLE, DBAdapter.KEY_ROWID};
	        int[] to = new int[]{R.id.tvViewRow};
     
	        SimpleCursorAdapter colours = new SimpleCursorAdapter(this, R.layout.view_row, coloursCursor, from, to);
	        setListAdapter(colours);
	       
	    }


		private void setListAdapter(SimpleCursorAdapter colours) {
			// TODO Auto-generated method stub
			
		}


		
	}

XML dazu ist dann:
Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<ListView
        android:id="@+id/android:list"
        android:layout_width="82dp"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:entries="@array/colours" >
    </ListView>
    <TextView
        android:id="@+id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="" />

    
	
</LinearLayout>
ICh hab auch eine Klasse databaseadapter:
Code:
public class DBAdapter {

	
	public static final String KEY_ROWID = "_id";
	public static final String KEY_TITLE = "title";
	    
	   
	 private static final String TAG = "DbAdapter";
    
	 private DatabaseHelper mDbHelper;
	 private SQLiteDatabase mDb;
    
    
	private static final String DATABASE_NAME = "droidTest1d";   
	private static final String table1 = "table1";    
	private static final int DATABASE_VERSION = 5;
    
    
/**
     * Database creation sql statement
     */
    
	private static final String DATABASE_CREATE1 =
	        " create table " + table1 +
	 
	        " (_id integer primary key autoincrement," +
	       " title text);";
	
	    
	private final Context mCtx;
    
    

    
	private static class DatabaseHelper extends SQLiteOpenHelper {
		
		DatabaseHelper(Context context) 
	{
            
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
       
	}
        

        
		@Override        
		public void onCreate(SQLiteDatabase db) {
		        	
			db.execSQL(DATABASE_CREATE1);
		      
		 }
		
		        
		@Override	       
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		            
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "		                    
			+ newVersion + ", which will destroy all old data");		            
			db.execSQL("DROP TABLE IF EXISTS " + table1 + ";");				            
			onCreate(db);
		        
		}
	}
	    
	
	    
	public DBAdapter(Context ctx) {
	        
		this.mCtx = ctx;
	}
	
	
	    
	    
	public DBAdapter open() throws SQLException {	        
		mDbHelper = new DatabaseHelper(mCtx);	        
		mDb = mDbHelper.getWritableDatabase();        
		return this;
	 }
	    
	    
	    
	public void close() {        
		mDbHelper.close();
	    }
	
	public long insertEntryTable(String colourName) {
	        
		ContentValues initialValues = new ContentValues();		        
		initialValues.put(KEY_TITLE, colourName);		        		        
		return mDb.insert(table1, null, initialValues);
	}
	    
	
	
	   
	 public boolean deleteEntryTable1(long rowId) {
	
		 return mDb.delete(table1, KEY_ROWID + "=" + rowId, null) > 0;
	    }
	 
	
	    
	   
	 public Cursor fetchAllEntriesForTable(){
			if (mDb == null){
				this.open();
			}		
			return mDb.query("table1", new String[] { KEY_ROWID, KEY_TITLE}, null, null, null, null, null);				
	    }	
}

Die Zeilen der Listview mache ich dann mit view_row.xml
hier wollte ich ursprunglich in jeder Zeile ein Button einfügen. hats aber auch nicht geklappt. :(
Code:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
	
	<TextView android:text="this is a row"		
	android:id="@+id/tvViewRow"		
	android:layout_width="wrap_content"		
	android:layout_height="wrap_content">	
	</TextView>	
	<Button android:text="Click me!"		
	android:id="@+id/BtnToClick"		
	android:layout_width="wrap_content"		
	android:layout_height="wrap_content"		
	android:onClick="myClickHandler">
	</Button>	


</LinearLayout>

zum schluss die Farben hab ich dann in res\values\color.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, droidTest1!</string>
<string name="app_name">droidTest1</string>

<string-array name="colours">
<item>Red</item>
<item>Blue</item>
<item>White</item>
<item>Yellow</item>
<item>Black</item>
<item>Green</item>
<item>Purple</item>
<item>Orange</item>
<item>Grey</item>
</string-array>
</resources>

Ich weiss nicht wie ich ein Array machen kann wo ich dann in meiner Listvew die Farben selber ausgeben und nicht die Namen ...


auf jeden Tipp würde mich freuen :)
ICh saß stünden dran und hab nichts gefunden. :(
THX
 
Zurück
Oben Unten