gpt4 book ai didi

android - 分离 EditText

转载 作者:行者123 更新时间:2023-11-30 02:33:43 31 4
gpt4 key购买 nike

在我从数据库中读取的代码中,我还使用自定义适配器打印出每一行,该行中有一个 texttview、2 个按钮和一个 edittext。这一切都很好,但是当按下按钮时,edittext 会递增或递减,有没有办法更改我的代码,以便每个 edittext 与其上方或下方的行分开?正如当我单击一行中的一个按钮时,edittext 将变为两个,而当我单击另一行中的一个按钮时,计数器不会从第一行移动到第二行。

这是自定义适配器

package com.example.rory.dbtest;

/**
* Created by Rory on 02/11/2014.
*/
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.pinchtapzoom.R;


public class CustomCursorAdapter extends CursorAdapter{

public int counter = 0;
private MyAdapterInterface mMyInterface;

public CustomCursorAdapter(Context context, Cursor c, MyAdapterInterface myInterface) {

super(context, c);
this.context = context;
this.mMyInterface = myInterface;
}
Context context;


@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// when the view will be created for first time,
// we need to tell the adapters, how each item will look
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.row, parent, false);

return retView;
}

public void bindView(View view, Context context, Cursor cursor) {
// here we are setting our data
// that means, take the data from the cursor and put it in views
TextView textViewPersonName = (TextView) view.findViewById(R.id.item1);
textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));

final EditText edit1 = (EditText) view.findViewById(R.id.runningTotal);

Button plusButton = (Button) view.findViewById(R.id.plusButton);
plusButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter++;
edit1.setText(Integer.toString(counter));
mMyInterface.updateEditText(counter);
}
});

final Button minusButton = (Button) view.findViewById(R.id.minusButton);
minusButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter--;
edit1.setText(Integer.toString(counter));
mMyInterface.updateEditText(counter);
}
});
}
}

这是主要 Activity

package com.example.rory.dbtest;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.pinchtapzoom.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;

public class MyActivity extends Activity implements MyAdapterInterface{

private CustomCursorAdapter customAdapter;
public ListView list1;

com.example.rory.dbtest.DBAdapter db = new com.example.rory.dbtest.DBAdapter(this);
public MyActivity mMyActivity;


//public EditText TotalCost;
//public EditText TotalLitres;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);

list1 = (ListView)findViewById(R.id.data_list);
db.open();

mMyActivity = this;

Button addBtn = (Button)findViewById(R.id.add);
addBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent i = new Intent(MyActivity.this, addassignment.class);
startActivity(i);
}
});

Button deleteBtn = (Button)findViewById(R.id.delete);
deleteBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent i = new Intent(MyActivity.this, Delete.class);
startActivity(i);
}
});

Button updateBtn = (Button)findViewById(R.id.update);
updateBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent i = new Intent(MyActivity.this, Update.class);
startActivity(i);
}
});


try {
String destPath = "/data/data/" + getPackageName() + "/databases/AssignmentDB";
File f = new File(destPath);
if (!f.exists()) {
CopyDB( getBaseContext().getAssets().open("mydb"),
new FileOutputStream(destPath));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


new Handler().post(new Runnable() {
@Override
public void run() {
//Log.d("test", "customadapter is " + customAdapter.toString());
//Log.d("test", "databaseHelper is " + databaseHelper.toString());
customAdapter = new CustomCursorAdapter(MyActivity.this, db.getAllRecords(), mMyActivity);
list1.setAdapter(customAdapter);
}
});

}

public void updateEditText(int value)
{
EditText myEditText = (EditText)findViewById(R.id.edit1);
EditText myEditText2 = (EditText)findViewById(R.id.edit2);
myEditText.setText(String.valueOf(value));
myEditText2.setText(String.valueOf(value));
}

private class DBAdapter extends BaseAdapter {
private LayoutInflater mInflater;
//private ArrayList<>

@Override
public int getCount() {

return 0;
}

@Override
public Object getItem(int arg0) {

return null;
}

@Override
public long getItemId(int arg0) {

return 0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {

return null;
}

}

public void CopyDB(InputStream inputStream, OutputStream outputStream)
throws IOException {
//---copy 1K bytes at a time---
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
}

这是我的 XML 行

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<TextView android:id="@+id/item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="item"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="30sp"
android:paddingBottom="5dp"/>

<Button
android:id="@+id/plusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/plusButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<EditText
android:id="@+id/runningTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/minusButton"
android:layout_toStartOf="@+id/minusButton"
android:layout_marginRight="30dp" />

<Button
android:id="@+id/minusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/minusButton"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

最佳答案

问题是你的计数器变量只存储最后一个值,每次你点击按钮时新值替换旧值。

我建议将计数器从 int 更改为 ArrayList

public ArrayList<Integer> counter;

一定要像这样在构造函数中初始化数组:

public CustomCursorAdapter(Context context, Cursor c, MyAdapterInterface myInterface) {

super(context, c);
this.context = context;
this.mMyInterface = myInterface;
counter = new ArrayList<Integer>();

//default all counters to 0
for(int i=0; i<cursor.getCount(); i++)
{
counter.add(0);
}
}

将点击监听器更改为如下所示:

plusButton.setOnClickListener(new View.OnClickListener() {          
private int counterPos;

public void onClick(View v) {
counterPos = counter.get(cursor.getPosition())
counterPos--;
counter.set(cursor.getPosition(), counterPos);
edit1.setText(Integer.toString(counterPos));
mMyInterface.updateEditText(counterPos);
}
});

minusButton.setOnClickListener(new View.OnClickListener() {
private int counterPos;

public void onClick(View v) {
counterPos = counter.get(cursor.getPosition())
counterPos++;
counter.set(cursor.getPosition(), counterPos);
edit1.setText(Integer.toString(counterPos));
mMyInterface.updateEditText(counterPos);
}
});

解释

      counterPos = counter.get(cursor.getPosition())

cursor.getPosition() 将返回列表中的当前行位置。我们将使用此位置编号来标识我们存储相关计数器值的 ArrayList 中的位置。我们获取此计数器的当前值并将其存储在变量 counterPos 中。

      counterPos++;
counter.set(cursor.getPosition(), counterPos);

我们递增/递减值,然后将关联的数组 Position 设置为这个新值

edit1.setText(Integer.toString(counterPos));
mMyInterface.updateEditText(counterPos);

然后您可以像上面那样重用这个变量用于显示目的

这是我在 StackOverFlow 上的第一个回答,希望对您有所帮助! :)

有任何问题请告诉我。

祝你好运,休。

关于android - 分离 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26914760/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com