我希望从数据库中读取一些产品详细信息,然后将它们添加到ListView中。
然后我想在每一行都有一个数量的EditText框,客户可以在其中添加数量。 我怎样才能做到这一点?我做了一个简单的页面,但当我输入一个数量并向下滚动然后再次备份我放松了数据,或者它甚至出现在另一行的另一个数字框中。
答案 0 :(得分:1)
好的,所以你需要做的第一件事就是为你希望列表中的每一行都有的布局创建一个Row.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="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/icon"
android:padding="2dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ok"
/>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
/>
//Add a edittext here..
/LinearLayout>
接下来,您需要扩展listview并覆盖get视图以加载到自定义行中。
public class Demo extends ListActivity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
setListAdapter(new Adapter());}
//Here extends a ArrayAdapter to create your custom view
class Adapter extends ArrayAdapter<String> {
Adapter() {
super(DynamicDemo.this, R.layout.row, R.id.label, items);
}
public View getView(int position, View convertView,
ViewGroup parent) {
//Here load in your views such as the edittext
}
这就是您开始所需的内容,然后您可以在用户点击该项目时调用onItemListClick()来获取每次点击。
你可以在这里获得完整的教程......
编辑:
此外,如果您想在数量框中保存数字,您需要拥有一个捆绑包。 如 saveState()方法
这将在应用程序处于活动状态时保存用户数量,并在返回视图时从数据包中提取数字或int。
这应该是有帮助的
http://www.edumobile.org/android/android-beginner-tutorials/state-persistence/
答案 1 :(得分:0)
您应该将状态(用户在EditText中输入的内容)保存在提供给列表适配器的某种数组中。您可能在列表适配器的getView()方法中创建了新的EditText。
答案 2 :(得分:0)
下面是我正在玩的代码
package sanderson.swords.mobilesales;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;
public class OrderProductSearch extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
setContentView(R.layout.orderproducts);
}
catch (Exception e) {
//
String shaw="";
shaw = e.getMessage();
}
//Create view of the list where content will be stored
final ListView listContent = (ListView)findViewById(R.id.orderproductlistview);
//Set for fast scrolling
listContent.setFastScrollEnabled(true);
//Create instance of the database
final DbAdapter db = new DbAdapter(this);
//Open the Database and read from it
db.openToRead();
//Routine to call all product sub groups from the database
final Cursor cursor = db.getAllSubGroupProduct();
//Manages the cursor
startManagingCursor(cursor);
//The columns we want to bound
String[] from = new String[]{DbAdapter.KEY_PRNAME,
DbAdapter.KEY_PRSIZE, DbAdapter.KEY_PKQTY};
//This is the id of the view that the list will be map to
int[] to = new int[]{R.id.productlinerow, R.id.productlinerow2, R.id.productlinerow3};
//Create simple cursor adapter
SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this, R.layout.productlinerow, cursor, from, to);
//Set the cursor to the list content view
listContent.setAdapter(cursorAdapter);
//Close the database
db.close();
//check if any orders are on the system
int check = cursor.getCount();
AlertDialog.Builder ps = new AlertDialog.Builder(OrderProductSearch.this);
final Button border = (Button) findViewById(R.id.orderqty);
//notify the user if there are no orders on the system
if (check == 0)
{
ps.setTitle("No Products Found");
ps.setMessage("There are no products in this group");
ps.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which)
{
OrderProductSearch.this.finish();
startActivity(new Intent("sanderson.swords.mobilesales.PRODUCTENQUIRY"));
}
});
ps.show();
}
border.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
try {
String clicked = "";
clicked = "neil shaw";
} catch (Exception e) {
String error = e.getMessage();
error = error + "";
}
}
});