2013-07-26 31 views
0

Eclipse給我錯誤此行 ArrayAdapter myAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_2,myArray);Java文件錯誤(Android)

下面是代碼:

package com.dbapp.activities; 

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

public class SecondActivity extends Activity { 

int i,j; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.second); 

     String[][] myArray=new String[10][10]; 
     ListView myListView=(ListView)findViewById(R.id.lv1); 



     CRUD crud=new CRUD(this); 
     crud.openDb(); 

     Cursor cursor= crud.readDbRows(); 
     cursor.moveToFirst(); 

     i=j=0; 
     while (cursor.moveToNext()) 
     { 
     myArray[i][0]=cursor.getString(cursor.getColumnIndex("column1")); 
     myArray[i][1]=cursor.getString(cursor.getColumnIndex("column2")); 
     } 

     ArrayAdapter<String> myAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_2,myArray); 
     myListView.setAdapter(myAdapter); 
    } 

} 

這不是主要的活動,它只是另一項活動。

+0

後logcat的...... – KOTIOS

+0

你得到什麼錯誤? – Shiv

回答

0

不知道你是在哪個API,但是從API級別17,你可以做這樣的事情:

myAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_2, myArray){ 
      @Override 
      public View getView(int position, View convertView, ViewGroup parent){ 
       TwoLineListItem row;    
       if(convertView == null){ 
        LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        row = (TwoLineListItem)inflater.inflate(android.R.layout.simple_list_item_2, null);      
       }else{ 
        row = (TwoLineListItem)convertView; 
       } 
       BasicNameValuePair data = list.get(position); 
       row.getText1().setText(myArray[position][0]); 
       row.getText2().setText(myArray[position][1]); 

       return row; 
      } 
     }; 
    myListView.setAdapter(myAdapter); 
+0

我有API級別8 – Ajinkya

+0

@AJZ是的,這只是一個建議,因爲你沒有指定你正在使用的API級別。關於你的錯誤 - 發佈錯誤,以便我們能夠理解你在做什麼錯誤 –

+0

數組索引超出界限。 (如在logcat中看到的)。 – Ajinkya

1

好,看來ArrayAdapter將1D數組作爲參數,而不是2D。

因此,我將代碼更改爲1D數組,並找到答案。

我不得不改變字符串數組到\

ArrayList<String> myArray = new ArrayList<String>(); 

,並添加使用以下行內容:

myArray.add(cursor.getString(cursor.getColumnIndex("name"))); 

繼承人的Java代碼

package com.example.databasedemo; 

import java.util.ArrayList; 

import com.example.databasedemo.CRUD; 

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

public class SecondActivity extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
    setContentView(R.layout.second); 


    ArrayList<String> myArray = new ArrayList<String>(); 
    ListView listv=(ListView)findViewById(R.id.dblist); 

    CRUD crud=new CRUD(this); 
    crud.openDb(); 
    Cursor cursor= crud.readDbRows(); 
    cursor.moveToFirst(); 


    while(cursor.moveToNext()) 
    { 
     myArray.add(cursor.getString(cursor.getColumnIndex("name"))); 

    } 



    ArrayAdapter<String> myAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myArray); 
    listv.setAdapter(myAdapter); 

    crud.closeDb(); 
} 
}