如果这是一个非常基本的问题,我深表歉意,但是如果我从服务器上的文本文件中获取列表项,我该如何设置 onListItemClick?我想要做的只是在单击列表视图上的项目时显示祝酒词,但我似乎无法添加 onListItemClick 来执行此操作。我认为这是因为 try/catch ?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Toast;
public class ListView extends ListActivity {
public String[] ListItems = new String[]{};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
URL textUrl;
try {
textUrl = new URL("http://172.30.54.153/databases/result.txt");
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(textUrl.openStream()));
String StringBuffer;
String stringText = "";
stringText.split(",");
while ((StringBuffer = bufferReader.readLine()) != null) {
stringText += StringBuffer;
}
String DbLines = new String(stringText);
ListItems = DbLines.split(",");
setListAdapter(new ArrayAdapter<String>( this,
android.R.layout.simple_list_item_1, ListItems));
bufferReader.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void OnListItemClick(ListView Parent,View v, int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(this, "y"+ ListItems[position], Toast.LENGTH_LONG).show();
}
}