我正在和朋友一起申请,我们有点卡住了。我有alredy设法有一个多行LisView和香港专业教育学院为它做了一个自定义适配器。这个应用程序所做的是从其他活动注册汽车,当点击提交按钮时,我们想要从汽车中添加3个项目到我们在选项卡式活动中的ListView。我已经尝试了一些使用Intent和Bundle的东西来获取信息然后把它放进去,但是它给出了一个空错误。
这是我的项目,直到现在:
MainActivity:
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, CarType.class);
MainActivity.this.startActivity(intent);
}
});
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SampleFragmentPageAdapter(getSupportFragmentManager(),
MainActivity.this));
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
Fragment fragment = null;
if(tab.getPosition()==0) {
PageFragment pageFragment = new PageFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container, pageFragment ).commit();
}
if(tab.getPosition()==1){
SecondPageFragment secondPageFragment = new SecondPageFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container,secondPageFragment).commit();
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
页面片段:
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class PageFragment extends ListFragment {
String[] cars ;
String[] modelo ;
String[] placas ;
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
public static PageFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}
public PageFragment() {
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle intent = getActivity().getIntent().getExtras();
cars = new String[]{intent.getString("marca")};
modelo = new String[]{intent.getString("modelo")};
placas = new String[]{intent.getString("placas")};
View view = inflater.inflate(R.layout.activity_page_fragment, container, false);
Adapter adapter = new Adapter(getActivity(), cars, modelo, placas);
ListView listView = (ListView) view.findViewById(android.R.id.list);
listView.setAdapter(adapter);
return view;
}
}
适配器:
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 Adapter extends ArrayAdapter<String> {
String [] cars ;
String [] modelo ;
String [] placas ;
Context c;
LayoutInflater inflater;
public Adapter(Context context, String[] cars, String[] modelo, String[] placas){
super(context, R.layout.single_row, cars);
this.c=context;
this.cars=cars;
this.modelo=modelo;
this.placas=placas;
}
public class ViewHolder{
TextView cars;
TextView model;
TextView placas;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.single_row,null);
}
final ViewHolder holder = new ViewHolder();
holder.cars=(TextView)convertView.findViewById(R.id.textView2);
holder.model=(TextView)convertView.findViewById(R.id.textView3);
holder.placas=(TextView)convertView.findViewById(R.id.textView4);
holder.cars.setText(cars[position]);
holder.model.setText(modelo[position]);
holder.placas.setText(placas[position]);
return convertView;
}
}
CarType:这是我想要将信息传递到列表视图的形式
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.*;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
public class CarType extends AppCompatActivity {
ImageButton androidImageButton;
ImageView imageView;
static final int CAM_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_car_type);
final EditText etMarca = (EditText)findViewById(R.id.etMarca);
final EditText etModelo = (EditText)findViewById(R.id.etModelo);
final EditText etPlacas = (EditText)findViewById(R.id.etPlacas);
final EditText etColor = (EditText)findViewById(R.id.etColor);
final CheckBox cbCamioneta = (CheckBox)findViewById(R.id.cbCamioneta);
final CheckBox cbCar = (CheckBox)findViewById(R.id.cbCar);
final boolean checkStatus = cbCamioneta.isChecked();
Button bSubmit = (Button)findViewById(R.id.bSubmit);
assert bSubmit != null;
bSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Adapter.class);
intent.putExtra("marca", etMarca.getText().toString());
intent.putExtra("modelo", etModelo.getText().toString());
intent.putExtra("placas", etPlacas.getText().toString());
intent.putExtra("color", etColor.getText().toString());
intent.putExtra("camioneta", cbCamioneta.getText().toString());
intent.putExtra("car", cbCar.getText().toString());
intent.putExtra("checkboxstatus", checkStatus);
startActivity(intent);
final String modelo = etModelo.getText().toString();
final String marca = etMarca.getText().toString();
final String placas = etPlacas.getText().toString();
final String color = etColor.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
Intent intent = new Intent(CarType.this, MainActivity.class);
CarType.this.startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(CarType.this);
builder.setMessage("Register Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
CarRegisterRequest carregisterRequest = new CarRegisterRequest(marca, modelo, placas, color, responseListener);
RequestQueue queue = Volley.newRequestQueue(CarType.this);
queue.add(carregisterRequest);
}
});
androidImageButton = (ImageButton) findViewById(R.id.image_button_android);
imageView = (ImageView) findViewById(R.id.Final_picture);
androidImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = getFile();
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(camera_intent, CAM_REQUEST);
}
});
}
private File getFile(){
File folder = new File ("sdcard/camera_app");
if (!folder.exists()){
folder.mkdir();
}
File image_file = new File(folder, "cam_image.jpg");
return image_file;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String path = "sdcard/camera_app/cam_image.jpg";
imageView.setImageDrawable(Drawable.createFromPath(path));
}
}
在此之后,我的页面片段的xml文件只包含一个ListView,我有一个&#34; single_row.xml&#34;其中的格式是多行ListView。
任何帮助都将非常感激,我对Android和本网站都很陌生,所以如果我没有直接得到答案,我会道歉。
非常感谢。
答案 0 :(得分:0)
活动在android中如何沟通非常容易
在您开始结果活动时,您需要startActivityForResult(intent, requestCode)
MainActivity
,您需要在同一个活动(MainActivity
)中等待结果,然后您将获得导致您的onActivityResult(int requestCode, int resultCode, Intent data)
以及该意图中的所有数据。
public class MainActivity extends AppCompatActivity {
private static final int RQ_CAR = 1;
....
....
// Open the second activity
Intent intent = new Intent(MainActivity.this, CarType.class);
startActivityForResult(intent, RQ_CAR);
....
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RQ_CAR && resultCode == Activity.RESULT_OK) {
Bundle bundle = data.getExtras();
// Extract data and do whatever you want
}
}
}
但是我可以在哪里分配意图中的Bundle并将resultCode设置为OK?
在您的第二个活动(CarType)中,您需要手动执行此操作
Intent intent = new Intent();
Bundle extras = new Bundle();
extras.put....(); // put all your data here
intent.putExtras(extras);
setResult(Activity.RESULT_OK, intent);