好的,这是我的第一个每个 android 应用程序,所以不要抨击我。如果我做错了什么或奇怪的事情,我非常愿意提出建议,所以请不要害羞。单击某个项目后,我在更新列表视图时遇到了一些麻烦。无论如何,这是代码......我只是不明白它是如何工作的,我认为......
[ MainActivity.java ]
package com.mycompany.myapp2;
import android.app.*;
import android.os.*;
import android.text.method.*;
import android.view.*;
import android.widget.*;
import android.content.*;
import java.util.*;
import java.io.*;
public class MainActivity extends ListActivity
{
public CustomAdapter adapterMain;
private FtpConnectionTask ftpTask;
private LayoutInflater mInflater;
private Vector<RowData> data;
private TextView textView;
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.txtView);
textView.setMovementMethod(new ScrollingMovementMethod());
xBins(textView);
}
/*public void addItems( ListView parent )
{
CustomAdapter adapter = (CustomAdapter) parent.getAdapter();
RowData rd = new RowData("item4", "description4");
data.add(rd);
rd = new RowData("item5", "description5");
data.add(rd);
rd = new RowData("item6", "description6");
data.add(rd);
CustomAdapter adapter = new CustomAdapter(this, R.layout.row, R.id.item, data);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}*/
public void onListItemClick( ListView parent, View v, int position, long id )
{
ftpTask.row = ftpTask.adapter.getItem(position);
String ftpItem = ftpTask.row.mItem;
if ( ftpTask.row.mDescription == "dir" )
{
String[] args = new String[] { ftpItem };
Object[] aObject = new Object[] { "cd", args, this };
//ftpTask.adapter.clear();
//ftpTask.processCmd(aObject);
}
}
private class RowData
{
protected String mItem;
protected String mDescription;
RowData( String item, String description )
{
mItem = item;
mDescription = description;
}
@Override
public String toString( )
{
return mItem + " " + mDescription;
}
}
private class CustomAdapter extends ArrayAdapter<RowData>
{
public CustomAdapter( Context context, int resource,
int textViewResourceId, List<RowData> objects )
{
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView( int position, View convertView, ViewGroup parent )
{
ViewHolder holder = null;
//widgets displayed by each item in your list
TextView item = null;
TextView description = null;
//data from your adapter
RowData rowData= getItem(position);
//we want to reuse already constructed row views...
if ( null == convertView )
{
convertView = mInflater.inflate(R.layout.row, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
//
holder = (ViewHolder) convertView.getTag();
item = holder.getItem();
item.setText(rowData.mItem);
description = holder.getDescription();
description.setText(rowData.mDescription);
return convertView;
}
}
/**
* Wrapper for row data.
*
*/
private class ViewHolder
{
private View mRow;
private TextView description = null;
private TextView item = null;
public ViewHolder( View row )
{
mRow = row;
}
public TextView getDescription( )
{
if ( null == description )
{
description = (TextView) mRow.findViewById(R.id.description);
}
return description;
}
public TextView getItem( )
{
if ( null == item )
{
item = (TextView) mRow.findViewById(R.id.item);
}
return item;
}
}
public void xBins( View view )
{
IrcConnectionTask task = new IrcConnectionTask();
task.execute(new Object[] { "irc.efnet.pl", 6667, "sgd5", this });
}
}
[IrcConnectionTask.java]
package com.mycompany.myapp2;
import android.os.*;
import java.io.*;
import java.net.*;
public class IrcConnectionTask extends AsyncTask<Object, String, String>
{
MainActivity callerActivity;
private Socket socket;
@Override
protected String doInBackground(Object... params)
{
String response = "";
String host = (String)params[0];
Integer port = (Integer)(params[1]);
String nick = (String)params[2];
callerActivity = (MainActivity)params[3];
try
{
socket = new Socket(host, port);
String msg1 = "NICK " + nick;
String msg2 = "USER sur 8 * :be";
String messages[] = { msg1, msg2 };
InputDumper task = new InputDumper();
task.execute(new Object[] { socket, messages, nick, callerActivity});
}
catch ( UnknownHostException e )
{
e.printStackTrace();
}
catch ( IOException e )
{
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String result)
{
//textView.setText(result);
}
@Override
protected void onProgressUpdate(String result)
{
//textView.setText(result);
}
}
[输入转储器.java]
package com.mycompany.myapp2;
import android.os.*;
import android.text.*;
import java.io.*;
import java.net.*;
import android.widget.*;
public class InputDumper extends AsyncTask<Object, String, String>
{
MainActivity callerActivity;
private String response = "";
private Integer i = 0;
private Boolean Connected = false;
protected String doInBackground(Object... params)
{
try
{
Socket socket = (Socket)params[0];
String messages[] = (String[])params[1];
String nickname = (String)params[2];
callerActivity = (MainActivity) params[3];
//Pattern pattern = Pattern.compile("USERNAME: (.*)");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String msg;
while ((msg = bufferedReader.readLine()) != null)
{
if (msg.indexOf("USERNAME:") > 0)
{
new FtpConnectionTask().execute(new Object[] { nickname, "emulation", callerActivity });
publishProgress("FTP CONNECTION STARTED!!!");
}
if (msg.startsWith("PING"))
{
String PONG = "PONG " + msg.substring(msg.indexOf(":"));
new OutputWriter().execute(new Object[] { socket, PONG, callerActivity });
Connected = true;
}
else if (i == 4)
{
for (String message : messages)
{
new OutputWriter().execute(new Object[] { socket, message, callerActivity });
}
}
else if (i == 13) //msg.endsWith("servers"))
{
new OutputWriter().execute(new Object[] { socket, "JOIN #xbins", callerActivity });
}
else if (i == 20)
{
new OutputWriter().execute(new Object[] { socket, "PRIVMSG #xbins :!list", callerActivity });
}
response += msg;
publishProgress(msg);
i++;
}
}
catch (IOException e )
{
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String result)
{
//textView.setText(result);
}
private TextView textView;
@Override
protected void onProgressUpdate(String... progress)
{
textView = (TextView) callerActivity.findViewById(R.id.txtView);
textView.append(Html.fromHtml("<font color='green'>" + progress[0] + "</font><br />"));
final int scrollAmount = textView.getLayout().getLineTop(textView.getLineCount())
- textView.getHeight();
// if there is no need to scroll, scrollAmount will be <=0
if (scrollAmount > 0)
textView.scrollTo(0, scrollAmount);
else
textView.scrollTo(0, 0);
}
}
[OutputWriter.java]
package com.mycompany.myapp2;
import android.os.*;
import android.text.*;
import java.io.*;
import java.net.*;
import android.widget.*;
public class OutputWriter extends AsyncTask<Object, String, String>
{
MainActivity callerActivity;
private String response = "";
protected String doInBackground(Object... params)
{
try
{
Socket socket = (Socket)params[0];
String message = (String)params[1];
callerActivity = (MainActivity)params[2];
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bufferedWriter.write(message + "\n\r");
bufferedWriter.flush();
publishProgress(message);
}
catch (IOException e )
{
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String result)
{
//textView.setText(result);
}
private TextView textView;
@Override
protected void onProgressUpdate(String... progress)
{
textView = (TextView) callerActivity.findViewById(R.id.txtView);
textView.append(Html.fromHtml("<font color='red'>" + progress[0] + "</font><br />"));
final int scrollAmount = textView.getLayout().getLineTop(textView.getLineCount())
- textView.getHeight();
// if there is no need to scroll, scrollAmount will be <=0
if (scrollAmount > 0)
textView.scrollTo(0, scrollAmount);
else
textView.scrollTo(0, 0);
}
}
[FtpConnectionTask.java]
package com.mycompany.myapp2;
import android.os.*;
import android.text.*;
import android.util.*;
import android.widget.*;
import java.io.*;
import org.apache.commons.io.*;
import org.apache.commons.net.ftp.*;
import java.util.*;
import android.view.*;
import android.content.*;
import android.app.*;
public class FtpConnectionTask extends AsyncTask<Object, String, String>
{
public static CustomAdapter adapter;
public static RowData row;
MainActivity callerActivity;
private LayoutInflater mInflater;
public Vector<RowData> data;
private FTPClient mFTPClient = new FTPClient();
protected String doInBackground( Object... params )
{
try
{
if ( mFTPClient.isConnected() )
{
Log.v("TESTING", "Is connected");
String cmd = (String)params[0];
String[] args = (String[])params[1];
callerActivity = (MainActivity) params[2];
if (cmd == "cd")
{
mFTPClient.changeWorkingDirectory(args[0].toString());
String[] names = mFTPClient.listNames();
listRemote(names);
}
}
else
{
String user = (String)params[0];
String pass = (String)params[1];
callerActivity = (MainActivity) params[2];
mFTPClient.connect("distribution.xbins.org");
mFTPClient.login(user, pass);
mFTPClient.enterLocalActiveMode();
mFTPClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
publishProgress("WORKING DIR: " + mFTPClient.printWorkingDirectory());
publishProgress("LOGGED IN");
String[] names = mFTPClient.listNames();
listRemote(names);
FTPFile[] remoteFiles = mFTPClient.listFiles();
for ( FTPFile remoteFile : remoteFiles ) //int i = 0; i < remoteFiles.length; i++)
{
if ( remoteFile.getType() == FTPFile.FILE_TYPE )
{
String name = remoteFile.getName();
long length = remoteFile.getSize();
String readableLength = FileUtils.byteCountToDisplaySize(length);
publishProgress(name + ":\t\t" + readableLength);
}
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
final void listRemote(final String... params)
{
callerActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
mInflater = (LayoutInflater) callerActivity.getSystemService(callerActivity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();
for ( String name : params )
{
row = new RowData(name, "dir");
data.add(row);
}
adapter = new CustomAdapter(callerActivity, R.layout.row, R.id.item, data);
callerActivity.setListAdapter(adapter);
callerActivity.getListView().setTextFilterEnabled(true);
}
});
}
private TextView textView;
@Override
protected void onProgressUpdate( String... progress )
{
textView = (TextView) callerActivity.findViewById(R.id.txtView);
textView.append(Html.fromHtml("<font color='yellow'>" + progress[0] + "</font><br />"));
final int scrollAmount = textView.getLayout().getLineTop(textView.getLineCount())
- textView.getHeight();
// if there is no need to scroll, scrollAmount will be <=0
if ( scrollAmount > 0 )
textView.scrollTo(0, scrollAmount);
else
textView.scrollTo(0, 0);
}
public class RowData
{
protected String mItem;
protected String mDescription;
RowData( String item, String description )
{
mItem = item;
mDescription = description;
}
@Override
public String toString( )
{
return mItem + " " + mDescription;
}
}
public class CustomAdapter extends ArrayAdapter<RowData>
{
public CustomAdapter( Context context, int resource,
int textViewResourceId, List<RowData> objects )
{
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView( int position, View convertView, ViewGroup parent )
{
ViewHolder holder = null;
//widgets displayed by each item in your list
TextView item = null;
TextView description = null;
//data from your adapter
RowData rowData= getItem(position);
//we want to reuse already constructed row views...
if ( null == convertView )
{
convertView = mInflater.inflate(R.layout.row, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
//
holder = (ViewHolder) convertView.getTag();
item = holder.getItem();
item.setText(rowData.mItem);
description = holder.getDescription();
description.setText(rowData.mDescription);
return convertView;
}
}
/**
* Wrapper for row data.
*
*/
public class ViewHolder
{
private View mRow;
private TextView description = null;
private TextView item = null;
public ViewHolder( View row )
{
mRow = row;
}
public TextView getDescription( )
{
if ( null == description )
{
description = (TextView) mRow.findViewById(R.id.description);
}
return description;
}
public TextView getItem( )
{
if ( null == item )
{
item = (TextView) mRow.findViewById(R.id.item);
}
return item;
}
}
}
[行.xml]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00000000">
<TextView
android:id="@+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"/>
<TextView
android:id="@+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false" />
</LinearLayout>
[主.xml]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:id="@+id/txtView"
android:maxLines = "5"
android:scrollbars = "vertical" />
<ListView
android:id="@android:id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_below="@+id/txtView" />
</RelativeLayout>