1. 程式人生 > >listView優化_AlertDialog彈窗修改條目

listView優化_AlertDialog彈窗修改條目

  • 一.tml程式碼
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="${relativePackage}.${activityClass}" >
       <ListView 
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:id="@+id/lv">
       </ListView>
    </RelativeLayout>
    

 二.條目程式碼

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80dp">
	
    <ImageView 
        android:id="@+id/img"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:src="@drawable/ic_launcher"/>
	
    
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">
        <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#000"
        android:id="@+id/text_name"
        android:text="灰太狼"/>
        <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:textSize="18sp"
        android:text="價格:"/>
            <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text_price"
        android:textColor="#000"
        android:text="2200元"/>
        </LinearLayout>
        
    </LinearLayout>

</RelativeLayout>

三.彈窗輸入框.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <EditText 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ed_upd"
        android:hint="請輸入修改的價格"/>
	
</LinearLayout>

四.java程式碼

package com.example.list_upd;

public class Person {
	private String name;
	private int price;
	public Person() {
		// TODO Auto-generated constructor stub
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public Person(String name, int price) {
		super();
		this.name = name;
		this.price = price;
	}
}
package com.example.list_upd;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
	private List<Person> list;
	private ListView lv;
	private MainActivity.Madapter madapter;
	private EditText ed_upd;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//獲取控制元件
		lv=(ListView) findViewById(R.id.lv);
		//初始化資料
		initData();
		//自定義介面卡
		madapter=new Madapter();
		//設定介面卡
		lv.setAdapter(madapter);
		//條目點選事件,修改條目中的內容
		lv.setOnItemClickListener(new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view,
			final int position, long id) {
			//獲取AlertDialog中輸入框的檢視
			View inflate = View.inflate(MainActivity.this,R.layout.ed_view, null);
			//獲取AlertDialog中輸入框的控制元件
			ed_upd = (EditText) inflate.findViewById(R.id.ed_upd);
				AlertDialog alertDialog=new AlertDialog.Builder(MainActivity.this)
				//頭部
				.setTitle("調整商品價格")
				//內容,獲取的是條目的name值
				.setMessage(list.get(position).getName())
				//輸入框
				.setView(inflate)
				//確定事件
				.setPositiveButton("確定", new OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						//ed_upd.getText().toString()獲取輸入框的值
						//Integer.parseInt() 把String型別強轉成int型別
						//list.get(position).setPrice()獲取點選條目的list值,修改price的內容
						list.get(position).setPrice(Integer.parseInt(ed_upd.getText().toString()));
						//重新整理介面卡
						madapter.notifyDataSetChanged();
					}
				})
				.create();
				alertDialog.show();
					}
				});
	}
	private void initData() {
		list=new ArrayList<Person>();
		for (int i = 0; i <20; i++) {
			list.add(new Person("喜羊羊與灰太狼",2200));
		}
	}
	public class Madapter extends BaseAdapter{
		@Override
		public int getCount() {
			return list.size();
		}
		@Override
		public Object getItem(int position) {
			return list.get(position);
		}
		@Override
		public long getItemId(int position) {
			return position;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// 優化
			ViewHolder holder;
			if (convertView==null) {
				holder=new ViewHolder();
				//獲取條目佈局
				convertView=View.inflate(MainActivity.this,R.layout.wang, null);
				//複用控制元件
				holder.name=(TextView) convertView.findViewById(R.id.text_name);
				holder.price=(TextView) convertView.findViewById(R.id.text_price);
				//繫結
				convertView.setTag(holder);
			} else {
				holder=(ViewHolder) convertView.getTag();
			}
			//給複用控制元件設定值
			holder.name.setText(list.get(position).getName());
			holder.price.setText(list.get(position).getPrice()+"");
			return convertView;
		}
	}
	class ViewHolder{
		TextView name;
		TextView price;
	}
}