我TextView
在用户登录后加载的活动中生成动态。我使用 Web 服务来检索一些数据(所有用户名),并textView
为每个数据创建一个。由于我有 http 请求,它在另一个线程 ( Asynctask
) 中执行。在创建项目之前,我等待查询的结果并将其返回"OnPostExecute"
。我有我的用户列表,每个项目都是可点击的,我现在想在点击它时显示一些信息,但我不知道该怎么做。
这是我的代码:
操作.java
public class operation extends AsyncTask<String, Void, ArrayList<String>>
{
public ArrayList<String> doLogin(AndroidHttpClient client, String userName,
String userAccessKey) { ..}
public ArrayList<String> doQuery(AndroidHttpClient client,
String sessionName, String query) { ...}
@Override
protected ArrayList<String> doInBackground(String... params) {
final AndroidHttpClient client = AndroidHttpClient
.newInstance("Android");
if (params[2] == "login") {
ArrayList<String> sessionName = doLogin(client, params[0],
params[1]);
return sessionName;
} else if (params[2] == "query") {
return doQuery(client, params[3],
"select accountname from Accounts;");
}
return null;
}
查询活动类:
public class QueryActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.query);
Button back = (Button) findViewById(R.id.back);
// Display users' name when the activity is launched
// creating dynamic TextView items for each user
// and allows the user to click on it to display more information.
// It calls the function "doInBackGround" to make http requests
// (queries) in another thread and retrieve the result to generate a
// dynamic page
new operation() {
@Override
public void onPostExecute(ArrayList<String> result) {
LinearLayout dynamicWindow = (LinearLayout) findViewById(R.id.windowLayout);
dynamicWindow.removeAllViewsInLayout();
// create a dynamic LinearLayout which will contain our
// TextViews
LinearLayout dynamicContent = new LinearLayout(
QueryActivity.this);
dynamicContent.setOrientation(LinearLayout.VERTICAL);
dynamicContent.setBackgroundColor(0xFF007AAD);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
params.gravity = Gravity.CENTER_HORIZONTAL;
dynamicWindow.addView(dynamicContent);
// retrieve every name returned from the query and create a
// TextView for each one
int nbTextView = result.size();
TextView[] textViewArray = new TextView[nbTextView];
for (int i = 0; i < nbTextView; i++) {
Log.i("Contenu ArrayList",
"item n°" + i + " = " + result.get(i));
textViewArray[i] = new TextView(QueryActivity.this);
textViewArray[i].setText(result.get(i));
textViewArray[i].setTextColor(0xFFFFFFFF);
textViewArray[i].setVisibility(View.VISIBLE);
textViewArray[i].setGravity(2);
textViewArray[i].setId(+i);
textViewArray[i].setLeft(10 + (i * 2));
final int test = i;
// makes the TextView clickable
textViewArray[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/* HERE IS THE PART I DON'T KNOW HOW TO DOW */
}
});
// add the TextView to the linearLayout
dynamicContent.addView(textViewArray[i],
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.FILL_PARENT));
}
}
}.execute(null, null, "query", sessionName);
// When the "back" button is clicked, the activity is destroyed and the
// previous activity on the stack is loaded
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
因此,当我单击用户名时,我想在屏幕中间显示信息,但我必须执行另一个 http 请求才能检索他的信息。它涉及我通过在类操作的 doInbackground 函数中创建和添加另一个案例来doInBackground
再次调用。new Operation().execute(...)
我有点迷路了,请有人有想法吗?PS:如果您在我的代码中发现可以改进的内容... :) Thx