台灣最大程式設計社群網站
線上人數
913
 
會員總數:246713
討論主題:190131
歡迎您免費加入會員
討論區列表 >> Android APP 開發 >> BaseAdapter修改一筆資料的問題
[]  
[我要回覆]
回應主題 加入我的關注話題 檢舉此篇討論 將提問者加入個人黑名單
BaseAdapter修改一筆資料的問題
價值 : 70 QP  點閱數:1119 回應數:0
樓主

佚名
門外漢
0 5
224 5
發送站內信


圖片連結:https://drive.google.com/open?id=0B2tk1k2HlqOXdjZFaTF2ZTMyZFU

檔案連結:https://drive.google.com/open?id=0B2tk1k2HlqOXcE01ekd2cjIwZnc

程式大致長這個樣子
這個圖片是一個ListView的刪除例子,用自訂的BaseAdapter可以即時刪除特定筆數。
我想問的是如果修改顯示對話框,再對話框的結果即時刷新在listview顯示,
如果用mAppList.set
或是先mAppList.remove再mAppList.add,
這部份我想不通,網路上多半為新增和刪除的例子,有修改一筆資料的例子嗎?



public void removeItem(int position){
Log.d("FragmentAlertDialog", "按下x " + position);
mAppList.remove(position);
this.notifyDataSetChanged();
}
public void updateItem(int position, String inputInfo){

this.notifyDataSetChanged();
}




主程式:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {
static lvButtonAdapter listItemAdapter = null;
HashMap<String, Object> map = null;
ArrayList<HashMap<String, Object>> remoteWindowItem = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// 关联Layout中的ListView
ListView vncListView = (ListView)findViewById(android.R.id.list);
//版本資訊","筆記地圖版本","行蹤地圖版本","顯示筆記地圖","顯示行蹤地圖
// 生成动态数组,加入数据
remoteWindowItem = new ArrayList<HashMap<String, Object>>();
map = new HashMap<String, Object>();
map.put("ItemImage", R.drawable.firefox);
map.put("title", "0001");
map.put("info", "a");
map.put("ItemCloseWin", android.R.drawable.ic_menu_close_clear_cancel);
remoteWindowItem.add(map);
map.put("ItemImage", R.drawable.firefox);
map.put("title", "0002");
map.put("info", "b");
map.put("ItemCloseWin", android.R.drawable.ic_menu_close_clear_cancel);
remoteWindowItem.add(map);
map.put("ItemImage", R.drawable.firefox);
map.put("title", "0003");
map.put("info", "c");
map.put("ItemCloseWin", android.R.drawable.ic_menu_close_clear_cancel);
remoteWindowItem.add(map);

// 生成适配器的Item和动态数组对应的元素
listItemAdapter = new lvButtonAdapter(
this,
remoteWindowItem,
R.layout.lvitem,
new String[] {"ItemImage", "title", "info", "ItemCloseWin"},
new int[] {R.id.ItemImage, R.id.title, R.id.info, R.id.ItemCloseWin}
);
vncListView.setAdapter(listItemAdapter);
vncListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Toast.makeText(getApplicationContext(), "你選擇的是" + position, Toast.LENGTH_SHORT).show();
if(position == 0){
MyAlertDialog.select00(view);


}else if(position == 1){
MyAlertDialog.select04(view);
}else if(position == 4){
MyAlertDialog.select04(view);
}else{
MyAlertDialog.select04(view);
}

}
});
}
//
//
public static void doPositiveClick04() {
Log.d("FragmentAlertDialog", "按下o");
listItemAdapter.updateItem(0, "test");
}
//
public static void doNegativeClick04() {
Log.d("FragmentAlertDialog", "按下x");
listItemAdapter.removeItem(0);
}
//
//
}




listview的adapter:

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

public class lvButtonAdapter extends BaseAdapter {
private class buttonViewHolder {
ImageView appIcon;
TextView title;
TextView info;
ImageButton buttonClose;
}

private ArrayList<HashMap<String, Object>> mAppList;
private LayoutInflater mInflater;
private Context mContext;
private String[] keyString;
private int[] valueViewID;
private buttonViewHolder holder;

public lvButtonAdapter(MainActivity c, ArrayList<HashMap<String, Object>> appList, int resource,
String[] from, int[] to) {
mAppList = appList;
mContext = c;
mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
keyString = new String[from.length];
valueViewID = new int[to.length];
System.arraycopy(from, 0, keyString, 0, from.length);
System.arraycopy(to, 0, valueViewID, 0, to.length);
}

@Override
public int getCount() {
return mAppList.size();
}

@Override
public Object getItem(int position) {
return mAppList.get(position);
}

@Override
public long getItemId(int position) {
return position;
}
public void removeItem(int position){
Log.d("FragmentAlertDialog", "按下x " + position);
mAppList.remove(position);
this.notifyDataSetChanged();
}
public void updateItem(int position, String inputInfo){

this.notifyDataSetChanged();
}
//
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView != null) {
holder = (buttonViewHolder) convertView.getTag();
} else {
convertView = mInflater.inflate(R.layout.lvitem, null);
holder = new buttonViewHolder();
holder.appIcon = (ImageView)convertView.findViewById(valueViewID[0]);
holder.title = (TextView)convertView.findViewById(valueViewID[1]);
holder.info = (TextView)convertView.findViewById(valueViewID[2]);
holder.buttonClose = (ImageButton)convertView.findViewById(valueViewID[3]);
convertView.setTag(holder);
}

HashMap<String, Object> appInfo = mAppList.get(position);
if (appInfo != null) {
String title = (String) appInfo.get(keyString[1]);
String info = (String) appInfo.get(keyString[2]);
int mid = (Integer)appInfo.get(keyString[0]);
int bid = (Integer)appInfo.get(keyString[3]);
holder.title.setText(title);
holder.info.setText(info);
holder.appIcon.setImageDrawable(holder.appIcon.getResources().getDrawable(mid));
holder.buttonClose.setImageDrawable(holder.buttonClose.getResources().getDrawable(bid));
holder.buttonClose.setOnClickListener(new lvButtonListener(position));
}
return convertView;
}

class lvButtonListener implements View.OnClickListener {
private int position;
lvButtonListener(int pos) {
position = pos;
}

@Override
public void onClick(View v) {
int vid=v.getId();
if (vid == holder.buttonClose.getId()){
//removeItem(position);
}


if (vid == 0){

}else if (vid == 1){

}else if (vid == 2){

}else if (vid == 3){

}else if (vid == 4){

}else{

}


}
}
}



對話框的類別:

import android.app.AlertDialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.view.View;

public class MyAlertDialog extends DialogFragment {
public static StringBuilder sb = new StringBuilder();
public static String[] selectCon01 = {"google api",".com api"};
public static boolean[] check01 = new boolean[selectCon01.length];
public static String[] selectCon02 = {"google api",".com api"};
public static boolean[] check02 = new boolean[selectCon02.length];

public static String[] selectCon03 = {"全部","最近一個月","最近一星期","最近三天","最近一天"};
public static int check03 = -1;
public static String[] selectCon04 = {"全部","最近一個月","最近一星期","最近三天","最近一天"};
public static int check04 = -1;
//00
public static void select00(View view){
AlertDialog.Builder builder=new AlertDialog.Builder(view.getContext());
builder.setTitle("是否確定更新圖資?");
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
dialog.dismiss();
}
});
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
dialog.dismiss();

//呼叫下載程序
}
});
builder.show();
}
//01
public static void select01(View view){
AlertDialog.Builder builder=new AlertDialog.Builder(view.getContext());
builder.setTitle("請選擇要顯示的圖資");
/* builder.setSingleChoiceItems(selectCon01, check01, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
});*/
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
dialog.dismiss();
}
});
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
for(int n=0; n<check01.length; n++) {
if(check01[n]) {
sb.append(selectCon01[n] + " ");
}
}
dialog.dismiss();
for(int n=0; n<check01.length; n++) check01[n]=false;
sb=new StringBuilder();
}

});
builder.show();
}
//03
public static void select03(View view){
AlertDialog.Builder builder=new AlertDialog.Builder(view.getContext());
builder.setTitle("請選擇要顯示的資料周期");
builder.setSingleChoiceItems(
selectCon03,
-1, // does not select anything
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int index) {
switch (index) {
case 0:
check03 = 0;
break;
case 1:
check03 = 1;
break;
case 2:
check03 = 2;
break;
case 3:
check03 = 3;
break;
case 4:
check03 = 4;
break;
default:
break;
}
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
check03 = -1;
//Fragment04.doNegativeClick04();
dialog.dismiss();
}
});
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
//Fragment04.doPositiveClick04();

}

});
builder.show();
}
//
//04
public static void select04(View view){
AlertDialog.Builder builder=new AlertDialog.Builder(view.getContext());
builder.setTitle("請選擇要顯示的資料周期");
builder.setSingleChoiceItems(
selectCon04,
-1, // does not select anything
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int index) {
switch (index) {
case 0:
check04 = 0;
break;
case 1:
check04 = 1;
break;
case 2:
check04 = 2;
break;
case 3:
check04 = 3;
break;
case 4:
check04 = 4;

break;
default:
break;
}
//dialog.dismiss();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
check04 = -1;
MainActivity.doNegativeClick04();
dialog.dismiss();
}
});
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
MainActivity.doPositiveClick04();
dialog.dismiss();
}
});
builder.show();
}
//
//
//
//
//
}



lvitem.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:descendantFocusability="blocksDescendants" >

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:textStyle="bold"
android:textColor="@color/colorBlack"
android:textSize="20dip"
android:text="title"
/>

<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:textColor="@color/colorBlack"
android:textSize="14dip"
android:text="info"
/>
</LinearLayout>

<ImageView
android:id="@+id/ItemImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:visibility="gone"
/>


<ImageButton
android:id="@+id/ItemCloseWin"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#e0000000"
android:gravity="left|center_vertical"
android:focusable="false"
android:visibility="gone"
android:src="@android:drawable/ic_menu_close_clear_cancel"
/>


</RelativeLayout>



main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="10dip" android:orientation="vertical">

<ListView android:id="@id/android:list" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>


搜尋相關Tags的文章: [ BaseAdapter ] , [ set ] , [ update ] , [ 修改 ] ,
本篇文章發表於2017-04-26 20:58
目前尚無任何回覆
   

回覆
如要回應,請先登入.