首先,我想通知我正在创建一个Android应用程序,允许用户通过使用sqlite来存储数据来查看和注册联系人
而且,我使用AsyncTask来执行插入和查看数据
现在,我想知道我应该在哪里编写可点击的视图列表代码。
它应该写在backgroudTask或displaycontact页面中?
有人能为我提供一些如何植入它的例子吗?
这是示例代码和一些解释
这是backgoundTask。
package com.example.assignment;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.widget.ListView;
import android.widget.Toast;
public class BackgroundTask extends AsyncTask <String ,Contact, String> {
Context ctx;
ContactAdapter contactAdapter;
Activity activity;
ListView listView;
BackgroundTask(Context ctx){
this.ctx = ctx;
activity = (Activity)ctx;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Contact... values) {
contactAdapter.add(values[0]);
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String method = params[0];
DatabaseOperations databaseOperations = new DatabaseOperations(ctx);
if (method.equals("add_info")){
String cFamName = params[1];
String cFirName = params[2];
String cHouseNo = params[3];
String cStreet = params[4];
String cTown = params[5];
String cCountry = params[6];
String cPostcode = params[7];
String cTelNo = params[8];
SQLiteDatabase db = databaseOperations.getWritableDatabase();
databaseOperations.putInformation(db ,cFamName,cFirName,cHouseNo,cStreet,cTown,cCountry,cPostcode,cTelNo);
return "One Row Inserted...";
}
else if(method.equals("get_info")){
listView = (ListView)activity.findViewById(R.id.display_listview);
SQLiteDatabase db = databaseOperations.getReadableDatabase();
Cursor cursor = databaseOperations.getInformations(db);
contactAdapter = new ContactAdapter(ctx, R.layout.display_contact_row);
String cFamName, cFirName, cTelNo;
while(cursor.moveToNext()){
cFamName = cursor.getString(cursor.getColumnIndex(TableData.TableInfo.eeFamName));
cFirName = cursor.getString(cursor.getColumnIndex(TableData.TableInfo.eeFirName));
cTelNo = cursor.getString(cursor.getColumnIndex(TableData.TableInfo.eeTelNo));
Contact contact = new Contact(cFamName, cFirName, cTelNo);
publishProgress(contact);
}
return "get_info";
}
return null;
}
@Override
protected void onPostExecute(String result) {
if(result.equals("get_info")){
listView.setAdapter(contactAdapter);
}
else{
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
}
}
}
这是DisplayContact代码,显示名为backgroudTask的联系人以显示列表。
package com.example.assignment;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class DisplayContact extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_contact);
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute("get_info");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_contact, menu);
return true;
}
}
这是我的contactAdaper
package com.example.assignment;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class ContactAdapter extends ArrayAdapter<Object> {
List<Contact> list = new ArrayList<Contact>();
public ContactAdapter(Context context, int resource) {
super(context, resource);
// TODO Auto-generated constructor stub
}
public void add(Contact object){
list.add(object);
super.add(object);
}
@Override
public int getCount(){
return list.size();
}
@Override
public Object getItem(int position){
return list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
ContactHolder contactHolder;
if(row==null){
LayoutInflater layoutInflater =(LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.display_contact_row,parent,false);
contactHolder = new ContactHolder();
contactHolder.name = (TextView)row.findViewById(R.id.name);
contactHolder.number = (TextView)row.findViewById(R.id.number);
row.setTag(contactHolder);
}
else{
contactHolder = (ContactHolder)row.getTag();
}
Contact contact = (Contact)getItem(position);
contactHolder.name.setText(contact.getName().toString());
contactHolder.number.setText(contact.getTelNo().toString());
return row;
}
static class ContactHolder{
TextView name, number;
}
}
答案 0 :(得分:0)
这取决于您需要该代码的位置。
如果只在一个活动/片段中需要它,请将其放在该活动/片段中。如果你需要它进入很多领域,最好的方法是在你复制的对象上实现一个函数(片段?自定义对象?任务?),并在执行单击时使用适当的参数调用该函数。
例如,您可以让asyncTask扩展“OnClickListener”,并使用CREATE FUNCTION [dbo].[fn_GetAEAssumptionFacts]
(
@pScenarioName varchar(500) = NULL
,@pBuildingID varchar(500) = NULL
,@pLeaseID varchar(500) = NULL
,@pTenantName varchar(500) = NULL
)
RETURNS TABLE
AS
RETURN
select
.....
from ae11.dbo.rvw_FinancialLineItems fli
....
INNER JOIN ae11.dbo.rvw_Scenarios s on s.Id = pas.ScenarioId
left join
(select
externalID,
PropertyAssetId,
LeaseID,
BeginDate
from ae11.dbo.ivw_Leases
WHERE PropertyAssetID IN
(select ID from AE11.dbo.PropertyAssets where scenarioID =
(CASE WHEN isnull(@pScenarioName, '') = ''
THEN (select ID from AEX.[dbo].[ConfigurationFieldTable]
where [Type] = 'Lease Connect Current Scenario' )
ELSE @pScenarioName
END)
)
) lea
ON lea.LeaseID = uni.ExternalID
AND lea.PropertyAssetID = uni.PropertyAssetId
where 1=1
......
AND s.id = (CASE WHEN isnull(@pScenarioName, '') = ''
THEN (select ID from AEX.[dbo].[ConfigurationFieldTable]
where [Type] = 'Lease Connect Current Scenario' )
ELSE @pScenarioName
END)
。
如果你需要更多的参数,你可以在asyncTask中有一个函数,比如“doOnClick(View view,param1,param2 ...)”然后使用匿名的onClickListener:
button.setOnClickListener(asyncTask);
您的第一步是了解您的需求:本地代码?可重用的?...然后在将要复制的内容(或在活动中)实现该功能。