这里我试图通过弹出对话框来保存数据并将数据保存到SQLite
数据库中点击listview
项目(每个listview
项目包含textView
和toggle
按钮,适配器生成的类似8项。
对话框包含edittext
字段,在输入数据并单击“确定”按钮时应创建数据库,同样应该是textview
项listview
的文本。
•我在更改textView
项listview
的数据以及设置数据库时遇到问题。任何建议都会有很大的帮助谢谢...
MainActivity.java
public class MainActivity extends ActionBarActivity implements
ListAdapter.customButtonListener {
String ipaddress, ipaddress1;
SharedPreferences sharedpreferences;
EditText ed1,ed2;
private ListView listView;
public static final String MY_PREFERENCES = "MyPreferences";
SharedPreferences myPreferences;
ListAdapter adapter;
ArrayList<String> dataItems = new ArrayList<String>();
ProgressDialog waitDialog;
AlertDialog.Builder alert;
String noteID;
TextView child;
NoteHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] dataArray = getResources().getStringArray(R.array.listdata);
List<String> dataTemp = Arrays.asList(dataArray);
dataItems.addAll(dataTemp);
ed1 = (EditText) findViewById(R.id.editText);
helper=new NoteHelper(this);
child=(TextView)findViewById(R.id.childTextView);
//alert = new AlertDialog.Builder(this);
listView = (ListView) findViewById(R.id.listView);
adapter = new ListAdapter(MainActivity.this, dataItems);
adapter.setCustomButtonListner(MainActivity.this);
listView.setAdapter(adapter);
registerForContextMenu(listView);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int i, long l) {
final Dialog dialog = new Dialog(MainActivity.this);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog.show();
dialog.getWindow().setAttributes(lp);
dialog.setContentView(R.layout.prompts);
dialog.setTitle("Update!!!");
final EditText userinput=(EditText)dialog.findViewById(R.id.editTextDialogUserInput);
Button Update = (Button) dialog.findViewById(R.id.dialogButtonOK);
Button Cancel=(Button)dialog.findViewById(R.id.dialogButtonCANCEL);
Update.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (userinput.getText() != null) {
String text=userinput.getText().toString();
//child.setText(userinput.getText().toString());
myPreferences = getSharedPreferences (MY_PREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = myPreferences.edit ();
editor.putString(MY_PREFERENCES,text);
editor.commit ();
myPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
String strUsername = myPreferences.getString(MY_PREFERENCES,text);
// child.setText(strUsername);
Toast.makeText(MainActivity.this, ""+userinput.getText()+"uploaded successfully", Toast.LENGTH_SHORT).show();
}
}
});
Cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
return true;
}
});
}
ListAdapter.java
public class ListAdapter extends ArrayAdapter<String> {
customButtonListener customListner;
public interface customButtonListener {
public void onButtonClickListner(int position,String value);
public void onClickListner(int position, String temp);
}
public void setCustomButtonListner(customButtonListener listener) {
this.customListner = listener;
}
private Context context;
private ArrayList<String> data = new ArrayList<String>();
public ListAdapter(Context context, ArrayList<String> dataItem) {
super(context, R.layout.row, dataItem);
this.data = dataItem;
this.context = context;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.childTextView);
viewHolder.button = (Button) convertView.findViewById(R.id.childButton);
viewHolder.buttonoff = (Button) convertView.findViewById(R.id.childButton1);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
final String temp = getItem(position);
viewHolder.text.setText(temp);
viewHolder.button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (customListner != null) {
customListner.onButtonClickListner(position,temp);
}
}
});
viewHolder.buttonoff.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (customListner != null) {
customListner.onClickListner(position, temp);
}
}
});
return convertView;
}
public class ViewHolder {
TextView text;
Button button;
Button buttonoff;
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/editText"
>
</ListView>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:hint="IP ADDRESS"
android:textAlignment="center"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
row.xml
<TextView
android:id="@+id/childTextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:editable="true"
android:layout_alignParentLeft="true"
android:layout_toStartOf="@+id/childButton" />
<Button
android:id="@+id/childButton"
android:layout_width="55dp"
android:layout_height="55dp"
android:text="ON"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="61dp" />
<Button
android:id="@+id/childButton1"
android:layout_width="55dp"
android:layout_height="55dp"
android:text="OFF"
android:layout_alignTop="@+id/childButton"
android:layout_alignParentEnd="true" />
prompts.xml
<EditText
android:id="@+id/editTextDialogUserInput"
android:layout_width="match_parent"
android:layout_height="50dp"
android:editable="true"
android:layout_alignParentStart="true">
<requestFocus/>
</EditText>
<Button
android:id="@+id/dialogButtonOK"
android:layout_width="105dp"
android:layout_height="60dp"
android:text="UPDATE"
android:layout_marginRight="5dp"
android:layout_marginStart="56dp"
android:layout_below="@+id/editTextDialogUserInput"
android:layout_alignParentStart="true" />
<Button
android:id="@+id/dialogButtonCANCEL"
android:layout_width="105dp"
android:layout_height="60dp"
android:text=" CANCEL "
android:layout_marginRight="5dp"
android:layout_below="@+id/editTextDialogUserInput"
android:layout_toEndOf="@+id/dialogButtonOK" />
MainActivity.java
下面是listView
项中按钮的onClickListener代码@Override
public void onClickListner(int position, String temp,View view) {
int position1 = Integer.parseInt(view.getTag().toString());
final Dialog dialog1 = new Dialog(MainActivity.this);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog1.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog1.show();
dialog1.getWindow().setAttributes(lp);
dialog1.setContentView(R.layout.prompts);
dialog1.setTitle("Update!!!");
final EditText userinput=(EditText)dialog1.findViewById(R.id.editTextDialogUserInput);
Button Update = (Button) dialog1.findViewById(R.id.dialogButtonOK);
Button Cancel=(Button)dialog1.findViewById(R.id.dialogButtonCANCEL);
Update.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//int position = Integer.parseInt(v.getTag().toString());
String text=userinput.getText().toString();
if(text!=null) {
// If you want to find the textView sibling of the button you clicked and change its text
//Null pointer exception occured
((TextView) ((View) v.getParent()).findViewById(R.id.childTextView)).setText(text);
}
}
});
}
答案 0 :(得分:0)
在您的活动中设置onClick方法并将其挂钩到xml中。像这样的东西
<Button
android:id="@+id/childButton1"
android:layout_width="55dp"
android:layout_height="55dp"
android:text="OFF"
android:onclick="onChildButton1"
android:layout_alignTop="@+id/childButton"
android:layout_alignParentEnd="true" />
在listView适配器实现中为此按钮设置标记
holder.childButton1.setTag(position);
您可以将与此行相关的任何信息设置为执行点击操作时可能需要的标记。
在您的活动中实施具有listView
的方法 public void onChildButton1(View view) {
// Do your Dialog action here
// If you want to figure out the position of the item of button just do this
int position = Integer.parseInt(view.getTag().toString())
// If you want to find the textView sibling of the button you clicked and change its text
((TextView)((View) view.getParent()).findViewById(R.id.text_view_id)).setText("Your string");
}
修改强>
您需要获取列表项的TextView而不是对话框提示
该功能应如下所示。
public void onChildButton1 (final View childButton1) {
final Dialog dialog1 = new Dialog(MainActivity.this);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog1.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog1.show();
dialog1.getWindow().setAttributes(lp);
dialog1.setContentView(R.layout.prompts);
dialog1.setTitle("Update!!!");
final EditText userinput=(EditText)dialog1.findViewById(R.id.editTextDialogUserInput);
Button Update = (Button) dialog1.findViewById(R.id.dialogButtonOK);
Button Cancel=(Button)dialog1.findViewById(R.id.dialogButtonCANCEL);
Update.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//int position = Integer.parseInt(v.getTag().toString());
String text=userinput.getText().toString();
if(text!=null) {
// If you want to find the textView sibling of the button you clicked and change its text
//Null pointer exception resolved
((TextView) ((View) childButton1.getParent()).findViewById(R.id.childTextView)).setText(text);
}
}
});
}