简体   繁体   English

使用对话框在列表视图项的文本视图和数据库中保存数据

[英]saving data on textview of listview item and in database using dialog

Here I m trying to save and store data in to SQLite database by poping up the dialog onLong click on the listview item (Each listview items contains textView and toggle button, similar 8 items generated by adapter).在这里,我试图通过在listview项上弹出对话框来将数据保存和存储到SQLite数据库中(每个listview项都包含textViewtoggle按钮,类似于适配器生成的 8 个项)。

Dialog contains edittext field, on entering the data and clicking on OK Button should create database and same should be the text of textview of that item of listview only.对话框包含edittext字段,在输入数据并单击 OK 按钮时应该创建数据库,并且相同的应该是listview的那个项目的textview的文本。

• I m facing problem in changing the data of the textView of listview item and also in setting up database. • 我在更改listview项的textView数据和设置数据库时textView问题。 Any suggestion will be of great help Thank you...任何建议都会有很大帮助谢谢...

MainActivity.java主活动.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活动_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行.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提示.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主活动.java

below is the code of onClickListener of the button in item of listView下面是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);
            }
        }
    });
}

Setup the onClick method in your activity and hook it in the xml.在您的活动中设置 onClick 方法并将其挂钩到 xml 中。 Something like this像这样的东西

<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" />

Set a tag to this button in the listView adapter implementation在 listView 适配器实现中为这个按钮设置一个标签

    holder.childButton1.setTag(position);

You can setup any information related to this row as a tag that you might require to perform the action on click.您可以将与此行相关的任何信息设置为执行点击操作可能需要的标签。

Implement the method in your activity which has the listView在您的活动中实现具有 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");

 }

EDIT编辑

You need to get the TextView of the list item not the dialog prompt您需要获取列表项的 TextView 而不是对话框提示

The function should look like this.该函数应如下所示。

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);


        }
    }
});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM