我正在使用自定义适配器类,并希望向其添加TextView并在简单的Activity
中显示User_cart.java it is my activity
public class User_Cart extends FragmentActivity
{
CartAdapter adapter;
ListView listview;
ArrayList<HashMap<String, String>> arraylist;
public static String DISH_NAME;
public static String DISH_COST;
public static String DISH_QUANTITY;
Button order_btn;
TextView result;
Double quantitySTRtemp, costSTRtemp, totalSTR, subtotalSTR;
private CoordinatorLayout coordinatorLayout;
DatabaseHandler db = new DatabaseHandler(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_cart);
result = (TextView) findViewById(R.id.subtotalET);
order_btn = (Button) findViewById(R.id.orderBT);
arraylist = new ArrayList<HashMap<String, String>>();
List<Order> orders = db.getAllOrders();
Log.d("tot", "" + db.getOrdersCount());
//db.deleteall();
for (Order cn : orders) {
String log = "Id: " + cn.getID() + " ,Name: " + cn.getName() + " ,Cost: " + cn.getCost() + " Quantoty: " + cn.getQuantity();
// Writing Contacts to log
/* DISH_NAME = cn.getName();
DISH_COST = cn.getCost();
DISH_QUANTITY = cn.getQuantity();*/
Log.d("Name: ", log);
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", cn.getName());
map.put("cost", cn.getCost());
map.put("quantity", cn.getQuantity());
arraylist.add(map);
// listview.getSelectedItem();
}
listview = (ListView) findViewById(R.id.userlistview);
// Pass the results into ListViewAdapter.java
adapter = new CartAdapter(User_Cart.this, arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
Toolbar toolbar = (Toolbar) findViewById(R.id.menus_toolbar);
toolbar.setNavigationIcon(R.mipmap.backbutton);
calculateSum();
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
order_btn.setOnClickListener(new View.OnClickListener() {
boolean showText = false;
@Override
public void onClick(View v) {
Toast.makeText(User_Cart.this, "Thank You For order", Toast.LENGTH_LONG).show();
db.deleteall();
Intent intent = new Intent(User_Cart.this, MainActivity.class);
startActivity(intent);
}
});
}
@Override
public void onBackPressed() {
//FragmentManager fm = User_Cart.this.getSupportFragmentManager();
// fm.popBackStack();
//getFragmentManager().popBackStackImmediate();
super.onBackPressed();
}
public void calculateSum() {
double sum = 0;
List<Order> orders = db.getAllOrders();
for (Order cn : orders) {
try {
//quantitySTR = new Double(et_dish_quan.getText().toString());
quantitySTRtemp = Double.valueOf(cn.getQuantity());
} catch (NumberFormatException e) {
quantitySTRtemp = 0.0; // your default value
}
try {
//costSTR = new Double(et_menucost.getText().toString());
costSTRtemp =Double.valueOf(cn.getCost());
} catch (NumberFormatException e) {
costSTRtemp = 0.0; // your default value
}
Double resultSTR = quantitySTRtemp * costSTRtemp;
sum = sum + (resultSTR);
}
Log.d("sum",""+sum);
}
**CartAdapter.java**
public View getView(final int position, View convertView, ViewGroup parent)
{
// Declare Variables
final TextView et_menuname, et_menucost, et_dish_quan;
Button btn_increment, btn_decrement;
final TextView totalET, costET, QuantityET;
ImageView img_dishtype;
final String rupee;
rupee = context.getString(R.string.Rs);
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.usercustomlistview, parent, false);
resultp = data.get(position);
btn_increment = (Button) itemView.findViewById(R.id.increment_btn);
btn_decrement = (Button) itemView.findViewById(R.id.decrement_btn);
totalET = (TextView) itemView.findViewById(R.id.ptotal);
et_menuname = (TextView) itemView.findViewById(R.id.Dish_NameET);
et_menucost = (TextView) itemView.findViewById(R.id.dish_costET);
et_dish_quan = (TextView) itemView.findViewById(R.id.Dish_quantityET);
et_menuname.setText(resultp.get("name"));
et_menucost.setText(resultp.get("cost"));
et_dish_quan.setText(resultp.get("quantity"));
try {
//quantitySTR = new Double(et_dish_quan.getText().toString());
quantitySTR = Double.parseDouble(et_dish_quan.getText().toString());
} catch (NumberFormatException e) {
quantitySTR = 1.0; // your default value
}
try {
//costSTR = new Double(et_menucost.getText().toString());
costSTR = Double.parseDouble(et_menucost.getText().toString());
} catch (NumberFormatException e) {
costSTR = 1.0; // your default value
}
resultSTR = costSTR * quantitySTR;
stringdouble = Double.toString(resultSTR);
totalET.setText(stringdouble);
empname = (resultp.get("name"));
btn_increment.setOnClickListener(new View.OnClickListener() {
boolean showText = false;
@Override
public void onClick(View v) {
resultp = data.get(position);
counter += 1;
showText = true;
if (counter == 1 || counter < 1) {
et_dish_quan.setText("1");
} else {
et_dish_quan.setText(String.valueOf(counter));
}
try {
quantitySTR = new Double(et_dish_quan.getText().toString());
} catch (NumberFormatException e) {
quantitySTR = 1.0; // your default value
}
try {
costSTR = new Double(et_menucost.getText().toString());
} catch (NumberFormatException e) {
costSTR = 1.0; // your default value
}
resultSTR = costSTR * quantitySTR;
final String stringdouble = Double.toString(resultSTR);
totalET.setText(stringdouble);
//db.updateOrder(TABLE_ORDERS, cv, "_id=" + id, null);
tempcost = Double.toString(costSTR);
tempquan = Double.toString(counter);
db.addOrder(new Order(empname, tempcost, tempquan));
}
});
btn_decrement.setOnClickListener(new View.OnClickListener() {
boolean showText = false;
@Override
public void onClick(View v) {
resultp = data.get(position);
counter--;
showText = true;
if (counter == 1 || counter < 1) {
et_dish_quan.setText("1");
} else {
et_dish_quan.setText(String.valueOf(counter));
}
try {
quantitySTR = new Double(et_dish_quan.getText().toString());
} catch (NumberFormatException e) {
quantitySTR = 1.0; // your default value
}
try {
costSTR = new Double(et_menucost.getText().toString());
} catch (NumberFormatException e) {
costSTR = 1.0; // your default value
}
resultSTR = costSTR * quantitySTR;
final String stringdouble = Double.toString(resultSTR);
totalET.setText(stringdouble);
tempcost = Double.toString(costSTR);
tempquan = Double.toString(counter);
db.addOrder(new Order(empname,tempcost,tempquan));
Log.d("Insert: ", "Inserting ..");
//uc.calculateSum();
}
});
return itemView;
}
litiview项目的总和(resultSTR)。我想做resutstr的添加。我得到了成本和数量的总价值。
答案 0 :(得分:2)
//Your Activity class will be like this:-
public class MainActivity extends AppCompatActivity {
ArrayList<String> listValues;
ListView listView;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview=(ListView)findViewById(R.id.list);
textView=(TextView)findViewById(R.id.textView);
listValues=new ArrayList<String>();
listValues.add("1");
listValues.add("2");
listValues.add("3");
listView= (ListView) findViewById(R.id.list);
customAdapter= new CustomAdapter(MainActivity.this, R.layout.myview, listValues);
listView.setAdapter(customAdapter);
}
//Create a method like below in your activity class
public void updateValue(String amount)
{
textView.setText(amount);
}
您的适配器将是这样的: -
private ArrayList<Currency> values;
private Context context;
public CustomAdapter(Context context, int textViewResourceId,
ArrayList<String> values) {
super(context, textViewResourceId, values);
this.values=values;
this.context=context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = inflater.inflate(R.layout.spinner, parent, false);
tempValues = null;
tempValues = values.get(position);
TextView label = (TextView) row.findViewById(R.id.tvLabel);
Button button=(Button)row.findViewById(R.id.convert);
label.setText(values.get(position));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int calculatedValue=Integer.parseInt(label.getText().toString())*2;
if(context instanceof MainActivity)
{
//Update your calculated value in activity textview
((MainActivity)context).update(String.valueOf(calculatedValue));
}
}
});
return row;
}
答案 1 :(得分:1)
制作如下界面:
public interface ChangeItemInterface {
public void doChange(float anyValue);
}
在适配器中,
初始化接口对象,如:
ChangeItemInterface changeItemInterface;
在适配器构造函数中,
this.changeItemInterface = context;
在适配器中,从任何视图单击:
AnyView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do total here and that value pass to the Activity via interface method like below
this.changeItemInterface.doChange(<AnyValue>);
// It will go to the Your Activity Overided method which is explained below this
}
});
在您的活动中实现此界面,如:
public class YourActivity extends Activity implements ChangeItemInterface{
/// You'll get override method of your interface, here your call back will come when from adapter click happen
@Override
public void doChange(float anyValue) {
/// Here you can update any value in your activity !
/// Here you can pass total value and update in your activity class
}
}
希望这能帮助您解决问题!