2012-01-03 3 views
1

arraylist 데이터를 listview에 표시해야합니다. 제 arraylist는 public static ArrayList<ArrayList<String>> ourstringList1입니다. 내 listadapter 클래스에서 나는 arraylist에서 데이터를 가져 와서 그것을 tesxtview로 설정하려고합니다. 하지만 arr.get (i) .get (j) ..이 필요합니다. 더 이상 진행할 수 없습니다. 안드로이드에서 arraylist 값을 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

섹션 헤더를 다시 사용 하시겠습니까? –

+0

목록보기에 데이터를 표시하는 방법을 설명하십시오. 본질적으로 데이터 격자가 있으므로 모든 값을 하나의 목록에 표시하고 각 ArrayList를 목록의 별도 헤더/섹션 아래에 둘 수 있습니다. –

+0

안녕 나는 퀴즈 응용 프로그램에 wroking입니다. 내 arraylist에서 데이터는 행과 열로 저장됩니다. 그래서 1 행 1 열을 가져올 필요가 다음 listview textview 수 설정해야합니다. 내가 어떻게 할 수 있니? – RaagaSudha

답변

0

에서

감사합니다 사용자 지정 어댑터 작성 후 목록에 설정
Get The Idea

희망이 도움이 될 것입니다. u는 당신이 다음 시도는 UR 코드는 다음과 같이한다 나의 이해에 따라 better.But을 했어야 무엇을 우리에게 보여 주었다 경우

+0

ListAdapter 어댑터 = 새 ArrayAdapter >> (this, android.R.layout.simple_list_item_1, ourstringList1); 나는 이런 식으로 주어진 있지만 3 인수를 취하지 않습니다 ... – RaagaSudha

0

이쁜이는 :

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); 근처에 오류가 점점 왜 그게 근처에 예외를 표시하는 말해주십시오 우리는 setadapter 클래스를 생성해야합니까? – RaagaSudha

+0

@Sweety는 예외가 무엇인지를 알려주지 만 ... setAdapter 클래스를 생성하지 않아도됩니다. – Maverick

+0

@Sweety 코드 pls를 편집하여 알려주세요. – Maverick

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 배열을 보유하고 생성자에서 초기화 할 클래스 특성을 만듭니다.

희망이 도움이됩니다. 추가 설명이 필요하면 알려주세요.

관련 문제