根据Gridview

时间:2017-08-01 09:09:31

标签: android gridview sql-server-2012 android-gridview

我正在创建一个GridView来显示数据库中的数据。连接和显示工作完美。当我在While循环中引入和If Else语句时,我的问题就出现了。下面是我的XML文件(分别是GridView和CustomGridview):

 <GridView
    android:id="@+id/gridView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:stretchMode="columnWidth">
</GridView>


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="5dp"
    android:text="ID:"
    android:textStyle="bold" />
<TextView
    android:id="@+id/ID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text="ID"
    android:layout_marginLeft="5dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="30dp"
    android:text="Company Name:"
    android:textStyle="bold" />
<TextView
    android:id="@+id/Client"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CompanyName"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="45dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="145dp"
    android:text="Emplyee Name:"
    android:textStyle="bold" />
<TextView
    android:id="@+id/Employee"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="EmployeeName"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="145dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="35dp"
    android:layout_marginLeft="255dp"
    android:textStyle="bold"
    android:id="@+id/amountname"
    android:text="Amount" />

<TextView
    android:id="@+id/Amount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Amount"
    android:layout_marginTop="50dp"
    android:layout_marginLeft="255dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="35dp"
    android:layout_marginLeft="105dp"
    android:text="Reason:"
    android:textStyle="bold" />

<TextView
    android:id="@+id/Reason"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Reason"
    android:layout_marginTop="50dp"
    android:layout_marginLeft="105dp" />


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="35dp"
    android:layout_marginLeft="5dp"
    android:text="Receipt Date:"
    android:textStyle="bold" />
<TextView
    android:id="@+id/ReceiptDate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ReceiptDate"
    android:layout_marginTop="50dp"
    android:layout_marginLeft="5dp" />


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="255dp"
    android:text="Category:"
    android:textStyle="bold" />
<TextView
    android:id="@+id/Category"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Catergory"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="255dp" />

布局分别如下所示; gridView

CustomGridView

这是我的主要代码:

GridView gridView;
TextView amountno,amountname;
String un, passwords, db, ip;
Connection connect;
ResultSet rs;
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_data);
    ip = "xxx";
    un = "xxx";
    passwords = "xxx";
    db = "xxx";
    amountname = (TextView)findViewById(R.id.amountname);
    gridView = (GridView) findViewById(R.id.gridView);
    CallableStatement callableStatement=null;
    amountno=(TextView)findViewById(R.id.Amount);
    try{
        connect = CONN(un, passwords, db, ip);
        String StoredProc = "{call xxx";
        callableStatement=connect.prepareCall(StoredProc);
        callableStatement.setString(1, MainActivity.usernam);
        rs = callableStatement.executeQuery();
        List<Map<String,String>> data = new ArrayList<Map<String,String>>();
        String eamount="";
        while(rs.next()){

            Map<String,String> datanum = new HashMap<String, String>();
            datanum.put("A", rs.getString("Id"));
            datanum.put("B", rs.getString("Client"));
            datanum.put("C", rs.getString("Employee"));

            if (rs.getString("Amount")=="0.0000"){
                eamount=rs.getString("Received");
            }else{
                eamount=rs.getString("Amount");
            }

            datanum.put("E", eamount);
            datanum.put("F", rs.getString("Reason"));
            datanum.put("H", rs.getString("Date"));
            datanum.put("K", rs.getString("Category"));
            data.add(datanum);
        }
        String[] from = {"A","B","C","K","H","F","E"};
        Log.d("String Value", String.valueOf(amountname));
        int[] views = {R.id.ID, R.id.Client, R.id.Employee,R.id.Category,R.id.ReceiptDate,R.id.Reason,R.id.Amount};
        Log.d("String Value", String.valueOf(amountname));
        final SimpleAdapter ADA = new SimpleAdapter(ViewData.this,data,R.layout.customgrid, from, views);
        gridView.setAdapter(ADA);
        if(eamount == "Received"){amountname.setText("Received");Log.d("String Value", eamount);}
        else if(eamount == "Amount") {amountname.setText("Expenses");Log.d("String Value", eamount);}
    }
    catch (SQLException e){
        e.printStackTrace();
    }


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


}


@SuppressLint("NewApi")
private Connection CONN(String _user, String _pass, String _DB,
                        String _server) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Connection conn = null;
    String ConnURL;
    try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        ConnURL = "jdbc:jtds:sqlserver://" + _server + ";" + "databaseName=" + _DB + ";user=" + _user + ";password=" + _pass + ";";
        conn = DriverManager.getConnection(ConnURL);
    } catch (Exception e) {
        Log.e("ERRO", e.getMessage());
    }
    return conn;
}

现在......如果您在我的自定义网格布局中看到我将textview作为 Amount 标签,并且它具有来自下面数据库的相应值,并且主活动中的IF ELSE语句更改了值(完美工作)....根据从数据库收到的值(&#34;接收或金额&#34;),我想要改变的是金额标签 ...我想要将标签更改为已收到或相应的费用。

我实际上尝试了很多方法,很多时候它在AmountLabel上显示NULL Point Exception错误,我意识到gridview的标题应该是唯一的,因此我不知道如何动态使用金额(AmountLabel下面的textview)接收的值更改金额标签。

注意:我的代码中没有错误...我只想根据amountValue接收的值动态设置AmText的AmTeLabel。 谢谢你^ _ ^

0 个答案:

没有答案