带有MSSQL的Android TableView

时间:2018-07-16 19:45:22

标签: android sql-server tableview

我正忙于尝试获取从MSSQL获取的数组,以便在应用程序中以表格视图的形式显示。我试图用谷歌搜索它,但是我似乎找不到这个例子。我已经尝试过,但是遇到一个小错误。 我收到以下错误Cannot resolve constructor:Simpletabledata adapter[package.mainactivity, package.itemarray]

这是我的mainactivy.java班:

public class MainActivity extends AppCompatActivity {
static String[] spaceProbeHeaders={"Name"};
private ArrayList<ClassListItems> itemArrayList;  //List items Array
private MyAppAdapter myAppAdapter; //Array Adapter
final TableView<String[]> tableView = (TableView<String[]>) findViewById(R.id.tableView);
private boolean success = false; // boolean

Connection conn; // Connection Class Initialization


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tableView.setHeaderBackgroundColor(Color.parseColor("#777777"));
    tableView.setHeaderAdapter(new SimpleTableHeaderAdapter(this,spaceProbeHeaders));
    tableView.setColumnCount(4);

    itemArrayList = new ArrayList<ClassListItems>(); // Arraylist Initialization

    // Calling Async Task
    SyncData orderData = new SyncData();
    orderData.execute("");
}

// Async Task has three overrided methods,
private class SyncData extends AsyncTask<String, String, String>
{
    String msg = "Internet/DB_Credentials/Windows_FireWall_TurnOn Error, See Android Monitor in the bottom For details!";
    ProgressDialog progress;

    @Override
    protected void onPreExecute() //Starts the progress dailog
    {
        progress = ProgressDialog.show(MainActivity.this, "Synchronising",
                "Tableview Loading! Please Wait...", true);
    }

    @Override
    protected String doInBackground(String... strings)  // Connect to the database, write query and add items to array list
    {
        try
        {
            ConnectionClass conStr=new ConnectionClass();
            conn =conStr.connectionclass();
            //Connection Object
            if (conn == null)
            {
                success = false;
            }
            else {
                // Change below query according to your own database.
                String query = "SELECT customer_first_name FROM cc_customer";
                Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery(query);
                if (rs != null) // if resultset not null, I add items to itemArraylist using class created
                {
                    while (rs.next())
                    {
                        try {
                            itemArrayList.add(new ClassListItems(rs.getString("customer_first_name")));
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                    msg = "Found";
                    success = true;
                } else {
                    msg = "No Data found!";
                    success = false;
                }
            }
        } catch (Exception e)
        {
            e.printStackTrace();
            Writer writer = new StringWriter();
            e.printStackTrace(new PrintWriter(writer));
            msg = writer.toString();
            success = false;
        }
        return msg;
    }

    @Override
    protected void onPostExecute(String msg) // disimissing progress dialoge, showing error and setting up my listview
    {
        progress.dismiss();
        Toast.makeText(MainActivity.this, msg + "", Toast.LENGTH_LONG).show();
        if (success == false)
        {
        }
        else {
            try {
                //myAppAdapter = new MyAppAdapter(itemArrayList, MainActivity.this);


                tableView.setDataAdapter(new SimpleTableDataAdapter(MainActivity.this,itemArrayList ));

            } catch (Exception ex)
            {

            }

        }
    }
}

这是我的classlist.java文件:

    public class ClassListItems
{


    public String name; //Name

    public ClassListItems(String name)
    {

        this.name = name;
    }



    public String getName() {
        return name;
    }

1 个答案:

答案 0 :(得分:1)

更新

NB:OP正在使用SortableTableView Library

您需要导入以下内容来解决Cannot resolve constructor:SimpleTableDataAdapter-

import de.codecrafters.tableview.toolkit.SimpleTableDataAdapter;

原始

您的项目中有SimpleTableDataAdapter类吗?似乎找不到该类,因此它不在同一包中。如果它在其他软件包中,则需要将其导入。 另外请注意,您的.java文件名应与类名匹配

在另一个不同的注释上,您是否测试过itemArrayList确实在填充?对于Android-MSSQL,这是教程指针-

https://parallelcodes.com/connect-android-to-ms-sql-database-2/

有很多教程,如果您用谷歌搜索。