我正在尝试使用Asynctask从jdbc驱动程序加载数据并将此数据放在multimap中,然后将其加载到listview中
我目前有mainactivity.java,带有一个按钮和clicklistener(mainactivity上的这个按钮用于启动下一个活动)。
第二个活动已经启动了一个按钮,并且还有一个listview,其中填充了来自backgroundpickingtask.java的数据。 secondactivity.java实际上是我称之为“new BackgrounPickingTask.execute();
我遇到的问题是secondactivity.java中的按钮首先显示,但listview在secondactivity.java中显示之前需要一两秒钟。
我认为最好是从mainactivity准备好数据,这样第二个活动按钮(R.id.nextButton)在启动第二个活动时并不总是超出listview。对不起,如果我不清楚,但我尽力解释它。真的很感激任何帮助或建议。我是Android应用程序开发的新手。以下是课程。
MainActivity.class
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button pickButton = (Button) findViewById(R.id.pickButton);
View.OnClickListener pickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
MainActivity.this.startActivity(intent);
}
};
pickButton.setOnClickListener(pickListener);
}
BackGroundPickingTask.java
public class BackgroundPickingTask extends AsyncTask<String, String, String> {
private Connection conn;
private String HOST = "jdbc:postgresql://IPaddress:(port number)/";
private String DataBaseNAME = "database";
private String USERNAME = "the_username";
private String PASSWORD = "the_password";
private PreparedStatement pStmt = null;
private ResultSet rs = null;
private long rowCounter = 0;
Multimap<String, String> myMultimap = ArrayListMultimap.create();
private String sql =
"SELECT id, name, title, project from thattable"
ArrayAdapter<String> adapter;
@Override
protected void onPreExecute() {
adapter = (ArrayAdapter<String>) (SecondActivity.list.getAdapter());
}
@Override
protected String doInBackground(String... params) {
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(
HOST + DataBaseNAME, USERNAME, PASSWORD);
//Prepares statement to execute query
pStmt = conn.prepareStatement(sql);
//Execute prepared statement
rs = pStmt.executeQuery();
//add results from sql query to multimap
while (rs.next()) {
myMultimap.put(rs.getString(1),rs.getString(2) + " (" + rs.getString(3) + " - " + rs.getString(4) + ")");
++rowCounter;
}
if (rowCounter == 0) {
//System.out.println("There are no results");
}
rs.close();
pStmt.close();
} catch(Exception e) {
e.printStackTrace();
}
//for loop to add values from each key to adapter
for (String keyz : myMultimap.keySet()) {
publishProgress(keyz);
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
//formatting my string here to organize in listview
String formattedString = "\nName (title- project):\n " + myMultimap.get(values[0]).toString()
.replace("," , "\n").trim()
.replace("[","").trim()
.replace("]","").trim();
//adding results to listview adapter
adapter.add("ID: " + values[0] + formattedString);
}
@Override
protected void onPostExecute(String values) {
}
SecondActivity.class
public class SecondActivity extends Activity {
public static ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button createWave = (Button) findViewById(R.id.nextButton);
list = (ListView) findViewById(R.id.list);
list.setAdapter(new ArrayAdapter<String>(this,
R.layout.simple_list_item_1, new
ArrayList<String>()));
new BackgroundPickingTask().execute();
}
答案 0 :(得分:0)
使用ArrayAdapter作为参数的方法创建一个接口..
在SecondActivity上实现它..
使用arrayAdapter作为参数调用您在BackGroundPickingTask的onPostExecute中创建的Interface方法。
在SecondActivity上的实现方法中填充listView ..
那就是
答案 1 :(得分:0)
public interface TaskInterface{
void onLoadingCompleted();
}
<强> SecondActivity.java 强>
默认情况下,您visibility
中的invisible
到xml
。
用
new BackgroundPickingTask(new TaskInterface(){
@Override
public void onLoadingCompleted(){
yourButton.setVisibility(View.VISIBLE);
}
}
)).execute();
在AsyncTask中添加参数化构造函数
public class BackgroundPickingTask extends AsyncTask<String, String, String> {
private TaskInterface taskInterface;
public BackgroundPickingTask(TaskInterface task) {
super();
taskInterface = task
}