2012-05-08 2 views
2

답변을 얻으려면이 사이트를 샅샅이 뒤지고 있었는데, 이것에 대해 실제로 발견하지 못했습니다. 나는 sqlite 데이터베이스를 사용하여 입력 한 색상 이름으로 16 진수 값을 조회하는 안드로이드 응용 프로그램을 만들고 있습니다. 동적으로 TextView를 만들고 Text 및 텍스트 색상을 설정 한 다음 ArrayList에 추가 한 다음 ArrayList를 추가하고 있습니다. ListView에 추가되었습니다. 텍스트가 ListView에 나타나지만 해당 color 속성이 설정되어 있지 않습니다. 각 목록보기 항목에 대해 텍스트 색을 설정하는 방법을 찾고 싶습니다. 여기 내 코드는 지금까지 있습니다 :목록보기 항목의 텍스트보기의 텍스트 색을 설정 하시겠습니까? (안드로이드)

클래스 변수 :에서 onCreate에서

private ListView lsvHexList; 

private ArrayList<String> hexList; 
private ArrayAdapter adp; 

() : 내 단추 처리기에서

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.color2hex); 

    lsvHexList = (ListView) findViewById(R.id.lsvHexList); 

    hexList = new ArrayList<String>(); 

: 당신은 추가되지 않습니다

public void btnGetHexValueHandler(View view) { 

    // Open a connection to the database 
    db.openDatabase(); 

    // Setup a string for the color name 
    String colorNameText = editTextColorName.getText().toString(); 

    // Get all records 
    Cursor c = db.getAllColors(); 

    c.moveToFirst(); // move to the first position of the results 

    // Cursor 'c' now contains all the hex values 
    while(c.isAfterLast() == false) { 

     // Check database if color name matches any records 
     if(c.getString(1).contains(colorNameText)) { 

      // Convert hex value to string 
      String hexValue = c.getString(0); 
      String colorName = c.getString(1); 

      // Create a new textview for the hex value 
      TextView tv = new TextView(this); 
      tv.setId((int) System.currentTimeMillis()); 
      tv.setText(hexValue + " - " + colorName); 
      tv.setTextColor(Color.parseColor(hexValue)); 

      hexList.add((String) tv.getText()); 

     } // end if 

     // Move to the next result 
     c.moveToNext(); 

    } // End while 

    adp = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, hexList); 
    lsvHexList.setAdapter(adp); 


    db.close(); // close the connection 
    } 

답변

3

만든 TextView를 목록에 추가하면 목록에 문자열을 추가하기 만하면 TextVi에서 호출 한 메서드가 중요하지 않습니다. EW : 당신이해야 할 일은

 if(c.getString(1).contains(colorNameText)) { 
     // ... 
     TextView tv = new TextView(this); 
     tv.setId((int) System.currentTimeMillis()); 
     tv.setText(hexValue + " - " + colorName); 
     tv.setTextColor(Color.parseColor(hexValue)); 

     hexList.add((String) tv.getText()); // apend only the text to the list 
     // !!!!!!!!!!!!!! lost the TextView !!!!!!!!!!!!!!!! 
    } 

는 다른 배열의 색상을 저장하는 것입니다, 실제 목록보기를 만들 때, 목록에서 해당 값에 따라 각각의 텍스트 뷰의 색상을 설정합니다.

이렇게하려면 ArrayAdapter을 확장하고 TextView 색상 논리를 추가해야합니다.

+0

훌륭한 사운드입니다. 어떻게 코드별로 보입니까? 여기 초보자 프로그래머. = P –

+0

지금 ListAdapter 예제가 없지만 아래의 BaseAdapter 확장 링크를 보면 아이디어는 거의 동일합니다. https://github.com/BinyaminSharet/Icelandic-Memory-Game/blob/master /src/com/icmem/game/BoardGridAdapter.java – MByD

+0

정확히 도움이되지는 않습니다. 다른 누군가? –

관련 문제