2016-05-10 15 views
0

私は行1に見出しを入れ、行2に対応するテキストを置くことができるリストビューを動的に追加したいと思っています。 単純なアダプタを使って値をlistviewに入れました。代替色で複数行の動的リストビューを追加するにはどうすればよいですか?

これは私のJavaアクティビティです:

String serverResponse = ""; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.setContentView(R.layout.activity_holidays); 
    StrictMode.ThreadPolicy ourPolicy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
    StrictMode.setThreadPolicy(ourPolicy); 

    ActionBar actionbar = getSupportActionBar(); 

    actionbar.setDisplayHomeAsUpEnabled(true); 
    actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    actionbar.setCustomView(R.layout.actionbar_layout); 
    ((TextView)actionbar.getCustomView().findViewById(R.id.mytext)).setText("Holidays"); 


    HttpResponseCache response = null; 
    try { 
     // Create http client object to send request to server 
     HttpClient client = new DefaultHttpClient(); 
     // Create URL string 
     String URL = "http://ran.mantraeducation.in/push.aspx?user=ASA103101&pass=20041129&request=notice&MSelect=4"; 
     // Create Request to server and get response 
     HttpGet httpget= new HttpGet(); 
     httpget.setURI(new URI(URL)); 
     ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
     serverResponse = client.execute(httpget, responseHandler); 

     initList(); 
     ListView listView = (ListView) findViewById(R.id.listView); 


     SimpleAdapter simpleAdapter = new SimpleAdapter(this, noticeList, android.R.layout.simple_list_item_2, new String[] {"notice"}, new int[] {android.R.id.text1}); 
     listView.setAdapter(simpleAdapter); 


    } catch (URISyntaxException e) { 
     e.printStackTrace(); 
    } 
    catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater menuInflater = getMenuInflater(); 
    menuInflater.inflate(R.menu.activity_main_actions, menu); 
    return super.onCreateOptionsMenu(menu); 

} 

List<Map<String,String>> noticeList = new ArrayList<Map<String,String>>(); 
private void initList(){ 
    try{ 
     JSONArray jarray= new JSONArray(serverResponse); 
     for(int i=0; i<jarray.length(); i++) 
     { 
      JSONObject jo = jarray.getJSONObject(i); 
      String description = jo.getString("Description"); 
      String noticeDate = jo.getString("NoticeDate"); 
      String outPut = noticeDate + ": " +description; 
      noticeList.add(createNotice("notice", outPut)); 
     } 
    } 
    catch(Exception e){ 
     e.printStackTrace(); 
     Toast.makeText(getApplicationContext(), "Error" + e.toString(), Toast.LENGTH_LONG).show(); 
    } 
} 

private HashMap<String, String>createNotice(String name,String number){ 
    HashMap<String, String> noticeNameNo = new HashMap<String, String>(); 
    noticeNameNo.put(name, number); 
    return noticeNameNo; 
} 


@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    Intent dashboardIntent = new Intent(HolidaysActivity.this, ProfileActivity.class); 
    startActivity(dashboardIntent); 
    return super.onOptionsItemSelected(item); 
} 

}

これは私のXMLファイルです:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<ListView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/listView" 
    android:layout_weight="1" 
    android:background="@color/white" 
    android:divider="@color/colorPrimary" 
    android:dividerHeight="8px"/> 

+0

カスタムアダプターを使用する必要があります。そのためには、行のxmlファイルを作成し、ArrayAdapterのようなAdapterクラスを拡張する必要があります。今はコードサンプルを置くことはできませんが、後ほど詳細な答えを書くことができます。 – leojg

答えて

関連する問題