我想将活动显示为对话框,但此对话框将显示从数据库动态填充的布局,但我没有得到任何结果。
package chintan.khetiya.sqlite.cursor;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class lista extends Activity {
private ListView lista;
DatabaseHandler db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listado);
//SE HACE TODO EL PROCESO EJECUTANDO UNA FUNCION
Refresh();
lista.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position,long id) {
final int selected_row = position;
final String cliente = DameCliente(selected_row);
final int limite = DameConteo();
int i;
AlertDialog.Builder myDialog
= new AlertDialog.Builder(lista.this);
我的问题在这里
LinearLayout linear = (LinearLayout) findViewById(R.id.generar_text);
TextView text = new TextView(lista.this);
text.setText("hola");
当然这是一个考验。我会做一个循环来创建更多的文本视图,但我需要构建第一个,然后循环更多。
LayoutInflater inflater = getLayoutInflater();
myDialog.setView(inflater.inflate(R.layout.dialog, null));
myDialog.setTitle(cliente);
myDialog.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
}
});
myDialog.setNeutralButton("Eliminar Pedido", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
final int eliminar = selected_row + 1;
AlertDialog.Builder dialogoDentro = new AlertDialog.Builder(lista.this);
dialogoDentro.setTitle("Alerta !");
dialogoDentro.setMessage("Eliminar el Pedido ?");
dialogoDentro.setNeutralButton("Eliminar", new DialogInterface.OnClickListener( {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
db.Delete_Pedido(eliminar);
lista.this.Refresh();
}
});
dialogoDentro.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
}
});
dialogoDentro.show();
}
});
myDialog.show();
}
});
}
public void Refresh(){
int limite = DameConteo();// numero de columnas insertadas como productos
int numero_pedidos = NumeroPedidos();
//Toast.makeText(getApplicationContext(), "Valor Columna: " + ValorColumna(columna) + " -- Columna: " + columna + " -- Precio : " + ValorPrecio(columna), Toast.LENGTH_SHORT).show();
ArrayList<lista_entrada> datos = new ArrayList<lista_entrada>();
for (int a = 1; a <= numero_pedidos;a ++){
String pedidor = InfoPedidos(a).getString(1);
float iva = (float) 1.16;
int articulos = 0;
float subtotal = 0;
float total = 0;
for (int b=0; b<limite; b++){
String columna = DameColumna(b).toString();
articulos = articulos + ValorColumna(columna,a);
subtotal = subtotal + (ValorColumna(columna,a)*(ValorPrecio(columna)));
total = total + (float) (((ValorColumna(columna,a)*(ValorPrecio(columna)))) * iva);
}
datos.add(new lista_entrada(pedidor,"Pedido: " + articulos + " artículos", "Subtotal: $" + String.format("%.2f", subtotal), "Total: $" + String.format("%.2f", total)));
//}
}
lista = (ListView) findViewById(R.id.ListView_listado);
lista.setAdapter(new lista_adaptador(this, R.layout.entrada, datos){
@Override
public void onEntrada(Object entrada, View view) {
if (entrada != null) {
TextView nombre = (TextView) view.findViewById(R.id.nombre);
if (nombre != null)
nombre.setText(((lista_entrada) entrada).getNombre());
TextView articulos = (TextView) view.findViewById(R.id.articulos);
if (articulos != null)
articulos.setText(((lista_entrada) entrada).getArticulos());
TextView subtotal = (TextView) view.findViewById(R.id.subtotal);
if (subtotal != null)
subtotal.setText(((lista_entrada) entrada).getSubtotal());
TextView total = (TextView) view.findViewById(R.id.total);
if (total != null)
total.setText(((lista_entrada) entrada).getTotal());
}
}
});
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
Refresh();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Clientes:
finish();
startActivity(new Intent(this, Main_Screen.class));
return true;
case R.id.Productos:
finish();
startActivity(new Intent(this, Product_Screen.class));
return true;
case R.id.Nuevo_Pedido:
finish();
startActivity(new Intent(this, home_screen.class));
return true;
case R.id.listado_pedidos:
finish();
startActivity(new Intent(this, lista.class));
return true;
case R.id.salir:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public String DameCliente(int contact_name){
//product_data.clear();
db = new DatabaseHandler(this);
ArrayList<Pedido> pedido_array_from_db = db.Get_Pedido();
String cte_name = pedido_array_from_db.get(contact_name).get_contact_name();
return cte_name;
}
public int DameConteo(){
db = new DatabaseHandler(this);
int conteo = db.Get_Total_Products();
return conteo;
}
public int NumeroPedidos(){
db = new DatabaseHandler(this);
int numero = db.Get_Total_Pedido();
return numero;
}
public Cursor InfoPedidos(int id){
db = new DatabaseHandler(this);
Cursor cursor = db.Get_Pedido(id);
return cursor;
}
public String DameColumna(int columna_id){
db = new DatabaseHandler(this);
ArrayList<Columna> columna_array_from_db = db.Get_Columna();
String columna = columna_array_from_db.get(columna_id).get_columnaName();
return columna;
}
public int ValorColumna(String columna, int a){
String valor_a = String.valueOf(a);
db = new DatabaseHandler(this);
int valor = db.get_valor_columna(columna,valor_a);
return valor;
}
public float ValorPrecio(String columna){
db = new DatabaseHandler(this);
float valor = db.get_valor_precio(columna);
return valor;
}
}
答案 0 :(得分:0)
试试这个。
更改
LinearLayout linear = (LinearLayout) findViewById(R.id.generar_text);
TextView text = new TextView(lista.this);
text.setText("hola");
LayoutInflater inflater = getLayoutInflater();
myDialog.setView(inflater.inflate(R.layout.dialog, null));
到
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.dialog, null);
LinearLayout linear = (LinearLayout) view.findViewById(R.id.generar_text);
TextView text = new TextView(this);
text.setText("hola");
linear.addView(text);
myDialog.setView(view);
答案 1 :(得分:0)
改变这个:
LinearLayout linear = (LinearLayout) findViewById(R.id.generar_text);
TextView text = new TextView(lista.this);
text.setText("hola");
为:
AlertDialog dialog=myDialog.create();
LinearLayout linear = (LinearLayout) dialog.findViewById(R.id.generar_text);
TextView text = new TextView(lista.this);
text.setText("hola");
dialog.show();
答案 2 :(得分:0)
如果要将View动态添加到ViewGroup(例如LinearLayout),最简单的方法是在单独的布局xml文件中定义View(例如TextView)。
例如
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="1"
android:textIsSelectable="true"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
存储为 /res/layout/mytextview.xml
现在,在代码中,您希望动态地放置视图而不是通过findViewById查找一个视图,每次视图的新实例都会膨胀,例如:
LayoutInflater inflater = getLayoutInflater();
LinearLayout rootView = (LinearLayout) view.findViewById(R.id.rootView);
for( int i=0; i < 5; ++i )
{
TextView mytextview = (TextView) inflater.inflate(R.layout.textview, null);
mytextview.setText("hola");
rootView.addView(mytextview);
}
注意:如果您这样做,您可以在XML中为要多次动态添加的视图设置所有样式和内容。这可以使您免于大量编写,因为在代码中定义View的外观涉及比XML中所需的更多“写入”。