Android 2活动中调用的异步任务和结果生成动态内容

Android 2活动中调用的异步任务和结果生成动态内容,android,dynamic,android-linearlayout,textview,Android,Dynamic,Android Linearlayout,Textview,我正在用户登录后加载的活动中生成动态TextView。我使用Web服务检索一些数据(所有用户名),并为每个数据创建一个textView。由于我有http请求,它是在另一个线程(Asynctask)中执行的。在创建项之前,我等待查询结果并在“OnPostExecute”中返回它。 我有我的用户列表,每个项目都是可点击的,我现在想在点击时显示一些信息,但我不知道怎么做 这是我的密码: operation.java public class operation extends AsyncTask<

我正在用户登录后加载的活动中生成动态
TextView
。我使用Web服务检索一些数据(所有用户名),并为每个数据创建一个
textView
。由于我有http请求,它是在另一个线程(
Asynctask
)中执行的。在创建项之前,我等待查询结果并在
“OnPostExecute”
中返回它。 我有我的用户列表,每个项目都是可点击的,我现在想在点击时显示一些信息,但我不知道怎么做

这是我的密码:

operation.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;

}
公共类操作扩展了异步任务
{
公共ArrayList doLogin(AndroidHttpClient客户端,字符串用户名,
字符串userAccessKey){..}
公共ArrayList doQuery(AndroidHttpClient,
字符串sessionName,字符串查询){…}
@凌驾
受保护的ArrayList doInBackground(字符串…参数){
最终AndroidHttpClient客户端=AndroidHttpClient客户端
.newInstance(“安卓”);
如果(参数[2]=“登录”){
ArrayList sessionName=doLogin(客户端,参数[0],
参数[1]);
返回sessionName;
}else if(参数[2]=“查询”){
返回doQuery(客户端,参数[3],
“从帐户中选择accountname;”;
}
返回null;
}
QueryActivityClass:

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();
        }

    });

}
公共类查询活动扩展活动{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.query);
按钮返回=(按钮)findViewById(R.id.back);
//启动活动时显示用户名
//为每个用户创建动态文本视图项目
//并允许用户单击它以显示更多信息。
//它调用函数“doInBackGround”来发出http请求
//(查询)并检索结果以生成
//动态页面
新行动(){
@凌驾
PostExecute上的公共void(ArrayList结果){
LinearLayout dynamicWindow=(LinearLayout)findViewById(R.id.windowLayout);
dynamicWindow.removeAllViewsInLayout();
//创建动态线性布局,其中将包含
//文本视图
LinearLayout dynamicContent=新的LinearLayout(
查询活动;
动态内容设置方向(线性布局、垂直);
动态内容设置背景色(0xFF007AAD);
LinearLayout.LayoutParams params=新的LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL\u父级,
LinearLayout.LayoutParams.FILL_PARENT);
参数重力=重心水平;
dynamicWindow.addView(dynamicContent);
//检索查询返回的每个名称并创建
//每一个的文本视图
int nbTextView=result.size();
TextView[]textViewArray=新建TextView[nbTextView];
对于(int i=0;i
}

当我点击用户名时,我想在屏幕中间显示信息,但是我不得不做另一个HTTP请求来检索他的信息。这涉及到我通过制作<代码>新操作()来再次调用<代码> doIngestue/Cudio>。
并在类操作中的doInbackground函数中添加另一个case。我有点迷路了,请有人知道吗? 附言:如果你在我的代码中发现一些可以改进的地方…:)
Thx

好的,我找到了另一种方法。当我的页面/活动创建时,我将有关我的客户端的所有信息存储在DB SQLite中,这样我就不必每次单击TextView时都发送HTTP请求,因为我已经有了数据