Android中一個單行與多行並存的listview

來源:互聯網
上載者:User

可以用來參考:

 

<?xml version="1.0" encoding="utf-8"?><br /><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> android:windowSoftInputMode="adjustUnspecified|stateHidden"<br /> ><br /><LinearLayout<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> android:orientation="vertical"<br /> ><br /><ListView<br />android:layout_width="fill_parent"<br />android:layout_height="380sp"<br />android:id="@+id/CarAddlistView"<br />android:paddingBottom="2sp"<br />></ListView><br /></LinearLayout><br /></ScrollView> 

 

java代碼:

import java.text.DateFormat;<br />import java.text.SimpleDateFormat;<br />import java.util.ArrayList;<br />import java.util.Calendar;<br />import java.util.Date;<br />import java.util.HashMap;<br />import java.util.List;<br />import java.util.Map;<br />import android.app.Activity;<br />import android.app.AlertDialog;<br />import android.app.DatePickerDialog;<br />import android.app.Dialog;<br />import android.app.ProgressDialog;<br />import android.content.Context;<br />import android.content.DialogInterface;<br />import android.content.Intent;<br />import android.os.Bundle;<br />import android.view.LayoutInflater;<br />import android.view.View;<br />import android.view.ViewGroup;<br />import android.view.WindowManager;<br />import android.view.View.OnClickListener;<br />import android.widget.AdapterView;<br />import android.widget.BaseAdapter;<br />import android.widget.DatePicker;<br />import android.widget.ImageButton;<br />import android.widget.LinearLayout;<br />import android.widget.ListView;<br />import android.widget.TextView;<br />import android.widget.Toast;<br />public class multiListview extends Activity{</p><p>private ListView listview;<br />private List<Map<String, Object>> DataList;<br />MyAdapter adapter;<br /> private Runnable viewOrders;</p><p> private int mYear;<br /> private int mMonth;<br /> private int mDay;</p><p>@Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br />//禁止休眠<br />getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,<br />WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);<br /> setContentView(R.layout.listview);</p><p> listview = (ListView)findViewById(R.id.listView);<br /> listview.setAdapter(adapter);<br /> listview.setOnItemClickListener(new ListView.OnItemClickListener(){<br />@Override<br />public void onItemClick(AdapterView<?> parent, View view, int position,<br />long arg3) {<br />setSelectedValues(position);<br />}<br /> });<br /> }</p><p> //選擇listview事件,處理相關邏輯<br /> private void setSelectedValues(int position)<br /> {<br /> Intent _Intent;<br /> Bundle bundle;<br />AreaOption area;<br />switch(position)<br />{<br />case 0://<br />Toast.makeText(multiListview.this, "請正確選擇您的日期1!",<br />Toast.LENGTH_SHORT).show();<br />showDialog(DATE1_DIALOG_ID);<br />break;<br />case 1://<br />Toast.makeText(multiListview.this, "請正確選擇您的日期2!",<br />Toast.LENGTH_SHORT).show();<br />showDialog(DATE2_DIALOG_ID);<br />break;<br />}<br /> }<br /> @Override<br /> protected Dialog onCreateDialog(int id) {<br /> switch (id) {<br /> case DATE1_DIALOG_ID:<br /> return new DatePickerDialog(this,<br /> mDateSetListener,<br /> mYear, mMonth, mDay);<br /> case DATE2_DIALOG_ID:<br /> return new DatePickerDialog(this,<br /> mDateSetListener1,<br /> mYear, mMonth, mDay);<br /> }<br /> return null;<br /> }<br /> private DatePickerDialog.OnDateSetListener mDateSetListener =<br /> new DatePickerDialog.OnDateSetListener() {<br /> public void onDateSet(DatePicker view, int year,<br /> int monthOfYear, int dayOfMonth) {<br /> mYear = year;<br /> mMonth = monthOfYear;<br /> mDay = dayOfMonth; </p><p> Map<String, Object> map = new HashMap<String, Object>();<br /> map.put("left1", "日期1:");<br /> map.put("center1", getDate());<br />map.put("star1", "*");<br />map.put("arrow1", ">"); </p><p> DataList.set(0, map);<br /> adapter.notifyDataSetChanged();<br /> }<br /> };</p><p> private DatePickerDialog.OnDateSetListener mDateSetListener1 =<br /> new DatePickerDialog.OnDateSetListener() {<br /> public void onDateSet(DatePicker view, int year,<br /> int monthOfYear, int dayOfMonth) {<br /> mYear = year;<br /> mMonth = monthOfYear;<br /> mDay = dayOfMonth; </p><p> Map<String, Object> map = new HashMap<String, Object>();<br /> map.put("left1", "日期2:");<br /> map.put("center1", getDate());<br />map.put("star1", "*");<br />map.put("arrow1", ">"); </p><p> DataList.set(1, map);<br /> adapter.notifyDataSetChanged();<br /> }<br /> }; </p><p> private List<Map<String, Object>> getData() {<br />List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();<br />//0<br />Map<String, Object> map = new HashMap<String, Object>();<br />map.put("left1", "日期1:");<br />map.put("center1", getDate());<br />map.put("star1", "*");<br />map.put("arrow1", ">");<br />list.add(map);<br />//1<br />map = new HashMap<String, Object>();<br />map.put("left1", "日期2:");<br />map.put("center1", getDate());<br />map.put("star1", "*");<br />map.put("arrow1", ">");<br />list.add(map);<br />//2<br />map = new HashMap<String, Object>();<br />map.put("left1", "地點1:");<br />map.put("center1", "請選擇");<br />map.put("star1", "*");<br />map.put("arrow1", ">");<br />list.add(map);<br />//3<br />map = new HashMap<String, Object>();<br />map.put("left1", "地點2:");<br />map.put("center1", "請選擇");<br />map.put("star1", "*");<br />map.put("arrow1", ">");<br />list.add(map);<br />//4<br />map = new HashMap<String, Object>();<br />map.put("left1", "體重:");<br />map.put("center1", "請選擇");<br />map.put("star1", "");<br />map.put("arrow1", ">");<br />list.add(map);<br />//5<br />map = new HashMap<String, Object>();<br />map.put("left1", "工作:");<br />map.put("center1", "請選擇");<br />map.put("star1", "*");</p><p>map.put("left2", "行動電話1:");<br />map.put("center2", "請選擇");<br />map.put("star2", "*");<br />map.put("arrow2", ">");</p><p>map.put("left3", "行動電話2:");<br />map.put("center3", "請選擇");<br />map.put("star3", "");<br />list.add(map);<br />//6<br />map = new HashMap<String, Object>();<br />map.put("left1", "其他:");<br />map.put("center1", "請選擇");<br />map.put("arrow1", ">");<br />list.add(map);</p><p>return list;<br />}</p><p>//獲得日期<br />private String getDate()<br />{<br />StringBuilder Date = new StringBuilder()<br /> .append(mYear).append("-")<br /> .append(mMonth + 1).append("-")<br /> .append(mDay).append(" "); </p><p>return Date.toString();<br />}</p><p> public final class ViewHolder{<br />public TextView left1;<br />public TextView left2;<br />public TextView left3;<br />public TextView center1;<br />public TextView center2;<br />public TextView center3;<br />public TextView star1;<br />public TextView star2;<br />public TextView star3;<br />public TextView arrow1;<br />public TextView arrow2;<br />public TextView arrow3;<br />public LinearLayout line1;<br />public LinearLayout line2;<br />public LinearLayout line3;<br />}</p><p>public class MyAdapter extends BaseAdapter{<br />private LayoutInflater mInflater;</p><p>public MyAdapter(Context context){<br />this.mInflater = LayoutInflater.from(context);<br />}<br />@Override<br />public int getCount() {<br />// TODO Auto-generated method stub<br />return DataList.size();<br />}<br />@Override<br />public Object getItem(int arg0) {<br />// TODO Auto-generated method stub<br />return null;<br />}<br />@Override<br />public long getItemId(int arg0) {<br />// TODO Auto-generated method stub<br />return 0;<br />}<br />@Override<br />public View getView(int position, View convertView, ViewGroup parent) {</p><p>ViewHolder holder = null;<br />if (convertView == null) {</p><p>holder=new ViewHolder(); </p><p>convertView = mInflater.inflate(R.layout.sourceaddlistview, null);<br />holder.left1 = (TextView)convertView.findViewById(R.id.left1);<br />holder.left2 = (TextView)convertView.findViewById(R.id.left2);<br />holder.left3 = (TextView)convertView.findViewById(R.id.left3);<br />holder.center1 = (TextView)convertView.findViewById(R.id.center1);<br />holder.center2 = (TextView)convertView.findViewById(R.id.center2);<br />holder.center3 = (TextView)convertView.findViewById(R.id.center3);<br />holder.star1 = (TextView)convertView.findViewById(R.id.star1);<br />holder.star2 = (TextView)convertView.findViewById(R.id.star2);<br />holder.star3 = (TextView)convertView.findViewById(R.id.star3);<br />holder.arrow1 = (TextView)convertView.findViewById(R.id.arrow1);<br />holder.arrow2 = (TextView)convertView.findViewById(R.id.arrow2);<br />holder.arrow3 = (TextView)convertView.findViewById(R.id.arrow3);<br />holder.line1=(LinearLayout)convertView.findViewById(R.id.line1);<br />holder.line2=(LinearLayout)convertView.findViewById(R.id.line2);<br />holder.line3=(LinearLayout)convertView.findViewById(R.id.line3);<br />convertView.setTag(holder);</p><p>}else {</p><p>holder = (ViewHolder)convertView.getTag();<br />}</p><p>holder.line2.setVisibility(convertView.GONE);<br />holder.line3.setVisibility(convertView.GONE);</p><p>holder.left1.setText((String)DataList.get(position).get("left1"));<br />holder.center1.setText((String)DataList.get(position).get("center1"));<br />holder.star1.setText((String)DataList.get(position).get("star1"));<br />holder.arrow1.setText((String)DataList.get(position).get("arrow1"));</p><p>if(position==5)<br />{<br />holder.left2.setText((String)DataList.get(position).get("left2"));<br />holder.center2.setText((String)DataList.get(position).get("center2"));<br />holder.star2.setText((String)DataList.get(position).get("star2"));<br />holder.arrow2.setText((String)DataList.get(position).get("arrow2"));</p><p>holder.left3.setText((String)DataList.get(position).get("left3"));<br />holder.center3.setText((String)DataList.get(position).get("center3"));<br />holder.star3.setText((String)DataList.get(position).get("star3"));<br />holder.arrow3.setText((String)DataList.get(position).get("arrow3"));</p><p>holder.line2.setVisibility(convertView.VISIBLE);<br />holder.line3.setVisibility(convertView.VISIBLE);<br />}<br />return convertView;<br />}<br />}<br />

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.