0

我正在努力做到这一点,一旦用户单击我的列表视图中的一个项目,它就会转到另一个为该场合动态创建的类。要添加,我正在创建一个联系人列表应用程序,当用户单击该应用程序时,它会显示通过我们系统的文本和通话记录;我不知道该怎么做,我猜垃圾收集器方法工作得很好,虽然,我认为这是错误的上下文。

谢谢 :)

代码:

public class ChatService extends ListActivity {
  String GotPass;
  String GotUname;
  public static final String PREFS_NAME = "MyPregs";
  private GetTask getTask;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getTask = new GetTask();
    getTask.execute();
  }

  public class GetTask extends AsyncTask<Void, Void, ReturnModel> {
    @Override
    protected ReturnModel doInBackground(Void... params) {
      return load();
    }

    @Override
    protected void onPostExecute(ReturnModel result) {

      if(result.passworderror == true)
      {
        Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
      }
      else
      {
        Toast.makeText(getApplicationContext(), "yaya", Toast.LENGTH_SHORT).show();

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, result.getheadlines());
        setListAdapter(adapter);
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                Toast.makeText(getApplicationContext(), "Yaya, we clicked on something", Toast.LENGTH_LONG).show();
                // TODO Auto-generated method stub

            }
          });
      }
    }
  }

  private ReturnModel load() {
    ReturnModel returnModel = new ReturnModel();

    BufferedReader in = null;
    String data = null;
    Bundle gotData = getIntent().getExtras();
    if (gotData != null) {
      GotPass = gotData.getString("key!");
      GotUname = gotData.getString("key!!");
    }

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    String username = settings.getString("key1", null);
    String password = settings.getString("key2", null);
    // username = "irock97"; // unremark to test like you got username from prefs..
    if (username != null  && username.equals("irock97")) {
      returnModel.setPassworderror(false);
    }
    else 
    {
      returnModel.setPassworderror(true);
      return returnModel;
    }

    HttpClient httpclient = new DefaultHttpClient();

    /* login.php returns true if username and password is equal to saranga */
    HttpPost httppost = new HttpPost("http://gta5news.com/login.php");

    try {
      // Add user name and password
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
      nameValuePairs.add(new BasicNameValuePair("username", username));
      nameValuePairs.add(new BasicNameValuePair("password", password));
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

      // Execute HTTP Post Request
      Log.w("HttpPost", "Execute HTTP Post Request");
      HttpResponse response = httpclient.execute(httppost);
      in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
      StringBuffer sb = new StringBuffer("");
      String l = "";
      String nl = "";
      while ((l = in.readLine()) != null) {
        sb.append(l + nl);
      }
      in.close();
      data = sb.toString();


      List<String> headlines = new ArrayList<String>();
      headlines.add(data);
      returnModel.setheadlines(headlines);

    }
    catch (ClientProtocolException e) {
      e.printStackTrace();
    }
    catch (IOException e) {
      e.printStackTrace();
    }

    return returnModel;
  }


  public class ReturnModel {
    private List<String> headlines;
    private boolean passworderror;

    public List<String> getheadlines() {
      return headlines;
    }

    public void setheadlines(List<String> headlines) {
      this.headlines = headlines;
    }

    public boolean getPassworderror() {
      return passworderror;
    }

    public void setPassworderror(boolean passworderror) {
      this.passworderror = passworderror;
    }
  }

}
4

1 回答 1

0

膨胀一个 xml 初始视图(在每个动态创建的布局上都相同的预设置)(膨胀意味着将 xml 格式设置为视图,简化)。

LayoutInflater inflater = (LayoutInflater) MyApplication.getAppContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newView = inflater.inflate(R.layout.your_layout, parent, false);

现在您可以根据自己的意愿修改 newView 并添加新的视图元素。

为此,您必须参考您想要添加内容的初始布局。

例如:

LinearLayout myInitialLL = (LinearLayout) newView.findViewById(R.id.my_initial_ll);

例如,您可以创建一个新的 Textview 并将其添加到 myIniitalLL。

TextView myTV = new TextView(this);
myTv.setText("Test text");

myInitialLL.addView(myTv);
myInitialLL.requestLayout();

您现在可以在活动中创建自定义私有方法来创建逻辑部分。这应该在 onItemClickListener 中调用。如果您现在不知道需要多少文本视图,也可以通过循环创建动态文本视图。我更喜欢一个 ArrayList 作为参数。

于 2012-04-22T16:50:53.557 回答