2012-01-03 64 views
1

我需要在列表視圖中顯示我的數據列表數據。我的數據列表是public static ArrayList<ArrayList<String>> ourstringList1。在我的listadapter類我試圖從arraylist中獲取數據並將其設置爲tesxtview。但因爲我需要arr.get(i).get(j)..。我無法繼續前進。 請幫我對此...如何將arraylist值設置爲android中的listview?

我的代碼: 公共類testreview延伸活動{

private ListView listViewScore = null; 
private ListViewAdapter listViewAdapter = null; 
public static ArrayList<ArrayList<String>> ourstringList1 = Select.stringList1; 
    private ArrayList<ArrayList<String>> usernameArrLst = ourstringList1; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.list); 
    listViewScore=(ListView)findViewById(R.id.list); 
    usernameArrLst = new ArrayList<ArrayList<String>>(); 
    listViewAdapter = new ListViewAdapter(); 
    listViewScore.setAdapter(listViewAdapter); 
} 

class ListViewAdapter extends BaseAdapter{ 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     if(usernameArrLst==null){ 
      return 0; 
     } 

     return usernameArrLst.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return usernameArrLst.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public View getView(int position, View view, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     View rowView=view; 
     if(rowView==null){ 
      LayoutInflater layoutinflate =LayoutInflater.from(testreview.this); 
      rowView=layoutinflate.inflate(R.layout.listrow, parent, false); 
     } 

     TextView textViewName=(TextView)rowView.findViewById(R.id.tv_case); 
     textViewName.setText((CharSequence) usernameArrLst.get(position)); 

     return rowView; 
    } 

} 

}

在此先感謝

+0

重新使用節標題? – 2012-01-03 09:57:29

+0

描述你希望數據顯示在你的列表視圖中,因爲你基本上有一個數據網格,你可以在一個列表中顯示所有的值,讓每個ArrayList位於列表的單獨標題/部分之下......等等 – 2012-01-03 09:59:51

+0

嗨我正在測驗應用程序。在我的數據列表中,數據存儲爲行和列。所以我需要獲得第1行第1列,然後我必須將其設置爲在listview中的textview。我怎樣才能做到這一點? – RaagaSudha 2012-01-03 10:09:33

回答

0

爲此,我認爲你需要編寫您的自定義適配器並將其設置爲列表
Get The Idea

希望這會幫助你。

+0

ListAdapter adapter = new ArrayAdapter >>(this,android.R.layout.simple_list_item_1,ourstringList1);我已經給出了這樣的,但它沒有采取3個參數... – RaagaSudha 2012-01-03 12:36:53

0

糖果如果u我們展示你已經嘗試了什麼,那麼它應該已經better.But按我的理解,我們的代碼看起來應該是這樣:

public class TestProjeectActivity extends Activity { 

    private ListView listViewScore = null; 
    private ListViewAdapter listViewAdapter = null; 
    private String[] usernameArr = null; 

     private ArrayList<String> usernameArrLst = null; 

    //private Helper helper = null; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

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

     //helper = new Helper(TestProjeectActivity.this); 
     //usernameArr = helper.getUserName(); 


     usernameArr = new String[]{"Alan","Bob"}; 

     usernameArrLst = new ArrayList<String>(Arrays.asList(usernameArr));//Changed line 


     listViewAdapter = new ListViewAdapter(); 
     listViewScore.setAdapter(listViewAdapter); 
    } 

    class ListViewAdapter extends BaseAdapter{ 

     @Override 
     public int getCount() { 
      // TODO Auto-generated method stub 
      if(usernameArrLst==null){ 
       return 0; 
      } 

      return usernameArr.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      // TODO Auto-generated method stub 
      return usernameArrLst.get(position); 
     } 

     @Override 
     public long getItemId(int position) { 
      // TODO Auto-generated method stub 
      return position; 
     } 

     @Override 
     public View getView(int position, View view, ViewGroup parent) { 
      // TODO Auto-generated method stub 
      View rowView=view; 

      if(rowView==null){ 
       LayoutInflater layoutinflate =LayoutInflater.from(TestProjeectActivity.this); 
       rowView=layoutinflate.inflate(R.layout.listviewtext, parent, false); 
      } 

      TextView textViewName=(TextView)rowView.findViewById(R.id.textViewName); 



      textViewName.setText(usernameArr.get(position)); 

      return rowView; 
     } 

    } 
} 
+0

嗨感謝您的迴應..我得到listViewScore.setAdapter(listViewAdapter)附近的錯誤; ...你能告訴我爲什麼它顯示異常接近line..do我們需要創建setadapter類? – RaagaSudha 2012-01-04 07:32:20

+0

@Sweety你可以讓我知道什麼是異常....不,我們不需要創建setAdapter類 – Maverick 2012-01-04 08:11:43

+0

@Sweety我編輯了代碼請嘗試並讓我知道 – Maverick 2012-01-04 08:20:51

0

對不起,沒看到你有鱗片狀的ArrayList。爲了獲得你需要的元素,使用類轉換來到達內部的ArrayList並迭代它們。

ArrayList<ArrayList<String>> stringList; 

stringList = ourStringList1; 

for (int i = 0; i < stringList.size(); i++) { 
    ArrayList<String> innerStringList = (ArrayList<String>) stringList.get(i); 

    for (int j = 0; j < innerStringList.size(); j++) { 
     String value = (String) innerStringList.get(j); 

     // put the value in the textView 
    } 
} 

當你建立你的適配器類,創建一個類的屬性,將持有的ArrayList陣列和在構造函數初始化。

希望這會有所幫助。如果你需要進一步的解釋讓我知道。

相關問題