gpt4 book ai didi

javascript - 从 ListView 调用 (OnitemClick)

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:16:32 25 4
gpt4 key购买 nike

我想尝试从 ListView 进行调用(将“Toast”更改为直接调用 ListView)。

希望大家帮帮我。

ContactListActiviy.java

package com.ngohung.view;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.markupartist.android.widget.ActionBar;
import com.ngohung.example.adapter.ExampleContactAdapter;
import com.ngohung.example.models.ExampleContactItem;
import com.ngohung.example.models.ExampleDataSource;
import com.ngohung.widget.ContactItemComparator;
import com.ngohung.widget.ContactItemInterface;
import com.ngohung.widget.ContactListView;

import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class ContactListActivity extends Activity implements TextWatcher {

private ExampleContactListView listview;

private EditText searchBox;
private String searchString;

private Object searchLock = new Object();
boolean inSearchMode = false;

private final static String TAG = "com.ngohung.view.ContactListActivity";

List<ContactItemInterface> contactList;
List<ContactItemInterface> filterList;
private SearchListTask curSearchTask = null;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_list);

final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);

actionBar.setTitle("Layanan Publik Kendari");
actionBar.setHomeLogo(R.drawable.ic_launcher);


filterList = new ArrayList<ContactItemInterface>();
contactList = ExampleDataSource.getSampleContactList();

ExampleContactAdapter adapter = new ExampleContactAdapter(this, R.layout.example_contact_item, contactList);

listview = (ExampleContactListView) this.findViewById(R.id.listview);
listview.setFastScrollEnabled(true);
listview.setAdapter(adapter);

// use this to process individual clicks
// cannot use OnClickListener as the touch event is overrided by IndexScroller
// use last touch X and Y if want to handle click for an individual item within the row
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View v, int position,
long id) {
List<ContactItemInterface> searchList = inSearchMode ? filterList : contactList ;

float lastTouchX = listview.getScroller().getLastTouchDownEventX();
if(lastTouchX < 45 && lastTouchX > -1){
Toast.makeText(ContactListActivity.this, "User image is clicked ( " + searchList.get(position).getItemForIndex() + ")", Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(ContactListActivity.this, "Nomor: " + searchList.get(position).getItemForIndex() , Toast.LENGTH_SHORT).show();
}
});

//My Trial to Add Call

//listview.setOnClickListener(new View.OnClickListener() {

// @Override
//public void onClick(View arg0) {
// TODO Auto-generated method stub
// String phone_no= listview.getScroller().toString().replaceAll("-", ""); //Create "getNumber" exampleContactListView
//Intent callIntent = new Intent(Intent.ACTION_CALL);
//callIntent.setData(Uri.parse(phone_no));
//callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//startActivity(callIntent);
//}
// });

//END Trial


searchBox = (EditText) findViewById(R.id.input_search_query);
searchBox.addTextChangedListener(this);
}

@Override
public void afterTextChanged(Editable s) {


searchString = searchBox.getText().toString().trim().toUpperCase();

if(curSearchTask!=null && curSearchTask.getStatus() != AsyncTask.Status.FINISHED)
{
try{
curSearchTask.cancel(true);
}
catch(Exception e){
Log.i(TAG, "Fail to cancel running search task");
}

}
curSearchTask = new SearchListTask();
curSearchTask.execute(searchString); // put it in a task so that ui is not freeze
}


@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// do nothing
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// do nothing
}

private class SearchListTask extends AsyncTask<String, Void, String> {



@Override
protected String doInBackground(String... params) {
filterList.clear();

String keyword = params[0];

inSearchMode = (keyword.length() > 0);

if (inSearchMode) {
// get all the items matching this
for (ContactItemInterface item : contactList) {
ExampleContactItem contact = (ExampleContactItem)item;

if ((contact.getNama().toUpperCase().indexOf(keyword) > -1) ) {
filterList.add(item);
}

}


}
return null;
}

protected void onPostExecute(String result) {

synchronized(searchLock)
{

if(inSearchMode){

ExampleContactAdapter adapter = new ExampleContactAdapter(ContactListActivity.this, R.layout.example_contact_item, filterList);
adapter.setInSearchMode(true);
listview.setInSearchMode(true);
listview.setAdapter(adapter);
}
else{
ExampleContactAdapter adapter = new ExampleContactAdapter(ContactListActivity.this, R.layout.example_contact_item, contactList);
adapter.setInSearchMode(false);
listview.setInSearchMode(false);
listview.setAdapter(adapter);
}
}

}
}


}

ExampleDataSource.java(顺便说一句,我使用的是“ArrayList”)

package com.ngohung.example.models;

import java.util.ArrayList;
import java.util.List;

import com.ngohung.widget.ContactItemInterface;

public class ExampleDataSource {

public static List<ContactItemInterface> getSampleContactList(){
List<ContactItemInterface> list = new ArrayList<ContactItemInterface> ();

list.add(new ExampleContactItem("Polda SULTRA" , "3195040" ,"Sentral PABX jl haluoleo 1" ) );
list.add(new ExampleContactItem("Polda SULTRA" ,"3195041" , "Sentral PABX jl haluoleo 1") );
list.add(new ExampleContactItem("Polda SULTRA" ,"3195042" , "Sentral PABX jl haluoleo 1" ) );
list.add(new ExampleContactItem("Direktorat diklat" , "3190549" , "Sentral PABX jl haluoleo 1") );
list.add(new ExampleContactItem("Direktorat intelkam" , "3190041", "Jl mayjen di panjaitan 1" ) );
list.add(new ExampleContactItem("Direktorat samapta" ,"3005114", "Jl mayjen di panjaitan") );
list.add(new ExampleContactItem("Direktorat lantas" , "3006112" , "Jl mayjen di panjaitan" ) );
list.add(new ExampleContactItem("Direktorat pol air" , "3007604", "Jl mayjen di panjaitan" ) );

return list;
}


}

谢谢..

最佳答案

添加<uses-permission android:name="android.permission.CALL_PHONE" />对您的 AndroidManifest 文件的许可。

然后在您的列表项点击监听器中启动一个 Activity :

Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:43..." ));
startActivity(intent);

更新:

只需在您的项目点击中添加以上代码:

    @Override
public void onItemClick(AdapterView parent, View v, int position, long id) {
List<ContactItemInterface> searchList = inSearchMode ? filterList : contactList ;

float lastTouchX = listview.getScroller().getLastTouchDownEventX();
if(lastTouchX < 45 && lastTouchX > -1){
Toast.makeText(ContactListActivity.this, "User image is clicked ( " + searchList.get(position).getItemForIndex() + ")", Toast.LENGTH_SHORT).show();
} else
Toast.makeText(ContactListActivity.this, "Nomor: " + searchList.get(position).getItemForIndex() , Toast.LENGTH_SHORT).show();

Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:xxx" ));// replcae "xxx" with the desired number
startActivity(intent);
}
}

关于javascript - 从 ListView 调用 (OnitemClick),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33775578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com