2

我创建了一个listview,并想知道如何让每个diff。项目打开差异。活动/意图 例如:在列表视图 -start之旅(它会抢了GPS定位数据发送到服务器马上和确认其已发送)Android:如何让listview中的项目打开diff。活动/意图

- 时钟在(它会抢时间/日期数据和发送到服务器的时候了,并确认其已发送)

- 客户SVC(意向条形码扫描仪和返回结果没有显示发送结果数据发送给服务器

马上) 非依赖性检查(意向到条形码扫描器并返回结果无显示并将结果数据发送到

服务器马上) -pick向上(意向条形码扫描仪和返回结果没有显示和结果数据发送到服务器右

远)

-Log了(它会要求输入密码才能完成注销)

即将使用项目2.2与sdk 3即将使用1.5固件设备motorola i1。我导入了zxing条码

扫描仪“android integration”到应该单独打开条码扫描器应用程序的项目。我正在使用eclipse。最后一件事我得到了resultTxt错误,并想知道如何解决这个问题?

感谢 美林

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView android:id="@+id/selection" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 
    <ListView android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:drawSelectorOnTop="false"/> 
</LinearLayout>  

Customer.java

import android.app.ListActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

public class Customer extends ListActivity 
{ 
    TextView selection; 
    String[] items = { "Start Trip", "Clock in", "Customer Svc", 
      "Independent Inspection", "Pick Up", "Log Out" }; 

    @Override 
    public void onCreate(Bundle icicle) 
    { 
     super.onCreate(icicle); 
     setContentView(R.layout.main); 
     setListAdapter(new ArrayAdapter<String>(
       this, android.R.layout.simple_list_item_1, items)); 
     selection = (TextView) findViewById(R.id.selection); 
    } 

    public void onListItemClick(ListView parent, View v, int position, long id) 
    { 
     Intent intent = new Intent("com.company.merrill.IntentIntegrator"); 
     intent.setPackage("com.company.merrill"); 
     intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); 
     startActivityForResult(intent, 0); 
    } 

    public void onActivityResult(int requestCode, int resultCode, Intent intent) 
    { 
     if (requestCode == 0) 
     { 
      if (resultCode == RESULT_OK) 
      { 
       // contents contains whatever the code was 
       String contents = intent.getStringExtra("SCAN_RESULT"); 

       // Format contains the type of code i.e. UPC, EAN, QRCode etc... 
       String format = intent.getStringExtra("SCAN_RESULT_FORMAT"); 

       // Handle successful scan. In this example 
       // I just put the results into the TextView 
       resultsTxt.setText(format + "\n" + contents); 
      } 
      else if (resultCode == RESULT_CANCELED) 
      { 
       // Handle cancel. If the user presses 'back' 
       // before a code is scanned. 
       resultsTxt.setText("Canceled"); 
      } 
     } 
    } 
} 
+0

是什么resultsTxt?它在哪里宣布?初始化? – rekaszeru 2011-05-13 16:25:46

+0

它来自代码示例,假设它可能是导入的类的一部分。 – merrill 2011-05-13 17:21:05

+0

如果它没有在任何地方声明,那么在你使用它的行上必须有编译错误。根据它的名称和'setText'的用法,它可能是一个'TextView'实例,但它必须在某处(全局或在'onActivityResult'方法内)声明和初始化,就像'final TextView resultsTxt =( TextView)findViewById(R.id.results_txt);' – rekaszeru 2011-05-13 17:26:00

回答

4

您可以在项目的开始ListView基于位置不同的活动你只需点击:

private static final int ACTIVITY_0 = 0; 
private static final int ACTIVITY_1 = 1; 
private static final int ACTIVITY_2 = 2; 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) 
{ 
    super.onListItemClick(l, v, position, id); 
    final Intent intent = new Intent(); 
    // Set up different intents based on the item clicked: 
    switch (position) 
    { 
     case ACTIVITY_0: 
      intent.setClass(this, com.company.merrill.IntentIntegrator.class); 
      break; 
     case ACTIVITY_1: 
      intent.setClass(this, com.company.merrill.SecondActivity.class); 
      break; 
     case ACTIVITY_2: 
      intent.setClass(this, com.company.merrill.ThirdActivity.class); 
      break; 
     default: 
      break; 
    } 
    // Pass the item position as the requestCode parameter, so on the `Activity`'s 
    // return you can examine it, and determine which activity were you in prior. 
    startActivityForResult(intent, position); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent intent) 
{ 
    super.onActivityResult(requestCode, resultCode, intent); 
    if (resultCode == RESULT_OK) 
    { 
     // Perform different actions based on from which activity is 
     // the application returning: 
     switch (requestCode) 
     { 
      case ACTIVITY_0: 
       // contents contains whatever the code was 
       String contents = intent.getStringExtra("SCAN_RESULT"); 

       // Format contains the type of code i.e. UPC, EAN, QRCode etc... 
       String format = intent.getStringExtra("SCAN_RESULT_FORMAT"); 

       // Handle successful scan. In this example 
       // I just put the results into the TextView 
       resultsTxt.setText(format + "\n" + contents); 
       break; 
      case ACTIVITY_1: 
       // TODO: handle the return of the SecondActivity 
       break; 
      case ACTIVITY_2: 
       // TODO: handle the return of the ThirdActivity 
       break; 
      default: 
       break; 
     } 
    } 
    else if (resultCode == RESULT_CANCELED) 
    { 
     // Handle cancel. If the user presses 'back' 
     // before a code is scanned. 
     resultsTxt.setText("Canceled"); 
    } 
} 

更新
对于多一点优雅的结果是,你可以创建自己的类型列表项的数据:使用这个类
CustomerListItem.java

public class CustomerListItem 
{ 
    private String label; 
    private Class<?> activity; 

    /** 
    * @param label 
    * @param activity 
    */ 
    public CustomerListItem(String label, Class<?> activity) 
    { 
     super(); 
     this.label = label; 
     this.activity = activity; 
    } 

    /** 
    * @return the label 
    */ 
    public String getLabel() 
    { 
     return label; 
    } 

    /** 
    * @param label the label to set 
    */ 
    public void setLabel(String label) 
    { 
     this.label = label; 
    } 

    /** 
    * @return the activity 
    */ 
    public Class<?> getActivity() 
    { 
     return activity; 
    } 

    /** 
    * @param activity the activity to set 
    */ 
    public void setActivity(Class<Activity> activity) 
    { 
     this.activity = activity; 
    } 

    /* (non-Javadoc) 
    * @see java.lang.Object#toString() 
    */ 
    @Override 
    public String toString() 
    { 
     return this.label; 
    } 
} 

你能结合一个Activity到一个列表项,所以当它被点击时,你会知道哪个Activity开始。

Customer.java类看起来像:

public class Customer extends ListActivity 
{ 
    TextView selection; 
    CustomerListItem[] items = { 
      new CustomerListItem("Start Trip", StartTripActivity.class), 
      new CustomerListItem("Clock in", ClockinActivity.class), 
      new CustomerListItem("Customer Svc", CustomerSvcActivity.class), 
      new CustomerListItem("Independent Inspection", IInspectionActivity.class), 
      new CustomerListItem("Pick Up", PickUpActivity.class), 
      new CustomerListItem("Log Out", LogoutActivity.class)}; 

    @Override 
    public void onCreate(Bundle icicle) 
    { 
     super.onCreate(icicle); 
     setContentView(R.layout.main); 
     setListAdapter(new ArrayAdapter<CustomerListItem>(
       this, android.R.layout.simple_list_item_1, items)); 
     selection = (TextView) findViewById(R.id.selection); 
    } 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) 
    { 
     super.onListItemClick(l, v, position, id); 
     final Intent intent = new Intent(this, items[position].getActivity()); 
     startActivityForResult(intent, position); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) 
    { 
     super.onActivityResult(requestCode, resultCode, intent); 
     if (resultCode == RESULT_OK) 
     { 
      // Perform different actions based on from which activity is 
      // the application returning: 
      switch (requestCode) 
      { 
       case 0: 
        // TODO: handle the return of the StartTripActivity 
        break; 
       case 1: 
        // TODO: handle the return of the ClockinActivity 
        break; 
       case 2: 
        // TODO: handle the return of the CustomerSvcActivity 
        // and so on... 
        break; 
       default: 
        break; 
      } 
     } 
     else if (resultCode == RESULT_CANCELED) 
     { 
      resultsTxt.setText("Canceled"); 
     } 
    } 
} 
+0

非常感谢。我想问你的活动2你有一个时间/日期的样本,因为用户点击列表视图中的“时钟输入”,它会立即将当前/日期发送到数据库。至于活动3,我想知道如何在硬编码中输入“注销”用户将无法注销,但他们的经理将不得不批准和注销自己。 – merrill 2011-05-16 15:04:32

+0

我很乐意帮助你,尽管我建议你为这些问题开辟一条新线索,所以要保持StakOverflow的清洁,并将注意力集中在特定问题上。这样其他人可能会找到帮助他们。谢谢! – rekaszeru 2011-05-16 15:12:00

+0

好吧听起来像一个好主意再次感谢 – merrill 2011-05-16 15:18:38

相关问题